Answer the question
In order to leave comments, you need to log in
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
Look here the source of my calculator
https://github.com/format37/CalcuBot
https://telegram.me/calcubot
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...
https://api.telegram.org/bot number : token /sendMessage?chat_id= integer &reply_to_message_id= integer &text= Test
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()
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question