Creating a Telegram Bot

3 minute read

Setting up a Telegram bot is easy but involves a few steps.

Create the bot

This is done via the Telegram app, so we need to open that up (if you don’t have it installed yet you’ll need to do that first).

  • Use search (magnifying glass icon) to find the bot called ‘BotFather’. This is an official Telegram bot, used for setting up new bots. Tap it to start a chat with it.
  • In the chat with ‘BotFather’, use the /newbot command, this is just a matter of typing the command as a message just like you’d send a message to a person
    The slash is part of the command so make sure you include it
  • Enter a name for your bot, this is the name which will be displayed in chats
  • Enter a username for the bot, this is for Telegram’s use and must end in ‘_bot’ (the prompts will tell you this)

This creates the bot, then you will receive a message with some info about your new bot. The main thing you need here is the new bot’s token for accessing the HTTP API, this will be a long string something like 7901112608:AAD9fDpa9kgw3lZ10o6qANkXnB_D2ZvwuPc

This is your BOT TOKEN, keep a note of it as you need it to do stuff with your bot

Check that everything is working so far…

In a browser, visit a special URL including your bot token (hitting it in a browser like this is one simple way of accessing the API).

  • The format is https://api.telegram.org/bot<BOT_TOKEN>/getMe
  • So using the example ID above https://api.telegram.org/bot7901112608:AAD9fDpa9kgw3lZ10o6qANkXnB_D2ZvwuPc/getMe
    Make sure the bot part is there just before the token as it’s an essential part of the URL
  • You should see a page in JSON format with some basic data about your new bot

Create a channel

To interact with the bot (eg to receive messages from it), you need to create a channel.

  • In Telegram, tap the pencil icon
  • The ‘New Message’ dialog will appear, tap ‘New Channel’
  • Give the channel a name (and description if you wish)
  • Choose whether it should be public or private
  • Choose subscribers — here you need to add your new bot. If you can’t see it in the list, tap the blank area at the top to search and type part of its name, it should appear.
    The list of subscribers can be edited at any time by tapping its icon in Telegram

Now you can use the API again to get the ID for the channel (or ‘chat’ as it is referred to). The URL is similar to the one you used above to get the token ID, but the last part changes from getMe to getUpdates, so https://api.telegram.org/bot<BOT_TOKEN>/getUpdates.

Like before, you will see some JSON data. You are looking for a channel_post.chat.id, something like -1002179830041 the dash is part of the ID.

This is your CHAT ID, keep a note of this too as it will allow your bot to send messages to this chat/channel

Test your bot

Now you have what you need to send automated messages via your bot. It’s as simple as:

# Send a message from a Telegram bot to a specific chat/channel
curl 'https://api.telegram.org/bot<BOT_TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=<YOUR_MESSAGE>'