S
S
seredaes2015-06-25 09:45:06
Telegram
seredaes, 2015-06-25 09:45:06

Created a bot in telegram. How to use its API?

Sending a POST to: https://api.telegram.org/bot <code>/sendMessage
As JSON: {"chat_id":"112276284", "text":"www"}
Response:
{"ok":false, "error_code":400,"description":"Error: Bad Request: text is empty"}
API: https://core.telegram.org/bots/api#sendmessage
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
Y
YurasovAlexey, 2015-07-08
@seredaes

Look here the source of my calculator
https://github.com/format37/CalcuBot
https://telegram.me/calcubot

S
seredaes, 2015-06-25
@seredaes

I figured it out a bit:
1. Get all the messages that were sent to the bot:
https://api.telegram.org/bot***/getUpdates
Get a JSON string in the browser window (not a POST response)
2. From the received response, find out the id of the user who asked the question is
https://api.telegram.org/bot***/sendMessage?chat_i...
And in the chat you will receive a response from the bot :)))
True, I have not yet figured out how to send a request via POST...

Y
YaVasilyAbakumov, 2015-06-25
@YaVasilyAbakumov

https://api.telegram.org/bot number : token /sendMessage?chat_id= integer &reply_to_message_id= integer &text= Test

I
Izzat Rakhmatov, 2015-10-04
@IzzatRakhmatov

Here is an example of sending a response in Python .

import cookielib
import urllib
import urllib2
import json
CJ = cookielib.LWPCookieJar()

USER_AGENT = 'LinuxBOT'
OPENER = urllib2.build_opener(urllib2.HTTPCookieProcessor(CJ))
BASE_URL = "https://api.telegram.org/bot<token>/"
url = BASE_URL + 'getUpdates'
req = urllib2.Request(url)
req.add_header("Accept","application/json")
req.add_header('User-agent',USER_AGENT)
content = OPENER.open(req).read()
for result in json.loads(content)['result']:
    if(result['message']['text'] == 'привет'):
        url = BASE_URL + 'sendMessage'
        req = urllib2.Request(url)
        req.add_header("Accept","application/json")
        req.add_header('User-agent',USER_AGENT)
        req.add_data(urllib.urlencode({'chat_id':result['message']['chat']['id'],'text':'Эй Привет чувак!'}))
        OPENER.open(req).read()

A
Alexey Ukolov, 2015-06-25
@alexey-m-ukolov

I did not find anything in the documentation about the fact that your request can be in json format. Most likely, Telegram simply does not parse your serialization and therefore does not see the text field. It perceives all your request as one big obscure line.

A
AcidUA, 2015-11-09
@AcidUA

tell me how to send a smiley via bot api

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question