avatarGiorgos Myrianthous

Summary

This article describes the process of generating a token for the Telegram Bot API using the BotFather account.

Abstract

The article begins by providing background information on Telegram, a popular instant messaging platform, and its two APIs for developers. The focus of the article is on the Bot API, which allows developers to create and connect bots to the messaging platform. The article explains that in order to create a Telegram bot and access the HTTP API, a token must be generated. The article then outlines the steps for generating a token using the BotFather account, which include finding the BotFather account, creating a new bot, and testing the token. The article also includes screenshots and code examples to illustrate the process. The article concludes by offering additional resources for readers interested in learning more about infrastructure as code and SQL joins.

Opinions

  • The article emphasizes the importance of storing the bot token in a secure place and only sharing it with people who need direct access to the bot.
  • The article suggests that creating Telegram bots is straightforward but requires registering the bot and generating a token.
  • The article advises that the username chosen for the bot must be unique and end in "bot".
  • The article recommends testing the token by sending a message in the chat with the newly created bot and running a curl command in the terminal.
  • The article suggests that readers can find additional resources on the author's Medium page and through the related articles listed at the end of the article.

Programming

How to Generate a Token for Telegram Bot API

Generate tokens to build Telegram bots using the Bot API

Photo by Rubaitul Azad on Unsplash

Telegram is a cross-platform, encrypted instant messaging platform that has seen its user-base grow significantly over the last few years. The application offers two APIs for developers; the Bot API that lets you create anc connect bots to the messaging platform and the Telegram API and TDLib that allows developers build customised Telegram clients.

Creating Telegram bots is straight-forward however, you will need to register your bot and generate a token that you can then use when performing HTTP requests to teh Telegram Bot API endpoint.

Your bot token is its unique identifier — store it in a secure place, and only share it with people who need direct access to the bot. Everyone who has your token will have full control over your bot.

Telegram Documentation

In this article, we will demonstrate how you can quickly generate a token before start the process of creating your own Telegram bot.

Step 1: Find BotFather account

Open Telegram app (either on Desktop or mobile application) and search for the BodFather account — just make sure you click on the verified account.

The BodFather Telegram bot account — Source: Author

You can see all the options available that this particular bot offers by typing /start:

Options available by BotFather bot — Source: Author

Step 2: Create a new bot and generate token

Now in order to generate a token, we first need to create a new bot. To do so, type /newbot in the chat and press enter to send the message.

You will then be prompted to name your newly created bot.

Then you will be prompted to pick a username of the bot that must be unique and also end in bot.

If you’ve provided a valid username, BodFather will reply with the generated token that you can now use to access the HTTP API.

Step 3: Test that token works

In the chat with the newly created bot, send a message. Then head to the terminal and run the following curl command:

curl https://api.telegram.org/bot<your-bot-token>/getUpdates

Then look fot the chat object, and copy the id field that we will then use as the chat_id for the subsequent call:

curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{"chat_id": "<your-chat-id>", "text": "This is a test from curl", "disable_notification": true}' \
     https://api.telegram.org/bot<your-bot-token>/sendMessage

If the curl command was executed successfully, you should now see the message we’ve sent in the chat.

Final Thoughts

In this tutorial we demonstrated how to create a new Telegram bot and generate a token using the BotFather Telegram bot. Additionally, we showcased how to test the token and get started by sending a message.

Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

Related articles you may also like

Programming
Technology
Telegram
API
Software Development
Recommended from ReadMedium