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.
103 lines
3.6 KiB
Markdown
103 lines
3.6 KiB
Markdown
# 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
|
|
|
|
1. Open Telegram and search for "BotFather" (@BotFather)
|
|
2. Start a chat with BotFather
|
|
3. Send the command `/newbot`
|
|
4. 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")
|
|
5. Once created, BotFather will provide you with a token (API key) that looks like this:
|
|
```
|
|
123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ
|
|
```
|
|
-> 8201929832:AAFhDu7LD4xbNyDQ9Cc2JSuTDMhqrLaDDdc
|
|
6. **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"
|
|
|
|
1. Search for "userinfobot" (@userinfobot) in Telegram
|
|
2. Start a chat with this bot
|
|
3. 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:
|
|
|
|
1. Add your bot to the group
|
|
2. Send a message in the group
|
|
3. Visit this URL in your browser (replace `YOUR_BOT_TOKEN` with your actual bot token):
|
|
```
|
|
https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates
|
|
```
|
|
4. Look for the "chat" object in the JSON response, which contains the "id" field
|
|
5. 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:
|
|
|
|
```python
|
|
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:
|
|
|
|
1. Make sure the application is running
|
|
2. Access the `/api/data` endpoint or wait for the regular data check
|
|
3. If any parts have red status, you should receive a notification in your Telegram chat
|
|
|
|
## Troubleshooting
|
|
|
|
If you're not receiving notifications:
|
|
|
|
1. Check that your bot token and chat ID are correct
|
|
2. Ensure your bot has permission to send messages to the chat
|
|
3. Check the application logs for any error messages related to Telegram notifications
|
|
4. Make sure the requests library is installed (`pip install requests`)
|
|
5. 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
|
|
|
|
## Additional Resources
|
|
|
|
- [Telegram Bot API Documentation](https://core.telegram.org/bots/api)
|
|
- [Python Requests Library Documentation](https://docs.python-requests.org/en/latest/) |