Skip to content

Commit edf37e4

Browse files
committed
Add Discord notifier plugin
1 parent ac728f1 commit edf37e4

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Discord Notifier Plugin
3+
======================
4+
Notify members about tools and doors usage statistics in Discord
5+
6+
How to use?
7+
-----------
8+
1. Setup WebHooks for your channel.
9+
HOWTO: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
10+
2. Create new branch in settings, e.g:
11+
"PLUGINS": {
12+
"slack_notifier": {
13+
"SLACK_DOOR_CHANNEL": "#prismo-door-channel",
14+
"SLACK_TOKEN": "xoxb-this-is-not-areal-slack-token",
15+
"SLACK_TOOL_CHANNEL": "#prismo-debug"
16+
},
17+
"discord_notifier": {
18+
"DISCORD_DOOR_EVENT_WEBHOOK": "https://discord.com/api/webhooks/your-webhook"
19+
},
20+
"""
21+
from flask import Flask
22+
23+
from .discord_notifier import DiscordNotifierPlugin
24+
25+
26+
def init_plugin(app: Flask):
27+
DiscordNotifierPlugin(app.app_context())
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import sqlite3
2+
from discord import SyncWebhook
3+
4+
# pylint: disable=consider-using-f-string
5+
6+
class DiscordNotifierPlugin:
7+
def __init__(self, app_context):
8+
try:
9+
self.app_context = app_context
10+
self.logger = self.app_context.app.logger
11+
self.ee = self.app_context.app.ee # Event emitter, used for event-based communication
12+
self.config = self.app_context.app.config["PRISMO"]["PLUGINS"]["discord_notifier"]
13+
self.db_uri = self.app_context.app.config["DATABASE_URI"]
14+
self.ee.add_listener('access-log-entry-added', self.access_log_entry_added)
15+
self.app_context.app.logger.info("Discord Notifier Plugin initialized")
16+
except Exception as e:
17+
self.logger.error("Error in DiscordNotifierPlugin.__init__: %s", e)
18+
raise e
19+
20+
def get_user_name(self, user_key):
21+
"""
22+
Get user name and id based on user key info
23+
"""
24+
connection = sqlite3.connect(self.db_uri)
25+
cursor = connection.cursor()
26+
27+
cursor.execute("SELECT name from users WHERE key = ?",
28+
(user_key,),
29+
)
30+
connection.commit()
31+
result = cursor.fetchone()
32+
33+
connection.close()
34+
if result:
35+
return result[0]
36+
37+
return None
38+
39+
def get_device_name(self, device_id):
40+
"""
41+
Get device name based on its ID
42+
"""
43+
connection = sqlite3.connect(self.db_uri)
44+
cursor = connection.cursor()
45+
46+
cursor.execute("SELECT name from devices WHERE id = ?",
47+
(device_id,),
48+
)
49+
connection.commit()
50+
result = cursor.fetchone()
51+
52+
connection.close()
53+
if result:
54+
return result[0]
55+
56+
return None
57+
58+
def get_device_type(self, device_id):
59+
"""
60+
Get device name based on its ID
61+
"""
62+
connection = sqlite3.connect(self.db_uri)
63+
cursor = connection.cursor()
64+
65+
cursor.execute("SELECT type from devices WHERE id = ?",
66+
(device_id,),
67+
)
68+
connection.commit()
69+
result = cursor.fetchone()
70+
71+
connection.close()
72+
if result:
73+
return result[0]
74+
75+
return None
76+
77+
def access_log_entry_added(self, event):
78+
try:
79+
if event["operation"] == "unlock":
80+
user_name = self.get_user_name(event["user_key"])
81+
device_name = self.get_device_name(event["device_id"])
82+
device_type = self.get_device_type(event["device_id"])
83+
self.logger.info("Access log entry added")
84+
self.logger.info("User name: %s", user_name)
85+
if device_type == "tool":
86+
self.logger.info("Device name: %s", device_name)
87+
text_message = "🔩**%s** was unlocked by **%s**" % (device_name, user_name)
88+
webhook = SyncWebhook.from_url(self.config["DISCORD_TOOL_EVENT_WEBHOOK"])
89+
webhook.send(text_message)
90+
91+
elif device_type == "door":
92+
self.logger.info("Door opened: %s", device_name)
93+
text_message = "🔐**%s** was opened by **%s**" % (device_name, user_name)
94+
webhook = SyncWebhook.from_url(self.config["DISCORD_DOOR_EVENT_WEBHOOK"])
95+
webhook.send(text_message)
96+
else:
97+
self.logger.error("Unknown reader type! %s", device_type)
98+
99+
except Exception as e:
100+
self.logger.error("Error in DiscordNotifierPlugin.access_log_entry_added: %s", e)
101+
raise e

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ intelhex
1717
pyserial
1818
rshell
1919
esptool
20+
discord

0 commit comments

Comments
 (0)