电报键盘无法使用Python工作

时间:2018-02-05 23:15:43

标签: python keyboard bots telegram

我试图在电报上发送常规键盘,但它失败了。我使用此代码:

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

我得到的错误是:

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

但是当我将其更改为inline_keyboard时,它可以完美运行!

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

我也尝试过解决方案here(这几乎是一样的)。

你能告诉我这里可能出现什么问题吗?

0 个答案:

没有答案