Posted 2 years ago
In this tutorial, we’re going to build a telegram bot ,and send messages to a group with a simple HTTP request.
to get start lets create a new telegram bot with, it’s own API Botfather.
and then search for bot father.
create a new bot
type a name for your bot I had a hard time finding a username 😆
alright, now we have the access token. The next question is how to send messages with the bot.
now let’s create a new group in telegram and add the bot to that group.
after adding give admin permission to our bot, to give access to the bot to send messages to the group (not all the permissions are necessary but I will give because this is a demo group and nothing harmful will happen ).
now everything is set in this end.
The next thing we need is to get our group chat id to send messages to the group.
to get it we need to interact with the telegram API.
before we sending requests, make sure to send some messages to our newly created group.
after that let’s fire up postman
POST request to the following telegram API endpoint.
if you get something like this, try few more times. I don’t know the exact cause, but it sometimes returns empty arrays.(make sure you have some messages in the group)
hopefully, you’ll receive something similar to this.
from here what we want is the chat id that starts with
this is the group chat id of our group.
after that, the only thing left is to send messages.
so to send messages to our group.
use the following API endpoint
https://api.telegram.org/bot<token>/sendMessage?chat_id=<group chat id >&text=<our text>
keep in mind that text should be URL Encoding String
ex - Hello%20World (spaces is replaced with %20)
checkout w3school for more info
wubba lubba dub dub your message is sent