///
The `air-quality` bot leverages a sophisticated `Message` class (found in `bot_refactor_test.py`) to categorize Air Quality Index (AQI) values, provide relevant advice, and generate dynamic, multiling
42 views
~42 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.
The air-quality bot leverages a sophisticated Message class (found in bot_refactor_test.py) to categorize Air Quality Index (AQI) values, provide relevant advice, and generate dynamic, multilingual updates for its Telegram subscribers. This class centralizes all textual content and logic for interpreting AQI data into human-readable messages.
Message Class: Definitions and InterpretationsThe Message class serves as the core linguistic engine for the bot. It defines:
verdict: A dictionary containing concise descriptions of each AQI category.advice: A dictionary offering actionable recommendations corresponding to each AQI category.Both verdict and advice are structured to support multiple languages, ensuring the bot's messages are accessible to a diverse audience. The supported languages include English (en), Khmer (kh), Japanese (jp), and German (de).
get_aqi_category MethodThis method is crucial for translating raw AQI (US AQI - aqius) values into a predefined health-based category. It takes an integer aqius value and returns a string indicating its category.
Here are the AQI ranges and their corresponding categories:
0 to 5051 to 100101 to 150151 to 200201 to 300301 and aboveverdict and advice ExamplesTo illustrate the multilingual nature, here are snippets from the verdict and advice dictionaries for the "good" and "unhealthy" categories:
verdict dictionary snippet:
advice dictionary snippet:
morning_message and signoffFor specific, recurring times, the Message class provides lambda functions to generate fixed messages:
morning_message(self, aqius): This function generates the daily good morning message. It incorporates the current AQI value (aqius), determines its category using get_aqi_category, and pulls the corresponding verdict and advice in all supported languages.signoff(self): This function generates the end-of-day sign-off message. It's a static message informing users that the bot is concluding its updates until the next morning, also provided in multiple languages.These lambda functions are called by the [Bot Core Logic and Scheduling] at 6:00 AM and 9:00 PM, respectively.
update MethodThe update method is responsible for constructing dynamic messages for regular hourly or sub-hourly reports. It intelligently compares the current AQI (aqius) and its category with the previous AQI (aqius_prior) and its category to provide context about changes.
The method determines the nature of the change using a change tuple, which can be one of:
["stagnant", "samecat", "samecat"]: AQI value changed by 2 or less, and category is the same.["deteriorating", "samecat", "samecat"]: AQI value increased by more than 2, and category is the same.["improving", "samecat", "samecat"]: AQI value decreased by more than 2, and category is the same.["deteriorating", previous_category, current_category]: AQI value increased, and category has changed.["improving", previous_category, current_category]: AQI value decreased, and category has changed.Based on this change tuple, the method selects one of three message templates (samecat, change_samecat, change) from an internal messages dictionary. It then populates this template with the current time, AQI values (current and prior), categories, and translated delta (e.g., "improved," "deteriorated," "stagnant") to form a comprehensive update.
When the main loop calls morning_message at 6 AM, for example, if the AQI is 45, the English part of the message might look like this:
🌅 Good morning, ladies and gentlemen. I hope you have had a restful sleep and are eager to begin the new day. The air today is at a **45**, which means it's **good**. 🌞 Go outside, breathe some of that fresh air and enjoy this wonderful day.
Similarly, for a regular update at 8:00, if the AQI was 45 (good) and remains 48 (good), the English message from the update method might be:
Currently, it is 8:00 with an update to the air quality. The air quality has **deteriorated** from **45** to **48**, which is still **good**. 🌞 Go outside, breathe some of that fresh air and enjoy this wonderful day.
The Message class ensures that all bot communications are not only informative but also contextually relevant and culturally appropriate through its robust multilingual framework.