DEV Community

Sending messages with Telegram bot

Rajitha Gunathilake on November 10, 2019

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 c...
Collapse
 
karataev profile image
Eugene Karataev • Edited

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

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
Rajitha Gunathilake

thanks 👍

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

That's interesting .
Thanks for sharing 👍

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

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
 
rizkyrajitha profile image
Rajitha Gunathilake

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
 
artashesqwerty profile image
Garegin

That was very useful for me. Thanks, Rajitha!

Collapse
 
rizkyrajitha profile image
Rajitha Gunathilake

🙌

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

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

view this doc

u can use html markdown and other formatting options

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
 
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
 
shoxruxmirzo profile image
shoxruxmirzo

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

Collapse
 
merodriguez9112 profile image
Manuel E • Edited

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

sounds cool 👌👌

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

Collapse
 
yusufsonmez profile image
yusufsonmez

Thanks for the tutorial

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
 
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
 
vschary profile image
Santosh

Thanks for the step by step instructions.

Collapse
 
emmykolic profile image
Emmanuel C. Okolie

You're welcome