DEV Community

Rajitha Gunathilake
Rajitha Gunathilake

Posted on • Updated on

Sending messages with Telegram bot

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!

Latest comments (45)

Collapse
 
vschary profile image
Santosh

Thanks for the step by step instructions.

Collapse
 
emmykolic profile image
Emmanuel C. Okolie

You're welcome

Collapse
 
antonov_mike profile image
Antonov Mike

Hi, thank you for this tutorial. Do you know how to make bot sending message any time user joins chat? Text message or button or kinda menu

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

i think that is possible using telegram bots but not sure .

stackoverflow.com/questions/383244...

Collapse
 
antonov_mike profile image
Antonov Mike

Looks like getUpdates and update.message.new_chat_members works only for group or supergroup not for private chats. But thank you anyway

Collapse
 
singhanupam04 profile image
SinghAnupam04

Thank you so much brother for this amazing blog, I tried everything and it went perfectly. I'm able to send messages in my group through bot.

But I have a question...
Can I send a reply to a message in my group through bot using this method....????

Collapse
 
encryptblockr profile image
encryptblockr

next time use real token and just delete; common sense man. first it is demo account yet you still can't add to tutorial? second you can always delete the token after done with tutorial. why is this difficult for people to grasp? i see everyone keep doing this like common sense is becoming rare?

Collapse
 
arsaaron profile image
arsaaron

i have error 400 Bad Request: chat not found

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

as the error suggest , check whether you are using the correct chat id
and also make sure - before the chat id .

Collapse
 
arsaaron profile image
arsaaron • Edited

i have error : Bad Request: chat not found

Collapse
 
smsdavis profile image
smsdavis

Ok. I followed all these steps and I cannot make this work. Maybe I don't understand. Please help.

I want to create a simple bot where I can send messages to a specific Telegram group (my family) and do so from a URL (so I can do it from within another app).

I created the bot, got the token, found the chat ID of the group I want the posts to go into and put in all the information and I get an error not found.

Your instructions say to create a bot THEN create a group. I already have a group chat. I added the bot into that group chat. But that bot has nothing else. It was just created and has a name. Am I missing something here? How do you get the new bot to be able to post messages into the group? I cannot create a new group as the group already exists.

Please explain. Thank you.

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

Existing group is fine you don't have to create a new group .
can you post the error you got.

Collapse
 
stevehan profile image
stevehan12

hi,I wanna know how about this situation:we input "api.telegram.org/bot/sendmessage?chat_id=&text=test1" in broswer,I know the chat_id will get the content"test1",but how can I get the content "test1" by another broswer url code link or php/java code.

Collapse
 
prasaat37234827 profile image
prasaath

By using php I wrote the code
Please review the link below
knowsomething.online/how-to-send-a...

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

hi prasaath ,
i don't have expertise in php to review this kind of an article😶.

Collapse
 
duvvurisandeep profile image
Duvvuri Sandeep

Telegram bot (my_bot messages not visible to others)

Sir, I have created Telegram bot(my_bot). I am getting project notifications in my mobile. When i asked other employee to install Telegram and add my_bot. He didn't get any notifications similar as mine. Its blank whereas I get project notifications.
Even I added the bot in new group and also added employee.
Still the same problem. Can you please find out where I have done mistake.

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake • Edited

hi Duvvuri Sandeep ,
this is quiet uncommon , i use this method recently and works fine.
can you make sure bot permissions are correctly set(admin permissions) .

and also make sure the group id (starts with "-" ) is also correct.

Collapse
 
superails profile image
Yaroslav Shmarov

Good intro article.
However:

  • You don't have to sign up to any service like Postman in to send API requests. You can do it directly in the browser
  • If you subscribed a bot to a group and can't get the bot ID, unsubscribe and subscribe again to make it work
Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

hi thanks for your feedback .

Collapse
 
yusufsonmez profile image
yusufsonmez

Thanks for the tutorial

Collapse
 
guillelerial profile image
Guillelerial

Great tutorial. I was able to reproduce it for both a group and a channel following the same steps. However I did find it compulsory to have sent at least 1 message to the said group or channel in order to receive the bot's update message with the ID of the group/channel

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

Thanks 🙌.
yeah when the group is empty we just get an empty array

Collapse
 
futuregadget profile image
danu

Thanks for the tutorial! It's very easy to follow.

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

Glad to hear that 👍

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

hi kishag212

i not clear , can you please explain it further .