Skip to content

Telegram bot#48

Open
mirkokurt wants to merge 18 commits intoarduino:release-0.6from
mirkokurt:telegram-bot
Open

Telegram bot#48
mirkokurt wants to merge 18 commits intoarduino:release-0.6from
mirkokurt:telegram-bot

Conversation

@mirkokurt
Copy link
Contributor

This PR adds the Telegram Bot Brick, a brick designed to create you own Telegram bot.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 13 to 57
@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)
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
logger.error(f"An error occurred: {e}")
return None

def run(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add the mandatory flag

@@ -0,0 +1 @@
# Telegram Bot Brick No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@dsammaruga dsammaruga added the applab-0.6 PR toward release-0.6 branch label Feb 2, 2026
@dsammaruga dsammaruga changed the base branch from release-1.1 to release-0.6 February 9, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

applab-0.6 PR toward release-0.6 branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants