How to send request to Telegram bot API?

51,650

Solution 1

You just send a POST request to:

https://api.telegram.org/bot{token}/{method}

For example:

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage

In the body of the request, you URL encode the parameters:

chat_id=12345&text=hello%20friend

For example, in Python using the requests module:

import requests
    
response = requests.post(
        url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
        data={'chat_id': 12345, 'text': 'hello friend'}
    ).json()

When a user chats with your bot, you get a Message object that has a chat id (and a user id, which you can substitute for a chat id). There's no way to initiate a chat with a user unless you already know their user id, so you have to wait for a user to talk to you. You can simplify that by using deep linking and having the user click on a link that sends a pre-made message when they hit the Start button.

Edit: for those struggling to find chat_id, here's a way:

1.- Create a bot: on Telegram's search look for @BotFather. Click start, write /newbot, give it a name and a username. You should get a token to access the HTTP API. Save this token.

2.- Find your bot on Telegram with its username. Write something to it e.g. 'test'. This will come in handy later.

3.- Print chat_id. Before running this function, make sure that you have at least written one message to your bot on Telegram (step 2)

Javascript code:

var token = "123456:kioASDdjicOljd_ijsdf"; // Fill this in with your token
var telegramUrl = "https://api.telegram.org/bot" + token;

function getChat_id(){
    var res = UrlFetchApp.fetch(telegramUrl+"/getUpdates").getContentText();
    var res = JSON.parse(res);
    Logger.log(res.result[0].message.chat.id.toString());
}

Solution 2

Try this

https://api.telegram.org/bot{token}/sendMessage?chat_id=<chat_id>&text=<Enter your text here>

Example

https://api.telegram.org/bot449123456:AAHSAnSGDm8PW2Z-1ZiwdVDmgv7sM3NMTxg/sendMessage?chat_id=311911234&text=Hi+Everyone
Share:
51,650
Ali Crash
Author by

Ali Crash

Updated on July 09, 2022

Comments

  • Ali Crash
    Ali Crash almost 2 years

    After creating a telegram bot and gain bot token, I want to send a request to the bot API.

    This link says we must send the HTTP request like this: https://api.telegram.org/bot<token>/METHOD_NAME and brings example for easiest method "getme" which has not any input parameters.

    Imagine I want to send some messages. I should use the sendMessage method which has two Required input parameters: chat_ID and text.

    Now my Questions begins:

    1. How can I write this sendMessage method in above request format with its parameters? I tried sendMessage(param1,param2) and received method not found message.

    2. What is chat_id? if I want to send a message to the contact, how can I know his chat_id?

    I searched a lot on the internet, there are plenty of projects on GitHub especially for this purpose, and honestly none of them makes any sense. for god's sake someone please help me. I am loosing way.

    Regards.

  • Ali Crash
    Ali Crash almost 9 years
    thanks for your good answer, about receiving messages, how can a user chat with my bot, I mean should they just send message to my telegram account and I can have their chat_id ?
  • Blender
    Blender almost 9 years
    @AliCrash: The user either finds your bot by name (@YourBot) or they click on a link (https://telegram.me/YourBot?start=something), which opens up a Telegram window and the message input box is replaced with a giant Start button which sends \start something to your bot.
  • Ali Crash
    Ali Crash almost 9 years
    aha, now I see how a bot works, thanks again, but how can I receive and store the messages in my Telegram account inbox into a local database? I want to make a desktop app to do such a thing.
  • Ali Crash
    Ali Crash almost 9 years
    and also in general about telegram api methods, do we have to write methods name in body of http request and send it tu DC ip of our api app? can you bring me an example of one api method in http request? let's say auth.checkPhone for example do we use get request or post . regards
  • Blender
    Blender almost 9 years
    @AliCrash: The Telegram API is not related to the Telegram Bot API. It's not even over HTTP, it's a custom protocol.
  • Shafizadeh
    Shafizadeh over 8 years
    Actually I have not any clue for starting ...! My dynamic language is PHP. Is it possible to create a Telegram Bot using PHP? and should I create a script containing a URL and upload it on my server? or what? tnx
  • Incerteza
    Incerteza over 8 years
    where does the value of "chat_id" come from?
  • Blender
    Blender over 8 years
    @アレックス: A user has to initiate a chat with you. Or if you know their user id, you can use that.
  • Incerteza
    Incerteza over 8 years
    use their user id how? instead of chat id? so I can do this 'chat_id': "@some_user_id"?
  • Blender
    Blender over 8 years
    @アレックス: Not the username, the user id. It's a number like the chat id.
  • Incerteza
    Incerteza over 8 years
    but it doesn't contain it, it contains a user name, chat id, message id
  • jlo
    jlo about 4 years
    If you struggle finding chat_id, here's a way: stackoverflow.com/questions/49634204/…
  • Long Do Thanh
    Long Do Thanh almost 2 years
    This GET request will not work for very long message, which maximum length of 2048 characters