Telegram platform#

The Telegram Platform allows a bot to communicate with the users using Telegram.

Telegram is a great platform for chatbots. You can create a Telegram bot through the Bot Father and then, link it to our BBF chatbot. This way, you can define your chatbot architecture and logics, and then use your Telegram bot as the communication channel.

Our Telegram Platform uses the python-telegram-bot library, which is a Telegram Bot API wrapper for Python.

Telegram demo

Example chatbot using Telegram#

Note

There are some properties the bot needs in order to properly set the Telegram connection. More details in the configuration properties documentation.

How to use it#

After you instantiate your bot, simply call the following function:

bot = Bot('example_bot')
...
telegram_platform = bot.use_telegram_platform()

After that, you can use the platform to send different kinds of messages to the user (from state bodies):

  • Text messages (strings):

telegram_platform.reply(session, 'Hello!')

Note

The bot cannot detect when a user opens the Telegram chat window. Therefore, to start the conversation, it is needed a first message to “wake the bot up”. After that, it will start running the initial state.

⏳ We are working on other replies (files, media, charts…). They will be available soon, stay tuned!

Handlers#

The python-telegram-bot library (Telegram Bot API Python wrapper under our Telegram Platform) uses handlers to handle bot updates (e.g., the user sends a text message, an image, a command, etc.). Our Telegram Platform interface allows you to add custom handlers to your bot, so this feature is not lost when using BBF.

This is an example handler function that will be executed when bot receives the /help command:

from telegram import Update
from telegram.ext import CommandHandler, ContextTypes
...
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
    session_id = str(update.effective_chat.id)
    session = bot.get_session(session_id)
    session.reply('Please introduce a number between 1 and 10')

help_handler = CommandHandler('help', help)

telegram_platform.add_handler(help_handler)

Our Telegram Platform has 2 built in handlers.

  • One to handle all user text messages (that simply captures the messages and sends them to the bot).

  • A reset handler that resets the bot when the user writes the command /reset.

API References#