rehabstudio / fbmessenger

A python library to communicate with the Facebook Messenger API's
Other
115 stars 50 forks source link

What is the right syntax for calling async functions? #81

Open master0v opened 3 years ago

master0v commented 3 years ago

Would you like assistance or advice? assistance

Issue description in the example below, trying to call an async function from within "def message(self, message):"

Steps to reproduce insert "await inside the "def message(self, message):" where is a function defined as async

Traceback or other info

master0v commented 3 years ago

I followed the advice given here: https://stackoverflow.com/questions/31866796/making-an-asynchronous-task-in-flask

Basically I had to change the Flask harness like so:

@app.route('/webhook', methods=['GET', 'POST'])
**async** def webhook():
  if request.method == 'GET':
    logger.debug(f"checking that {request.args.get('hub.verify_token')} == {configuration.cfg['FB_VERIFY_TOKEN']}")
    if (request.args.get('hub.verify_token') == configuration.cfg['FB_VERIFY_TOKEN']):
      return request.args.get('hub.challenge')
    raise ValueError('FB_VERIFY_TOKEN does not match.')
  elif request.method == 'POST':
    **await** messenger.handle(request.get_json(force=True))
  return ''

as well as install pip3 install asgiref

is this correct?