///
This page details the essential configuration elements required for the Air Quality Telegram Bot to function correctly: environment variables for sensitive API credentials and a `keys.json` file for m
79 views
~79 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 page details the essential configuration elements required for the Air Quality Telegram Bot to function correctly: environment variables for sensitive API credentials and a keys.json file for maintaining bot state.
The bot relies on several environment variables to secure sensitive information and ensure flexible deployment. These variables are loaded by the Main class in bot.py using os.getenv(). As detailed in the Getting Started guide, it is recommended to set these using a .env file or directly in your shell environment.
API_KEY (AirVisual)
Main.get_phnom_penh_aq method, the API_KEY is embedded in the API request URL: f"http://api.airvisual.com/v2/city?city=Phnom Penh&state=Phnom Penh&country=Cambodia&key={self.API_KEY}".BOT_TOKEN (Telegram)
Main.initializeBot method uses this token to create the telebot.TeleBot instance: telebot.TeleBot(self.BOT_TOKEN).@BotFather on Telegram; it will provide this token.CHAT_ID (Telegram)
Main.send_message and Main.send_only_message) target this CHAT_ID.@userinfobot or @getidsbot on Telegram to find the ID of your desired chat.keys.json FileThe keys.json file plays a crucial role in maintaining the bot's state, specifically for tracking changes in air quality over time.
Role and Purpose: This JSON file is used to store persistent data that needs to be retained across different runs or update cycles of the bot. Its primary function is to store the aqius_prior value. This value represents the Air Quality Index (AQI) from the previous update. By comparing the current AQI with aqius_prior, the bot can determine if the air quality has improved, deteriorated, or remained stagnant, and if the AQI category has changed. This logic is detailed in the Main.update method of the Bot Core Logic.
Structure:
The keys.json file is a simple JSON object. Initially, it should contain the aqius_prior key set to 0.
Read and Update Mechanism:
Main class loads the contents of keys.json into self.keys:
Main.update_keys method is responsible for modifying the keys.json file. After each relevant update, this method writes the new aqius_prior value back to the file, ensuring the state is saved for the next bot cycle.
Main.main method specifically resets aqius_prior to 0 at 6:00 AM daily to ensure the first morning message always treats the current AQI as a fresh reading.