Obtaining the Telegram bot token & Chat ID for building communication programs

"Only a life lived for others is a life worthwhile." – Albert Einstein

Obtaining the Telegram bot token & Chat ID for building communication programs

Chatbots are one of the iconic platforms that uniquely highlight this era of internet communication automation. Chatbots can be used to build engaging conversations between a real person and a software system. The possibilities of building automation of different scales using a chatbot interfacing system are unlimited.

Telegram is one of the most popular free communication platforms today for text/audio/video. What is even more promising is that telegram offers a simplified approach to chatbot creation and usage through its extensive API as opposed to other similar products in the market. While other products may also be free to use, their API usage for building automation is generally not free. But with telegram both the system and its API are free.

So let us quickly see how to use telegram & its API to deliver automated messages to your PC/Smartphone for free.

CREATE A BOT AND OBTAIN THE TOKEN


To create a chatbot on Telegram, you need to talk to the BotFather, which is a special telegram system bot used to create bots for users.

  • Go to the search tab and search for the bot called @Botfather.
  • Once you see Botfather in the search results select it & Click “Start” to activate it.
  • In response, BotFather will share a list of commands to manage bots.
  • Type /newbot command to BotFather. This will indicate a request to create a new bot.
  • Botfather will then initiate a new bot creation and ask you for the different parameters needed for the bot. –  
    • Choose a name for your bot – The bot name for conversations
    • Choose a username for your bot — Username/ID is the4 unique identification of the bot. . Username must be unique and end with the word bot



  • Once the bot is created, BotFather will share the bot’s name and the API access token for it. You must note down the bot’s name, username & token in a safe place. This will be required when setting up telegram-based notifications in apps.


OBTAINING A CHAT ID FOR COMMUNICATING WITH THE BOT


So if you have created a bot successfully, you would be thinking about how would we use it. when using telegram, people & bots usually communicate in groups, channels, or private messages (DM). Groups/channels are useful when we need to deliver messages to & from multiple users. Similarly private messaging is useful when you want certain messages delivered to just one user.

In this example, we will see how to get a channel/group’s CHAT ID or a User’s CHAT ID (in private messaging) from the bot’s [perspective. Once we know this, we can programmatically send messages to the channel/group or to the user using the bot’s token & the Telegram API.

  • Go to search and look for the bot you just created by using @{BOTNAME}
  • Once located select the bot and click “Start” to initiate a conversation with the bot
  • Send a message to the bot such as “hello” or “hi”
  • Visit the following API endpoint using your browser:
     https://api.telegram.org/botYOUR_TOKEN/getUpdates
    Where YOUR_TOKEN is your bot token that BotFather gave us on bot creation.
  • This will give you a JSON that represents the conversations between the bot and others. In the JSON you can easily identify your message (Hello or Hi) to the bot. Against the message, you will find the CHAT ID that identifies the conversation session. Not it down for future correspondence. You can use a similar technique to get the CHAT ID of a channel or group as well.

    JSON DATA SAMPLE
{"ok":true,"result":[{"update_id":633239032,
"message":{"message_id":7822,"from":{"id":494506384,"is_bot":false,"first_name":"xxxxxxx","last_name":"xxxxx","username":"xxxxxxx","language_code":"en"},"chat":{"id":494506384,"first_name":"Rajdeep","last_name":"xxxx","username":"xxxxxxx","type":"private"},"date":1662642433,"text":"hello"}}]}

That’s all!! Make sure to carefully store the information we obtained here. Further along the way, we will be using the bot TOKEN & the CHAT ID obtained to communicate with the bot and get messages from the bot to our PC/Phone running telegram.