0

CherryPyの助けを借りてwebhookを電報のチャットボットに設定しました。そして今、私はwebhookを通じて受け取ったデータを処理しようとしています。私は、必要なjsonデータを含むcherrypy webhookクラスの変数を見つけました。私のコードでは、変数名はです.json_stringです。私はこの変数をPythonスクリプトのどこでも呼び出す必要があります。私はそれをどのようにすることができますか?ありがとう。cherrypy webhookクラスから変数を呼び出す

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) 

答えて

1

のは、JSONを自動的json_inツールで辞書にデコードされ、その結果がcherrypy.request.jsonに格納された単純化されたHTTPハンドラを考えてみましょう。

あなたは、外部モジュールにいくつかの関数を書くutils.pyを言う、サーバーコードとにそのデータを渡すとともに、それをインポートすることがあります。

== >>your_server.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 '' 

# ... 

== >>utils.py < < ==

def process_telegram_webhook(data): 
    # ... do smth with json data from telegram: 
関連する問題