How to Create a Telegram Bot and Send Messages via API

Telegram bot Overview

Telegram fully supports the use of bots via the Telegram bot API. Bots can do anything – and are really only limited to what your imagination can come up with. You can use them to search, send reminders, or integrate with other services. There are many applications out there now that support the use of Telegram bots. You can integrate your bot with Plex and get notifications when family members are playing content, or when Plex is down. If you have a Synology, or other NAS device, you can also adjust settings to send alerts and messages via the Telegram bot API. You could even code your own solution to leverage your bot, or use something like PoshGram to integrate with your current workflow.

Video Walk-through

If you prefer video format over written documentation I demo how to create a Telegram bot with the BotFather and use the new bot to send messages:

Steps for creating a Telegram bot

1. Find, and start a conversation with the BotFather

If you’ve never had a conversation with @BotFather before, just bring up your Contacts and search for him. Then add him as a contact and start a conversation with him.

Finding the Telegram BotFather in Contacts

2. Use newbot to get the BotFather to create your bot

It only takes one command to get the BotFather to create a new bot: /newbot

Once that’s entered you’ll just need to provide the name of your new bot and the username of the new bot, which must end in bot. What I typically do is keep those similar like this:

  • Bot name: techthoughts
  • Bot user name: techthoughts_bot
/newbot
nameOfYourBot
userNameOfYour_Bot
Creating a new bot via the Telegram BotFather

3. Copy and keep the bot token

The BotFather will provide you with your new bot’s access token. Copy this and store it in a secure place. You’ll use this token whenever you want to send messages via your new bot.

Using your new telegram bot

In order to use your new bot to send message you’ll need to add your bot as a group member to the desired Telegram chat. Once that’s completed, you’ll need to reference the chat ID number. There are many ways to find the chat ID, but I’ve found the easiest is to be logging into the Telegram Web client and clicking on the desired chat group. From there, you can reference the chat ID number in the url of your browser.

Finding Telegram chat ID via Telegram web client

Don’t forget to add a ‘-‘ in front of your channel ID! Ex: 263767625 will be: -263767625

Armed with the token and the chat ID, you can engage the Telegram API to send messages with your new bot!

#you place the bot token and chat id in the appropriate location of the telegram API address
$token = "604891196:AAEtNizH6-nr3tOINAIhzmWy8VJjSKhjQK4"
$chat = "-263767625"
https://api.telegram.org/bot$token/sendMessage?chat_id=$chat&text=Hello+World

#here is a full example:
https://api.telegram.org/bot604891196:AAEtNizH6-nr3tOINAIhzmWy8VJjSKhjQK4/sendMessage?chat_id=-263767625&text=Hello+World

Additional Information

Here are a few links for more information regarding Telegram bots:

If you’re using PowerShell and want to add Telegram messaging capabilities check out PoshGram .

23 Responses

  1. Paul Moore says:

    Good step-by-step tutorial. Many thanks.
    However, you forgot to explain what is a chat and chat_id. How do I create one?

  2. jitendra says:

    Thank you.

    Your process og guide and video, really saves my time a lot.
    Keep posting…

  3. Anders says:

    Hi,

    I get this error when trying to send from browser:

    {“ok”:false,”error_code”:400,”description”:”Bad Request: message text is empty”}

    I have copy your’s and replace token + id with my data.

    Any ideas ?

  4. dastan says:

    change “;” in amp;text to amp&text

  5. Dermot Jones says:

    Hello I get an error when trying this as well..{“ok”:false,”error_code”:401,”description”:”Unauthorized”}

  6. Arjun says:

    For anyone getting a “Bad Request: message text is empty” error, do as follows:
    Remove the amp; in ?chat_id=-263767625&text=Hello+World

    It should be ?chat_id=-263767625&text=Hello+World

  7. Daniel says:

    Not working for me :(

    I get the below:
    {“ok”:false,”error_code”:400,”description”:”Bad Request: chat not found”}

  8. dori says:

    How can i post photos with caption ?

  9. mak says:

    Not working : Shows – {“ok”:false,”error_code”:400,”description”:”Bad Request: chat not found”}

  10. G says:

    Thanks!
    Can i also sent to multiple groups at once?

  11. Sahil says:

    Thanks for the article.

  12. Srithika says:

    How to send file attachments and images?

  13. Pui Wee Yaw says:

    How can i send local storage photo ?

  14. ian says:

    Getting this error:

    {“ok”:false,”error_code”:400,”description”:”Bad Request: chat not found”}

    Also is there development for posting to channels?

  15. ringleader says:

    hello great info thank you so much. How do you connect the telegram to a webhook like from google. I am trying to send my alerts from tradingview to telegram

  16. ego says:

    can we make trigger like when someone uploading file or omage in synology than that image uploaded in teleram group??

  17. Jazz says:

    Really well explained, I really like your writing skills it’s awesome. A non technical guy can easily understand these technical vocabulary. Thank you so much.

Leave a Reply

Your email address will not be published. Required fields are marked *