mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
Add Telegram blocks that allow the use of [Telegram bots' API features](https://core.telegram.org/bots/features). ### Changes 🏗️ 1. Credentials & API layer: Bot token auth via `APIKeyCredentials`, helper functions for JSON API calls (call_telegram_api) and multipart file uploads (call_telegram_api_with_file) 2. Trigger blocks: - `TelegramMessageTriggerBlock` — receives messages (text, photo, voice, audio, document, video, edited message) with configurable event filters - `TelegramMessageReactionTriggerBlock` — fires on reaction changes (private chats auto, groups require admin) 2. Action blocks (11 total): - Send: Message, Photo, Voice, Audio, Document, Video - Reply to Message, Edit Message, Delete Message - Get File (download by file_id) 3. Webhook manager: Registers/deregisters webhooks via Telegram's setWebhook API, validates incoming requests using X-Telegram-Bot-Api-Secret-Token header 4. Provider registration: Added TELEGRAM to ProviderName enum and registered `TelegramWebhooksManager` 5. Media send blocks support both URL passthrough (Telegram fetches directly) and file upload for workspace/data URI inputs ### Checklist 📋 #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - [x] Non-AI UUIDs - [x] Blocks work correctly - [x] SendTelegramMessageBlock - [x] SendTelegramPhotoBlock - [x] SendTelegramVoiceBlock - [x] SendTelegramAudioBlock - [x] SendTelegramDocumentBlock - [x] SendTelegramVideoBlock - [x] ReplyToTelegramMessageBlock - [x] GetTelegramFileBlock - [x] DeleteTelegramMessageBlock - [x] EditTelegramMessageBlock - [x] TelegramMessageTriggerBlock (works for every trigger type) - [x] TelegramMessageReactionTriggerBlock --------- Co-authored-by: Reinier van der Leer <pwuts@agpt.co>
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
"""
|
|
Telegram Bot credentials handling.
|
|
|
|
Telegram bots use an API key (bot token) obtained from @BotFather.
|
|
"""
|
|
|
|
from typing import Literal
|
|
|
|
from pydantic import SecretStr
|
|
|
|
from backend.data.model import APIKeyCredentials, CredentialsField, CredentialsMetaInput
|
|
from backend.integrations.providers import ProviderName
|
|
|
|
# Bot token credentials (API key style)
|
|
TelegramCredentials = APIKeyCredentials
|
|
TelegramCredentialsInput = CredentialsMetaInput[
|
|
Literal[ProviderName.TELEGRAM], Literal["api_key"]
|
|
]
|
|
|
|
|
|
def TelegramCredentialsField() -> TelegramCredentialsInput:
|
|
"""Creates a Telegram bot token credentials field."""
|
|
return CredentialsField(
|
|
description="Telegram Bot API token from @BotFather. "
|
|
"Create a bot at https://t.me/BotFather to get your token."
|
|
)
|
|
|
|
|
|
# Test credentials for unit tests
|
|
TEST_CREDENTIALS = APIKeyCredentials(
|
|
id="01234567-89ab-cdef-0123-456789abcdef",
|
|
provider="telegram",
|
|
api_key=SecretStr("test_telegram_bot_token"),
|
|
title="Mock Telegram Bot Token",
|
|
expires_at=None,
|
|
)
|
|
|
|
TEST_CREDENTIALS_INPUT = {
|
|
"provider": TEST_CREDENTIALS.provider,
|
|
"id": TEST_CREDENTIALS.id,
|
|
"type": TEST_CREDENTIALS.type,
|
|
"title": TEST_CREDENTIALS.title,
|
|
}
|