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.
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
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 .