I'm trying to create a bot using DialogFlow, Twilio and Flask but I'm currently stuck at something that show seem easy but couldn't find a lot of answers.
Basically I fetch de json answer from Dialogflow using the function below:
def fetch_reply(query, session_id):
#gets response from DialogFlow
response = detect_intent_from_text(query, session_id)
resp = {}
#Understading response and seeting it to a dictionary
print(response)
resp['text']=response.fulfillment_text
resp['intent']=response.intent.display_name
resp['parameters'] = response.parameters.fields.values()
return resp
I printed the full response, and it gives me the following:
query_text: "Tim\303\243o"
parameters {
fields {
key: "soccerteams"
value {
string_value: "Corinthians"
}
}
}
all_required_params_present: true
fulfillment_messages {
text {
text: ""
}
}
intent {
name: "projects/whatsappbotsports-ylml/agent/intents/e7bcf0f5-d37f-4c8b-81ad-09579fded36a"
display_name: "Default Team Request"
}
intent_detection_confidence: 1.0
language_code: "pt-br"
but when I print the resp['parameter'] my result is:
ValuesView({'soccerteams': string_value: "Corinthians"
})
All I need to access is "Corinthians", or the value of string_value, but I can't find a way to do it. If I try to use resp['parameter'].value or resp['parameter'].string_value it gives me that ValuesView doesn't have this attributes.
Any idea how to do it?
That's some very strange output that you have. It's not a JSON, since the keys don't have quotes around them.
Can you try something like this?
import json
from google.protobuf.json_format import MessageToJson
def fetch_reply(query, session_id):
#gets response from DialogFlow
response = detect_intent_from_text(query, session_id)
# trying to parse json
soccerteams = response.parameters.fields["soccerteams"]
soccerteams_json = json.loads(MessageToJson(soccerteams))
return soccerteams_json
and then try to get the value from json.
That's just my try to adapt this code: Get Dialogflow context parameters from a follow up intent in Python
Let me know if it worked. If not, please send some output.