Search code examples
python-3.6chatbottelegram-bottelegram-webhookpy-telegram-bot-api

Handle telegram webhook data in python 3


I made chatbot using this framework pyTelegramBotAPI and set webhook to my chatbot in Telegram. I use CherryPy for this. Everything works fine. But I can't handle data which user sends to my bot. I just get notification that user send something. How I can solve this? Thanks.


Solution

  • I solved this issue. Just found variable in my code that responds for json. Here's my code:

    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)