2

我使用这个框架pyTelegramBotAPI制作了聊天机器人,并将 webhook 设置为我在 Telegram 中的聊天机器人。我为此使用 CherryPy。一切正常。但我无法处理用户发送给我的机器人的数据。我只是收到用户发送内容的通知。我该如何解决这个问题?谢谢。

4

1 回答 1

1

我解决了这个问题。刚刚在我的代码中找到响应 json 的变量。这是我的代码:

class WebhookServer(object):
@cherrypy.expose
def index(self):
    if 'content-length' in cherrypy.request.headers and \
                    'content-type' in cherrypy.request.headers and \
                    cherrypy.request.headers['content-type'] == 'application/json':
        length = int(cherrypy.request.headers['content-length'])
        json_string = cherrypy.request.body.read(length).decode("utf-8") <-- this one responds for json from webhook
        update = telebot.types.Update.de_json(json_string)

        global jsonObj

        jsonObj = json.loads(json_string)

        print(jsonObj)

        bot.process_new_updates([update])
        return ''
    else:
        raise cherrypy.HTTPError(403)
于 2017-07-21T08:47:02.723 回答