簡體   English   中英

如何使用 webhook 使用 python 和 dialogflow 制作簡單的面積計算器?

[英]how do i make simple area calculator with python and dialogflow using webhook?

我正在嘗試使用 webhook 制作帶有對話流和 python 的面積計算器。 我想要的只是返回區域的值。這是響應

{
  "responseId": "94e23055-2c54-42da-ba78-1f44eff8d0ab-0f0e27e1",
  "queryResult": {
    "queryText": "find the area of 55cm and 88m",
    "parameters": {
      "unit-length": "",
      "unit-length1": {
        "unit": "cm",
        "amount": 55
      }
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/ultrabot-tlyvuq/agent/intents/b2b8e38b-6982-4b8d-8ff5-0bc42b955443",
      "displayName": "findArea"
    },
    "intentDetectionConfidence": 0.66548675,
    "diagnosticInfo": {
      "webhook_latency_ms": 357
    },
    "languageCode": "en"
  },
  "webhookStatus": {
    "code": 14,
    "message": "Webhook call failed. Error: UNAVAILABLE."
  },
  "alternativeQueryResults": [
    {
      "queryText": "find the area of 55cm and 88m",
      "languageCode": "en"
    }
  ]
}

我如何做出此響應以返回該區域的值?

我使用 Django 框架進行了嘗試,並開發了一個基本的算術計算器。 為了執行此操作,您需要使用 dialogflow 的 webhook 概念。 JsonResponse 將履行文本返回到對話流。

@csrf_exempt
def webhook(request):
# build a request object
req = json.loads(request.body)
# get action from json
action = req.get('queryResult').get('action')
# get the arithmetic operation that needs to be done
num = req.get('queryResult').get('parameters')
n1 = int(num.get('number'))
n2 = int(num.get('number1'))
if action == 'addition':
# return a fulfillment message
    fulfillmentText = {'fulfillmentText': n1+n2}

elif action == 'subtraction':
    fulfillmentText = {'fulfillmentText': n1-n2}

elif action == 'division':
    fulfillmentText = {'fulfillmentText': n1/n2}

else:
    fulfillmentText = {'fulfillmentText': n1*n2}
# return response
return JsonResponse(fulfillmentText, safe=False)

下面 jsonResponse 將返回履行文本,

返回 JsonResponse(fulfillmentText, safe=False)

如果您需要更多詳細信息,請閱讀我的博客,其中提供了我所做的整個工作流程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM