loading...

Sending messages with Telegram bot

rizkyrajitha profile image Rajitha Gunathilake ・3 min read

Hi everyone,

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.

One Bot To Rule Them All

so go to telegram app on your phone, or log in with telegram web

and then search for bot father.
create a new bot /newbot

create new bot

now follow along with instructions.

type a name for your bot
I had a hard time finding a username 😆

Alt Text

now you'll get a token to authenticate with your bot.
Alt Text

keep the access token securely. Anyone with access token can manipulate your bot

alright, now we have the access token. The next question is how to send messages with the bot.

we will create a group with our bot. And then our bot will massage to that group

it's simple as that😎

now let's create a new group in telegram and add the bot to that group.

search with the username of your bot

Alt Text

add our bot to the group

Alt Text

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

Alt Text

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.

i hope yall have postman📬 ready. if not you can download postman , since it is a useful tool in web dev.
or alternatively, you can use postwoman which is a great way to build requests online.

before we sending requests, make sure to send some messages to our newly created group.

after that let's fire up postman

send a POST request to the following telegram API endpoint.

replace with your access token we previously got from bot father

https://api.telegram.org/bot<token>/getUpdates

and Send.

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)

Alt Text

hopefully, you'll receive something similar to this.

Alt Text

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

Alt Text

and wubba lubba dub dub your message is sent

Alt Text

This is a great way to monitor and notify your apps state. I use this for the backend of my web apps to report errors. you can also use it with frontends, CLI applications regardless of the language. you can use this creatively to do many things because this needs no 3rd party libraries or dependencies, just a simple HTTP request will do the work.

Thanks for following along with me 🙌

what are your plans ? 💭

share them in the comments section.

I'd love to hear your feedback!

Discussion

pic
Editor guide
Collapse
karataev profile image
Eugene Karataev

Thanks for the tutorial.
In my case for some reason POST and GET requests to https://api.telegram.org/bot<token>/getUpdates in Postman failed, but GET request in the browser to this endpoint was successfull.

Collapse
tech2blog profile image
Tech2Blog.com

Try to add the bot to the group and then try the command.

Collapse
karataev profile image
Eugene Karataev

The bot was in the group, but the request in Postman failed 🤷‍♀️

Thread Thread
tech2blog profile image
Tech2Blog.com

Strange, mine was also failing, but after adding to the group issue was resolved. Just check if the bot has admin privilege.

Also, try with curl once.

Thread Thread
karataev profile image
Eugene Karataev

Oh, I think I figured out the difference in results between Postman and browser requests.
Telegram servers are blocked here in Russia, that's why Postman requests fail. But in the browser I use VPN extension and the request resolves as expected.
Thanks for your help 😉

Thread Thread
rizkyrajitha profile image
Rajitha Gunathilake Author

wow , that's some serious debugging.
for me the results came after i added the bot to the group and then i send some messages to the group. so there is some data to show.

Thread Thread
karataev profile image
Eugene Karataev

Yeah, but in normal situation even if you don't add the bot to the group and send request https://api.telegram.org/bot<token>/getUpdates, you'll get an emtpy response like {"ok":true,"result":[]}.
In the case of access restriction on a government level, there is no response in Postman at all.
no response

Thread Thread
rizkyrajitha profile image
Collapse
rizkyrajitha profile image
Rajitha Gunathilake Author

That's interesting .
Thanks for sharing 👍

Collapse
innocuousgee profile image
Creamy Uprising!

Hi can i send a message to an individual rather than a group please

Collapse
rizkyrajitha profile image
Rajitha Gunathilake Author

hi ,
most of articles say it is not possible but i don't know it for sure.
if i found something i'll let you know.

Collapse
rizkyrajitha profile image
Rajitha Gunathilake Author

this might help you

Send a message to your bot then retrieve the update with:

https://api.telegram.org/bot<BOTID>/getUpdates

In the json string returned you will find you chat_id/user_id.
In single chat with the bot chat_id is equal to user_id.
Now you can send your message with:

https://api.telegram.org/bot<BOTID>/sendMessage?chat_id="yourchatid"&text="Hello!"

stackoverflow.com/a/32777943/9374003

Collapse
artashesqwerty profile image
Garegin

That was very useful for me. Thanks, Rajitha!

Collapse
rizkyrajitha profile image
Collapse
kishag212 profile image
kishag212

I want to send a message to a group as soon as the chat opens

Collapse
rizkyrajitha profile image
Rajitha Gunathilake Author

hi kishag212

i not clear , can you please explain it further .

Collapse
shoxruxmirzo profile image
shoxruxmirzo

Using Telegram bot framework can be good idea, since you should not implement classes or more scripts.

Collapse
glitchx profile image
glitchX

How can Bot send rich messages like the text in bold or italic or some sort of option buttons

Collapse
rizkyrajitha profile image
Rajitha Gunathilake Author

hi
core.telegram.org/bots/api#sendmes...

view this doc

u can use html markdown and other formatting options

Collapse
merodriguez9112 profile image
Manuel E

Thanks for the tutorial very helpfully. I'm working in a bot to make a web crawler and send notifications to a community. Regards from Cuba.

Collapse
rizkyrajitha profile image
Rajitha Gunathilake Author

sounds cool 👌👌