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.
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
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.
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 .
Good step-by-step tutorial. Many thanks.
However, you forgot to explain what is a chat and chat_id. How do I create one?
Thank you.
Your process og guide and video, really saves my time a lot.
Keep posting…
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 ?
Use “%20” instead of “+” for space. For example:
……text=Hello%20World
Can we send message on mobile number instead of chat id?
change “;” in amp;text to amp&text
Hello I get an error when trying this as well..{“ok”:false,”error_code”:401,”description”:”Unauthorized”}
Corrected above, thanks!
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
Corrected above, thanks!
Not working for me :(
I get the below:
{“ok”:false,”error_code”:400,”description”:”Bad Request: chat not found”}
any solution for above,m getting same error
How can i post photos with caption ?
Not working : Shows – {“ok”:false,”error_code”:400,”description”:”Bad Request: chat not found”}
Thanks!
Can i also sent to multiple groups at once?
Thanks for the article.
How to send file attachments and images?
How can i send local storage photo ?
Getting this error:
{“ok”:false,”error_code”:400,”description”:”Bad Request: chat not found”}
Also is there development for posting to channels?
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
can we make trigger like when someone uploading file or omage in synology than that image uploaded in teleram group??
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.
1) go to this bot https://t.me/chatIDrobot
2) copy your chat_id
3) https://api.telegram.org/bot$token/sendMessage?chat_id=your_chat_id_telegram$chat&text=Hello+World