daaru00 / aws-telegram-bot-connector

AWS Serverless Telegram Bot Connector for message receive and send.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AWS Serverless Telegram Bot Connector

CloudFormation API Gateway EventBridge

This is a serverless application that implements a Telegram bot connector for message receive and send.

Created resources

This application create an API Gateway endpoint to listen for webhook request, an Event Bridge bus to send received messages.

Infrastructure Schema

Installation

Using the SAM CLI:

sam build
sam deploy --guided

Parameters

  • TelegramBotToken: Telegram bot token.

  • TelegramIPsWhitelist: Comma separate list of IPs of permitted senders IP, leave empty to disable whitelist.

Outputs

  • WebhookEndpoint: The Telegram bot Webhook URL

  • MessagesEventBusName: Name of EventBridge bus where message event are sent

Telegram bot configurations

Create a new bot talking to @BotFather and copy the provide token (needed for TelegramBotToken parameter).

Retrieve list of sender IPs

Telegram report which IPs they’re sending webhook from.

Here the latest values:

149.154.160.0/20
91.108.4.0/22

Retrieve updates

Retrieve bot updates navigating to https://api.telegram.org/bot<here the bot token>/getUpdates.

You should see a response like this:

{"ok":true,"result":[{"update_id":1234567890,
"message":{"message_id":10,"from":{"id":1234567890,"is_bot":false,"first_name":"Fabio","username":"daaru","language_code":"it"},"chat":{"id":1234567890,"first_name":"Fabio","username":"daaru","type":"private"},"date":1234567890,"text":"test"}}]}

The chat.id is the value to use in TelegramChatsWhitelist parameter.

Set Webhook

Set the WebHook URL endpoint (retrieved from WebhookEndpoint stack output after the first deploy) navigating to: https://api.telegram.org/bot<here the bot token>/setWebhook?url=<here the webhook endpoint url>.

You should see a response like this:

{"ok":true,"result":true,"description":"Webhook was set"}

You can also double check the webhook configuration navigating to https://api.telegram.org/bot<here the bot token>/getWebhookInfo, You should see a response like this:

{"ok":true,"result":{"url":"https://xxxxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/webhook/","has_custom_certificate":false,"pending_update_count":0,"max_connections":40,"ip_address":"0.0.0.0"}}

Receive a message

When a message is sent to the bot (and the username whitelist pass) this application send an event to the exported Event Bridge bus with the following format:

{
  "source": "my-stack-name",
  "detail": "Message Received",
  "details-type": { /* Update object */ }
}

Event's details as the same format as Update object.

Send a message

In order to send a message through the Telegram bot send an event to the exported Event Bridge with the following format:

{
  "detail": "Send Message",
  "details-type": { /* sendMessage payload */ }
}

Event's details need to have the same format as sendMessage API payload:

Credits

About

AWS Serverless Telegram Bot Connector for message receive and send.

License:MIT License