带inline_keyboard的TelegramBot sendMessage

时间:2018-12-14 14:47:44

标签: python-3.x telerik

我正在尝试创建没有第三方库和漫游器的消息。 正常消息发送成功。 但是,如果我添加inline_keyboard,则会收到错误响应。 请求:

def pushMsg(self, url, id_chat, id_message):
    jsKeyboards = {
        'inline_keyboard': [
            [{ 'text': 'BOOM', 'callback_data': 'click_113' }],
            [{ 'text': 'DEAD', 'callback_data': 'click_119' }]
        ]
    }
    params = {'reply_markup': str(jsKeyboards), 'reply_to_message_id': id_message }
    self.sendMessage(url, id_chat, "TEST", params)


def sendMessage(self, url, chat_id, text, params):
    URL_FULL = url + '/' + "sendMessage?chat_id=" + str(chat_id) + "&text=" + str(text)
    print("sendMessage: " + URL_FULL)
    return self.postUrl(URL_FULL, params)

def postUrl(self, urlFull, params):
    print('urlFull: ' + urlFull + ' params: ' + str(params))
    try:
        req = requests.post(urlFull, proxies=self.proxies, data=params)
    except requests.exceptions.RequestException as e:
        print("error: " + str(e))
    except:
        print('Except requests')
        return {}
    json = req.json()
    if not req.status_code == 200:
        print("status_code: " + str(req.status_code) + "; body: " + str(json))
        return {}
    if not req.json()['ok']:
        print("body: " + str(json))
        return {}
    return json

答案:

{
    'ok': False,
    'error_code': 400,
    'description': "Bad Request: can't parse reply keyboard markup JSON object"
}

参数有什么问题?

2 个答案:

答案 0 :(得分:0)

您似乎无法发送带有URL的嵌入式键盘。

我强烈建议您使用 pyTelegramBotAPI 。它非常容易学习,可让您创造出奇妙的事物。我创建的其中一种漫游器显示了文件夹中的文件,并将其发送给我。

答案 1 :(得分:0)

str(jsData) != json.dumps(jsData, separators=(',',':'))

结果:

params = {'reply_markup': json.dumps(jsKeyboards, separators=(',',':')) }