0

I'm trying to send a regular keyboard on telegram and it is failing. I'm using this code:

def reply(msg=None):
    """ """
    if msg:
        import urllib2, urllib, json
        https_request = "https://api.telegram.org/bot" + token + "/"
        a = urllib.urlencode({
            'chat_id': str(chat_id),
            'text': msg.encode('utf-8'),
            'disable_web_page_preview': 'true',
            'reply_markup': '{"keyboard":[["Yes","No"],["Maybe"],["1","2","3"]],"one_time_keyboard":true}',
        })
        resp = urllib2.urlopen(https_request + 'sendMessage', a).read()

The error I get is:

>>> reply("hi")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<module2>", line 17, in reply
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 435, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 473, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 556, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request

But when I change it to inline_keyboard it works perfectly!

def reply(msg=None):
    if msg:
        import urllib2, urllib, json
        https_request = "https://api.telegram.org/bot" + token + "/"
        a = urllib.urlencode({
            'chat_id': str(chat_id),
            'text': msg.encode('utf-8'),
            'disable_web_page_preview': 'true',
            'reply_markup': '{"inline_keyboard": [[{"text": "yo", callback_data": "yo"}, {"text": "yo", "callback_data": "yo"}]]}'
        })
        resp = urllib2.urlopen(https_request + 'sendMessage', a).read()

I also tried the solution here (which is pretty much the same).

Can you please advise what could be the problem here?

roganjosh
  • 12,594
  • 4
  • 29
  • 46
Guy
  • 23
  • 3
  • I can use [same payload](https://tg.sean.taipei/payload.php?sendMessage-b66d81f6) without error – Sean Wei Feb 05 '18 at 23:47
  • Both kind of keyboards works for you? Any idea why it's not working for me? – Guy Feb 06 '18 at 01:19
  • Try it with your own token, just like change to another library – Sean Wei Feb 06 '18 at 02:41
  • Already working with my token. Inline keyboard is working and regular keyboard is not working. Update: Regular keyboard works when working directly vs the bot, and failing when it is in a channel. Does regular keyboard works on channels? Or the purpuse is only to work on a chats (only myself and the bot), and use inline keyboard on channels? Sean can you please check if it works for you in a channel? – Guy Feb 07 '18 at 11:01
  • No. I sure, no. – Sean Wei Feb 07 '18 at 11:09

0 Answers0