NOTICE: All information contained herein is, and remains
the property of TechnoCore.
The intellectual and technical concepts contained
herein are proprietary to TechnoCore and dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from TechnoCore.
The ObjNotifySlack class provides a straightforward interface for sending messages and files to Slack channels from within the backend application. It is designed to be used as part of the centralized notification system.
The class is configured through the main config.yaml file. The following section must be present for the class to initialize correctly:
slack:
token: "xoxb-your-slack-bot-token"
channel: "#your-default-channel"
__init__(self, DB: object = 0)The constructor initializes the Slack client. It reads the token and channel from the global configuration (config.yaml). If the token is missing or invalid, the client will not be initialized, and errors will be logged.
transmit_message(self, message: str, channel: str = "")Sends a text message to a Slack channel.
#alerts, @username). If omitted, the message will be sent to the default_channel from the configuration.transmit_message_file(self, message: str, file_path: str, channel: str = "")Sends a message and attaches a file to a Slack channel.
default_channel.not_in_channel ErrorIf you receive a not_in_channel error when trying to send a message, it means the Slack bot is not a member of the specified channel. To resolve this:
/invite @AxionBot (replacing AxionBot with your app's bot name) and press Enter.chat:write) to post in the channel. You can verify this in your app's configuration on the Slack API dashboard.from factory.sms.package.core.ObjNotifySlack import ObjNotifySlack
# Instantiate the notifier
# The database object (DB) may be required by the base class
slack_notifier = ObjNotifySlack(DB)
# Send a simple message to the default channel
slack_notifier.transmit_message("The nightly backup process has completed successfully.")
# Send a message to a specific channel
slack_notifier.transmit_message(
message="Critical alert: CPU usage has exceeded 95% on server 'web-prod-01'.",
channel="#critical-alerts"
)
# Send a message with a log file attached
slack_notifier.transmit_message_file(
message="An error occurred during data import. Please see the attached log for details.",
file_path="/home/axion/projects/axion/local.documents/logs/import_error.log",
channel="#dev-ops"
)