Skip to content

Commit

Permalink
Slack notifier separately notify door and tool (#67)
Browse files Browse the repository at this point in the history
* Slack notifier separately notify door and tool

* Fix pylint
  • Loading branch information
leechwort authored Feb 11, 2024
1 parent 26a2ea6 commit 5640b86
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/config_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
],
"PLUGINS": {
"slack_notifier": {
"SLACK_CHANNEL": "#prismo-debug",
"SLACK_TOKEN": "xoxb-this-is-not-areal-slack-token"
"SLACK_TOOL_CHANNEL": "#prismo-debug",
"SLACK_DOOR_CHANNEL": "#prismo-door-channel",
"SLACK_TOKEN": "xoxb-this-is-not-areal-slack-token"
}
},
"READER_FIRMWARE_FLASHING_SCRIPT_PATH": "/home/artsin/Dev/prismo-reader/src/flasher.sh",
Expand Down
87 changes: 81 additions & 6 deletions app/plugins/slack_notifier/slack_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,48 @@ def unlock_message_block_constructor(tool, user):
}]


def door_message_block_constructor(door, user):
return [
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "emoji",
"name": "door",
"unicode": "1f6aa"
},
{
"type": "text",
"text": " "
},
{
"type": "text",
"text": "%s" % user,
"style": {
"bold": True
}
},
{
"type": "text",
"text": " entered through the "
},
{
"type": "text",
"text": "%s" % door,
"style": {
"bold": True
}
}
]
}
]
}
]


class SlackNotifierPlugin:
def __init__(self, app_context):
# Configure the Slack client with your token
Expand Down Expand Up @@ -102,19 +144,52 @@ def get_device_name(self, device_id):

return None

def get_device_type(self, device_id):
"""
Get device name based on its ID
"""
connection = sqlite3.connect(self.db_uri)
cursor = connection.cursor()

cursor.execute("SELECT type from devices WHERE id = ?",
(device_id,),
)
connection.commit()
result = cursor.fetchone()

connection.close()
if result:
return result[0]

return None

def access_log_entry_added(self, event):
try:
if event["operation"] == "unlock":
user_name = self.get_user_name(event["user_key"])
device_name = self.get_device_name(event["device_id"])
device_type = self.get_device_type(event["device_id"])
self.logger.info("Access log entry added")
self.logger.info("User name: %s", user_name)
self.logger.info("Device name: %s", device_name)
text_message = "🔓 * %s Tool was unlocked* by %s" % (device_name, user_name)
blocks = unlock_message_block_constructor(device_name, user_name)
self.slack_app.client.chat_postMessage(channel=self.config["SLACK_CHANNEL"],
text=text_message,
blocks=blocks)
if device_type == "tool":
self.logger.info("Device name: %s", device_name)
text_message = "🔓 * %s Tool was unlocked* by %s" % (device_name, user_name)
blocks = unlock_message_block_constructor(device_name, user_name)
self.slack_app.client.chat_postMessage(
channel=self.config["SLACK_TOOL_CHANNEL"],
text=text_message,
blocks=blocks)
elif device_type == "door":
self.logger.info("Door opened: %s", device_name)
text_message = "🔓 * %s Door opened by * by %s" % (device_name, user_name)
blocks = door_message_block_constructor(device_name, user_name)
self.slack_app.client.chat_postMessage(
channel=self.config["SLACK_DOOR_CHANNEL"],
text=text_message,
blocks=blocks)
else:
self.logger.error("Unknown reader type! %s", device_type)

except Exception as e:
self.logger.error("Error in SlackNotifierPlugin.access_log_entry_added: %s", e)
raise e
Expand Down

0 comments on commit 5640b86

Please sign in to comment.