///
This guide will walk you through the process of setting up and running the Air Quality Telegram Bot. The bot fetches air quality data for Phnom Penh and sends periodic updates, including advice and vi
115 views
~115 views from guests
Guest views are estimated from total page views. These include anonymous visitors and users who weren't logged in when they viewed the page.
This guide will walk you through the process of setting up and running the Air Quality Telegram Bot. The bot fetches air quality data for Phnom Penh and sends periodic updates, including advice and visual cues, to a designated Telegram chat in multiple languages.
To run this bot, you will need:
pyTelegramBotAPI (often installed as telebot)requestsThe bot relies on external services to function. You will need to obtain specific keys and identifiers:
@BotFather./newbot.@userinfobot (another Telegram bot) or @getidsbot. It will tell you your Chat ID.@getidsbot and add it to the same group. It will provide the Chat ID for that group (which typically starts with -).@getidsbot.It's best practice to keep sensitive information like API keys out of your code. You will set these as environment variables.
You can create a .env file in the root directory of your project to manage these variables. Make sure to install python-dotenv if you plan to use a .env file.
Install python-dotenv (if using .env file):
Create a .env file: In the root directory of the cloned repository, create a file named .env and add the following lines, replacing the placeholder values with your actual keys and ID:
Alternatively, you can set these variables directly in your shell environment, which might be preferred for deployment on servers:
Linux/macOS:
Windows (Command Prompt):
Windows (PowerShell):
keys.jsonThe bot uses a keys.json file to persist the previous Air Quality Index (AQI) value (aqius_prior) between runs, which is essential for detecting air quality changes.
Create keys.json: In the root directory of the cloned repository, create a file named keys.json.
Add initial content: Populate the file with the following JSON structure:
This initializes the aqius_prior value to 0, ensuring the bot starts correctly.
Follow these steps to get the repository and install the necessary dependencies:
Clone the Repository:
Create a Virtual Environment (Recommended):
Install Python Dependencies:
Prepare the labels directory: The bot sends images based on AQI categories. You need to create a labels directory in the root of your project and place the corresponding image files there. The images should be named according to the AQI categories (e.g., good.png, moderate.png, unhealthy_for_sensitive_groups.png, etc.).
Once all the prerequisites, API keys, environment variables, keys.json, and image files are set up, you can start the bot.
bot.py script:
The bot will now start running. You should see a "Test message sent!" in your console and, if configured correctly, your bot will send an initial "intell1slt_bot is now online" message to the specified CHAT_ID. It will then begin sending air quality updates according to its Workflow as described in the [Overview] page.