1

我使用這個框架pyTelegramBotAPI製作了聊天機器人,並在我的聊天機器中設置了webhook。我爲此使用CherryPy。一切正常。但我無法處理用戶發送給我的機器人的數據。我剛剛收到用戶發送內容的通知。我如何解決這個問題?謝謝。在Python中處理電報webhook數據3

回答

0

我解決了這個問題。剛剛在我的代碼中找到了響應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)