I was trying to create a chatbot using dialogflow, VS Code, python, flask, an API and ngrok. I tried hard to find the solution of this problem but still unsuccessful. With heavy heart, I decided to share it with my stack overflow community.
Here is the full flask backend code:
from flask import Flask,request,jsonify
import requests
app = Flask(__name__)
@app.route('/',methods=['POST'])
def index():
data = request.get_json()
source_currency = data['queryResult']['parameters']['unit-currency']['currency']
amount = data['queryResult']['parameters']['unit-currency']['amount']
target_currency = data['queryResult']['parameters']['currency-name']
cf = fetch_conversion_factor(source_currency,target_currency)
final_amount = amount * cf
final_amount = round(final_amount,2)
response = {
'fulfillmentText':"{} {} is {} {}".format(amount,source_currency,final_amount,target_currency)
}
return jsonify(response)
def fetch_conversion_factor(source,target):
url = "https://prepaid.currconv.com/api/v7/convert?q={}_{}&compact=ultra&apiKey=cf50a6e1e1a9bf32f3b9".format(source,target)
response = requests.get(url)
response = response.json()
return response['{}_{}'.format(source,target)]
if __name__ == "__main__":
app.run(debug=True)
and here is raw API response(on dialogflow):
{
"responseId": "adb7509a-add5-4674-b4ad-6bff5e541b52-0e7b65aa",
"queryResult": {
"queryText": "convert 500 USD to INR",
"parameters": {
"unit-currency": [
{
"currency": "USD",
"amount": 500
}
],
"currency-name": [
"INR"
]
},
"allRequiredParamsPresent": true,
"fulfillmentText": "Error! Please try again later.",
"fulfillmentMessages": [
{
"text": {
"text": [
"Error! Please try again later."
]
}
}
],
"intent": {
"name": "projects/moneybot-tv9q/agent/intents/ef5701ea-afb0-4700-a28b-b5c78495b8d9",
"displayName": "currency-convertor"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 1052
},
"languageCode": "en",
"sentimentAnalysisResult": {
"queryTextSentiment": {}
}
},
"webhookStatus": {
"code": 14,
"message": "Webhook call failed. Error: UNAVAILABLE, State: URL_UNREACHABLE, Reason: UNREACHABLE_5xx, HTTP status code: 500."
}
}
Please help me to solve this problem as I am very depressed. I am trying to solve this problem from last 2 days. I searched on all platform but still no solution.