Create Telegram chatbot in API.chat

Connecting Telegram chatbot to API.chat consists of two steps.

1. Create a Telegram chatbot and obtain a token

There are Telegram bots and there are API.chat bots. Telegram bot is third-party applications that run inside Telegram, and API.chat bot is a scenario that moves it. For simplicity, we will name Telegram bots (and any other messengers bots) as Channels everywhere outside this section. Please don't mistake this with the actual Telegram channel.

We support a lot of channels, but for a quick start let's start with the most advanced and easiest to use - Telegram.

To create a Telegram bot use @BotFather bot. It will help you create new bots and change settings for existing ones.

  1. Use the /newbot command to create a new bot. The BotFather will ask you for a name and username, then generate an authorization token for your new bot.
  2. The name of your bot is displayed in contact details and elsewhere.
  3. The Username is a short name, to be used in mentions and t.me links. Usernames are 5-32 characters long and are case insensitive, but may only include Latin characters, numbers, and underscores. Your bot's username must end in 'bot', e.g. 'tetris_bot' or 'TetrisBot'.
  4. The token is a string like 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw which is required to send requests to the Bot API. You will use this token later to connect the created channel to API.chat chatbot.
quickstart_botfather.jpg

2. Provide the token to the Telegram Channel API POST method

Use channels APIs to connect Telegram channel to API.chat chatbot.

HTTP Request must be as follow

-- CODE lang-bash -- curl -v -X POST "https://bot.api.chat/channels/telegram/botName" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Ocp-Apim-Subscription-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" --data-raw '{ "Token": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw" }'

If the token applied successfully you will receive 201 Created answers with an added Telegram chatbot name

-- CODE lang-http -- HTTP/1.1 201 Created cache-control: no-cache content-length: 124 content-type: application/json; charset=utf-8 { "Token": "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw", "BotName": "name_of_telegram_bot" }

If the token already assigned to another chatbot under the current subscription, it will be removed and set for the current chatbot. If token assigned to a chatbot in another subscription 409 Conflict response will be returned.

3. Removing Telegram token

If you need to connect Telegram chatbot to another API.chat bot use DELETE request on channels endpoint.

-- CODE lang-bash -- curl -v -X DELETE "https://bot.api.chat/channels/telegram/botName" -H "Cache-Control: no-cache" -H "Ocp-Apim-Subscription-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

Successful delete will return you 200 OK

Don't miss these stories: