Plugin — Telegram Bot
Plugin — Telegram Bot
El plugin de Telegram convierte mensajes de texto y notas de voz recibidos en un bot en tareas cascarón. Opera vía webhook: Telegram envía un POST al endpoint de Joe por cada actualización.
Mecanismo: Webhook · Autenticación: Secret Token en header · Tipos soportados: Texto, Voz
Endpoint del webhook
POST
/plugins/telegram/webhook/{plugin_id}/
| Parámetro | Ubicación | Descripción |
|---|---|---|
plugin_id |
Path | ID del PluginConfig registrado para este bot. |
X-Telegram-Bot-Api-Secret-Token |
Header | Token secreto configurado al registrar el webhook en Telegram. |
Configuración paso a paso
-
Crear el bot en Telegram con BotFather
/newbot # Sigue las instrucciones, BotFather te entrega: # Token: 1234567890:ABCDefghIJKlmnOPQrsTUVwxyz -
Registrar el PluginConfig en Joe
from apps.common.models import PluginConfig config = PluginConfig( usuario=user, servicio="telegram", identificador="@joe_bot", metadatos={"webhook_secret": "mi_secret_seguro_aleatorio"}, ) config.set_token("1234567890:ABCDefghIJKlmnOPQrsTUVwxyz") config.save() # Anota el ID generado: config.pk → ej. 3 -
Registrar el webhook en Telegram
# Reemplaza TOKEN, URL y SECRET curl -X POST \ "https://api.telegram.org/bot<TOKEN>/setWebhook" \ -H "Content-Type: application/json" \ -d '{ "url": "https://joe.pedroospino.net/plugins/telegram/webhook/3/", "secret_token": "mi_secret_seguro_aleatorio" }' # Respuesta esperada: # {"ok": true, "result": true, "description": "Webhook was set"} -
Verificar el webhook
curl "https://api.telegram.org/bot<TOKEN>/getWebhookInfo"
Flujo de un mensaje entrante
- Telegram envía
POSTal webhook con el update JSON. - Joe valida el header
X-Telegram-Bot-Api-Secret-Tokencontra elwebhook_secretalmacenado. - Se deserializa el payload (
TelegramUpdate). - Si es texto: se crea una tarea con
tipo=telegramy el texto como título. - Si es voz: se crea una tarea con
tipo=telegram_voiceypendiente_stt=Trueen metadatos. - Joe responde
{"ok": true}con HTTP 200 (evita reintentos de Telegram). - El bot envía un mensaje de confirmación al chat: "✅ Recibido. Encolando... [CODIGO]"
Campos en metadatos_extra de la tarea
| Campo | Descripción |
|---|---|
chat_id | ID del chat de Telegram. Necesario para responder. |
message_id | ID del mensaje en Telegram. |
plugin_id | ID del PluginConfig que procesó el mensaje. |
owner_id | ID del usuario dueño del plugin. |
texto_original | Texto completo del mensaje (solo en tareas de texto). |
pendiente_stt | true si es una nota de voz pendiente de transcripción. |
Nota V1.5 (NOVA): El soporte para transcripción de audio (Whisper STT) está planificado para V1.5. Actualmente las notas de voz generan una tarea pero no se transcriben automáticamente.
Campos del PluginConfig para Telegram
| Campo | Valor esperado |
|---|---|
servicio | "telegram" |
identificador | Username del bot: @joe_bot |
token_cifrado | Bot token de BotFather (cifrado con Fernet) |
metadatos.webhook_secret | String aleatorio seguro. Se envía a Telegram al registrar el webhook. |
The Telegram plugin converts text messages and voice notes received by a bot into shell tasks. It operates via webhook: Telegram sends a POST to Joe's endpoint for each update.
Mechanism: Webhook · Authentication: Secret Token in header · Supported types: Text, Voice
Webhook endpoint
POST
/plugins/telegram/webhook/{plugin_id}/
| Parameter | Location | Description |
|---|---|---|
plugin_id |
Path | ID of the PluginConfig registered for this bot. |
X-Telegram-Bot-Api-Secret-Token |
Header | Secret token configured when registering the webhook in Telegram. |
Step-by-step configuration
-
Create the bot in Telegram with BotFather
/newbot # Follow the instructions, BotFather gives you: # Token: 1234567890:ABCDefghIJKlmnOPQrsTUVwxyz -
Register the PluginConfig in Joe
from apps.common.models import PluginConfig config = PluginConfig( usuario=user, servicio="telegram", identificador="@joe_bot", metadatos={"webhook_secret": "my_secure_random_secret"}, ) config.set_token("1234567890:ABCDefghIJKlmnOPQrsTUVwxyz") config.save() # Note the generated ID: config.pk → e.g. 3 -
Register the webhook in Telegram
# Replace TOKEN, URL, and SECRET curl -X POST \ "https://api.telegram.org/bot<TOKEN>/setWebhook" \ -H "Content-Type: application/json" \ -d '{ "url": "https://joe.pedroospino.net/plugins/telegram/webhook/3/", "secret_token": "my_secure_random_secret" }' # Expected response: # {"ok": true, "result": true, "description": "Webhook was set"} -
Verify the webhook
curl "https://api.telegram.org/bot<TOKEN>/getWebhookInfo"
Incoming message flow
- Telegram sends
POSTto the webhook with the update JSON. - Joe validates the
X-Telegram-Bot-Api-Secret-Tokenheader against the storedwebhook_secret. - The payload is deserialized (
TelegramUpdate). - If text: a task is created with
tipo=telegramand the text as title. - If voice: a task is created with
tipo=telegram_voiceandpendiente_stt=Truein metadata. - Joe responds
{"ok": true}with HTTP 200 (prevents Telegram retries). - The bot sends a confirmation message to the chat: "✅ Received. Queuing... [CODE]"
Fields in task metadatos_extra
| Field | Description |
|---|---|
chat_id | Telegram chat ID. Required to reply. |
message_id | Message ID in Telegram. |
plugin_id | PluginConfig ID that processed the message. |
owner_id | ID of the plugin's owner user. |
texto_original | Full message text (text tasks only). |
pendiente_stt | true if it's a voice note pending transcription. |
Note V1.5 (NOVA): Audio transcription support (Whisper STT) is planned for V1.5. Currently voice notes create a task but are not automatically transcribed.
PluginConfig fields for Telegram
| Field | Expected value |
|---|---|
servicio | "telegram" |
identificador | Bot username: @joe_bot |
token_cifrado | BotFather bot token (Fernet-encrypted) |
metadatos.webhook_secret | Secure random string. Sent to Telegram when registering the webhook. |