【问题标题】:Handle telegram webhook data in python 3在 python 3 中处理电报 webhook 数据
【发布时间】:2017-11-15 01:38:10
【问题描述】:

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

【问题讨论】:

    标签: python-3.6 chatbot telegram-bot telegram-webhook py-telegram-bot-api


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2014-02-14
      • 2021-01-21
      • 2021-05-28
      • 2017-01-14
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      相关资源
      最近更新 更多