You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.6 KiB
3.6 KiB
Telegram Notification Setup Guide
This guide explains how to set up Telegram notifications for the Digital Twin Monitoring application. The application will send alerts to a Telegram chat when parts with red status are detected.
Prerequisites
- Telegram account
- Internet connection
- Access to the Digital Twin Monitoring application
Step 1: Create a Telegram Bot
- Open Telegram and search for "BotFather" (@BotFather)
- Start a chat with BotFather
- Send the command
/newbot - Follow the instructions to create a new bot:
- Provide a name for your bot (e.g., "Digital Twin Monitor")
- Provide a username for your bot (must end with "bot", e.g., "digital_twin_monitor_bot")
- Once created, BotFather will provide you with a token (API key) that looks like this:
-> 8201929832:AAFhDu7LD4xbNyDQ9Cc2JSuTDMhqrLaDDdc123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ - Important: Keep this token secure and do not share it publicly
Step 2: Get Your Chat ID
There are several ways to get your Telegram chat ID:
Method 1: Using the "userinfobot"
- Search for "userinfobot" (@userinfobot) in Telegram
- Start a chat with this bot
- The bot will reply with your chat ID (a number like
123456789)
Method 2: For Group Chats
If you want to send notifications to a group:
- Add your bot to the group
- Send a message in the group
- Visit this URL in your browser (replace
YOUR_BOT_TOKENwith your actual bot token):https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates - Look for the "chat" object in the JSON response, which contains the "id" field
- This is your group chat ID (usually a negative number for groups) -> -1002721738007
Step 3: Configure the Application
There are two ways to configure the Telegram notification feature:
Method 1: Environment Variables (Recommended)
Set the following environment variables:
TELEGRAM_BOT_TOKEN=your_bot_token_here
TELEGRAM_CHAT_ID=your_chat_id_here
You can set these variables in your operating system, or create a .env file in the project root directory with these values.
Method 2: Direct Configuration in Code
You can also pass the bot token and chat ID directly when calling the notification function:
from telegram_notifier import check_red_status_and_notify
# After analyzing data and getting results
check_red_status_and_notify(results, bot_token="your_bot_token_here", chat_id="your_chat_id_here")
Testing the Setup
To test if your Telegram notification setup is working:
- Make sure the application is running
- Access the
/api/dataendpoint or wait for the regular data check - If any parts have red status, you should receive a notification in your Telegram chat
Troubleshooting
If you're not receiving notifications:
- Check that your bot token and chat ID are correct
- Ensure your bot has permission to send messages to the chat
- Check the application logs for any error messages related to Telegram notifications
- Make sure the requests library is installed (
pip install requests) - Verify that there are actually parts with red status in the system
Security Considerations
- Never hardcode your bot token in the source code
- Use environment variables or a secure configuration system
- Regularly rotate your bot token if you suspect it has been compromised
- Be careful about what information is included in the notifications