treethought / flask-assistant

Framework for Building Virtual Assistants with Dialogflow and python
Apache License 2.0
380 stars 100 forks source link

Get user message on flask-assistant #114

Closed Vonisoa closed 5 years ago

Vonisoa commented 5 years ago

I use flask-assistant on python 3 with dilaogflow as a webhook. I looked at the official documentation and I don't find how to get the user message ("queryText" on dialogflow json request). I tried this with no success:

# -*- coding: utf-8 -*-
from flask import Flask
from flask_assistant import Assistant, ask, tell, context_manager, event

project_id='myproject_id'
app = Flask(__name__)
assist = Assistant(app, route='/', project_id = project_id)

@assist.action('Default Fallback Intent')
def manage_fallback(queryText):
    print('*'*40,queryText)
    speech='Running'
    return tell(speech)

if __name__ == '__main__':
    app.run(debug=True)

The print of queryText always shows None, but when I inspect on the ngrok web interface (http://127.0.0.1:4040) , I can see the request.

And I want to know how canI get the user message from flask-assistant?

treethought commented 5 years ago

You can get the query text from the flask-assistant request object.

from flask_assistant import request
...
...
@assist.action('Default Fallback Intent')
def manage_fallback():
    user_query = request['queryResult']['queryText']
    speech = "You said " + user_query
    return tell(speech)

The reason the value of queryText expected by your manage_fallback function is None is because the parameter name must match the entity type expected by the intent.

Parameters are used to receive parsed entities for an intent, not the full user query.

Vonisoa commented 5 years ago

Thank you so much! You save me