Python cherrypy webhook类中的调用变量

Python cherrypy webhook类中的调用变量,python,python-3.x,cherrypy,python-telegram-bot,telegram-webhook,Python,Python 3.x,Cherrypy,Python Telegram Bot,Telegram Webhook,在CherryPy的帮助下,我将webhook设置为我的电报聊天机器人。现在我正在尝试处理通过webhook接收到的数据。我在cherrypy webhook类中找到了这个变量,它包含所需的json数据。在我的代码中,变量名是json\u string。我需要在python脚本中的任何地方调用这个变量。我怎么能做到?谢谢 class WebhookServer(object): @cherrypy.expose def index(self): if 'conte

在CherryPy的帮助下,我将webhook设置为我的电报聊天机器人。现在我正在尝试处理通过webhook接收到的数据。我在cherrypy webhook类中找到了这个变量,它包含所需的json数据。在我的代码中,变量名是json\u string。我需要在python脚本中的任何地方调用这个变量。我怎么能做到?谢谢

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")
            update = telebot.types.Update.de_json(json_string)
            bot.process_new_updates([update])
            return ''
        else:
            raise cherrypy.HTTPError(403) 

让我们考虑一下简化的HTTP处理程序,其中json通过工具中的
json_自动解码为dict,结果存储在
cherrypy.request.json

您可以在外部模块中编写一些函数,例如
utils.py
,将其与服务器代码一起导入,并将数据传入:

==>
您的_服务器.py
utils.py
from utils import process_telegram_webhook

class WebhookServer(object):
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def index(self):
        req = cherrypy.request
        incoming_dict_object = req.json
        process_telegram_webhook(incoming_dict_object)
        return ''

# ...
def process_telegram_webhook(data):
    # ... do smth with json data from telegram: