Skip to main content

Configuring a Telegram Bot

This guide explains how to setup and configure a Telegram bot for the Bharat SahAIyak platform.

Getting Credentials

  • A Telegram Bot requires a bot token as a secret credential.

  • To get a bot token, first step is to create a bot using @BotFather on telegram. Open @BotFather on your telegram and send a /start message. The bot would provide you with a lot of options for managing different bots.

  • Send /newbot message. After that you will be prompted to enter the name as well as a username for your bot, but the username must end with a bot. This can be as per your choice.

  • Once you are done, you will be presented with an API token for the bot. Store this token safely since we'll use this to configure the bot. You can always use /mybots command to fetch this token again.

  • Once you have the token, create a secret with this token, with the key botToken. See Setting Secret Credentials for more details.

Here is a sample curl:

curl --location 'UCI_API_BASE_URL/admin/secret' \
--header 'Authorization: YOUR_JWT_TOKEN' \
--header 'Content-Type: application/json' \
--data '{
"variableName": "YOUR_VARIABLE_NAME",
"secretBody": {
"botToken": "YOUR_API_TOKEN"
}
}'

Setting up a Bot

Prerequisite

This section assumes that you have already created a bot instance and have a bot id to work with. See Creating a Bot for more details.

Setting webhook

A webhook redirects the messages sent to your bot to the Bharat SahAIyak system. Here is a simple curl request which sets the webhook for your bot.

curl --location --request POST 'https://api.telegram.org/bot{YOUR_API_TOKEN}/setWebhook?url={INBOUND_BASE_URL}/inbound/bot/{YOUR_BOT_ID}'

In the above curl request:

  • {YOUR_API_TOKEN} is the token that you generated in Getting Credentials.

  • {INBOUND_BASE_URL} is the base url of the deployed inbound service.

  • {YOUR_BOT_ID} is the id that you have generated as a result of Creating a Bot.

That's it, you're good to go now. You can start conversing with your bot right away.