Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Telegram Bot Brick that enables developers to create and manage Telegram bots through the Arduino app bricks framework. The brick provides methods for registering commands, handling text and photo messages, sending messages, and running the bot's polling loop.
Changes:
- Added TelegramBot class with command/message handler registration and message sending capabilities
- Added python-telegram-bot dependency (version >=21.1) to pyproject.toml
- Created brick configuration file specifying TELEGRAM_BOT_TOKEN environment variable
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/arduino/app_bricks/telegram_bot/telegram_bot.py | Core TelegramBot class implementing bot initialization, handler registration, and message sending |
| src/arduino/app_bricks/telegram_bot/brick_config.yaml | Configuration file defining brick metadata and required environment variables |
| src/arduino/app_bricks/telegram_bot/init.py | Module initialization exporting TelegramBot and telegram library components |
| src/arduino/app_bricks/telegram_bot/README.md | Placeholder documentation file (currently only contains title) |
| pyproject.toml | Dependency configuration adding python-telegram-bot to the telegram_bot optional dependency group |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1 @@ | |||
| # Telegram Bot Brick No newline at end of file | |||
There was a problem hiding this comment.
The README.md file only contains a title and no actual documentation. Other bricks in this codebase (e.g., MQTT) include comprehensive documentation with usage examples, code snippets, and explanations. This brick should follow the same pattern and include proper documentation on how to use the TelegramBot class, including examples of registering commands, handling messages, and sending messages.
| @brick | ||
| class TelegramBot: | ||
| """A brick to manage Telegram Bot interactions.""" | ||
|
|
||
| def __init__(self, token: str = None): | ||
| """Initialize the bot with a token from arg or environment variable.""" | ||
| self.token = token or os.getenv("TELEGRAM_BOT_TOKEN") | ||
| if not self.token: | ||
| raise ValueError("Telegram BOT_TOKEN must be provided or set as environment variable") | ||
|
|
||
| self.application = Application.builder().token(self.token).build() | ||
|
|
||
| def add_command(self, command: str, callback): | ||
| """Register a slash command (e.g., /start).""" | ||
| self.application.add_handler(CommandHandler(command, callback)) | ||
|
|
||
| def on_text(self, callback): | ||
| """Register a handler for text messages.""" | ||
| self.application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, callback)) | ||
|
|
||
| def on_photo(self, callback): | ||
| """Register a handler for photo messages.""" | ||
| self.application.add_handler(MessageHandler(filters.PHOTO & ~filters.COMMAND, callback)) | ||
|
|
||
| def send_message(self, chat_id: int, message_text: str): | ||
| """Send a message to a specific chat ID.""" | ||
| logger.info(f"Sending message to chat_id={chat_id if chat_id != 0 else self.chat_id}") | ||
| url = f"https://api.telegram.org/bot{self.token}/sendMessage" | ||
| payload = { | ||
| "chat_id": chat_id, | ||
| "text": message_text | ||
| } | ||
| try: | ||
| response = requests.post(url, data=payload) | ||
| response.raise_for_status() | ||
| logger.info("Message sent successfully!") | ||
| return response | ||
| except requests.exceptions.RequestException as e: | ||
| logger.error(f"An error occurred: {e}") | ||
| return None | ||
|
|
||
| def run(self): | ||
| """Start the Telegram polling loop.""" | ||
| logger.info("Telegram Bot starting...") | ||
| self.application.run_polling(allowed_updates=Update.ALL_TYPES) |
There was a problem hiding this comment.
The TelegramBot class lacks test coverage. Other bricks in this repository (such as MQTT, web_ui, wave_generator, etc.) have comprehensive test suites. Tests should be added to cover initialization, command registration, message handlers, send_message functionality, and the run method to maintain consistency with the testing standards of this codebase.
| logger.error(f"An error occurred: {e}") | ||
| return None | ||
|
|
||
| def run(self): |
There was a problem hiding this comment.
Why run()?
This is a brick: shouldn't you need to use the start()/stop() functions?
| description: A brick to interact with Telegram Bot API | ||
| variables: | ||
| - name: TELEGRAM_BOT_TOKEN | ||
| description: Telegram Bot Token |
There was a problem hiding this comment.
We need to add the mandatory flag
| @@ -0,0 +1 @@ | |||
| # Telegram Bot Brick No newline at end of file | |||
There was a problem hiding this comment.
Please add a simple example in examples/ folder
| """Register a handler for photo messages.""" | ||
| self.application.add_handler(MessageHandler(filters.PHOTO & ~filters.COMMAND, callback)) | ||
|
|
||
| async def send_message(self, chat_id: int, message_text: str): |
There was a problem hiding this comment.
We generally don't expose async APIs to the user, otherwise the user would have to make the application an async one and use asyncio due to an implementation detail on our side.
Could you make send_message a blocking method?
9abf082 to
5a127a8
Compare
df09cd5 to
6ebc1e2
Compare
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
5a127a8 to
896405f
Compare
This PR adds the Telegram Bot Brick, a brick designed to create you own Telegram bot.