能够得到响应,但不显示

2024-05-14 19:45:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用flask和webhook为对话框流编写了python代码。我能够得到响应,但它没有显示在对话框流中。这段代码运行得很好。你知道吗

代码:

# import os
import json
# import urllib
import datetime
from config import Configuration
from swe_commands import SweCommands
from flask import Flask, request, make_response


# Flask application should start in global layout
app = Flask(__name__)


@app.route('/webhook', methods=['POST'])
def webhook():
    req = request.get_json(silent=True, force=True)
    print "Request:"
    print json.dumps(req, indent=1)

    res = make_webhook_result(req)
    res = json.dumps(res, indent=1)
    print "Response:"
    print res

    r = make_response(res)
    r.headers['Content-Type'] = 'application/json'

    return r


def make_webhook_result(req):
    # if req.get("queryResult").get("action") != "nakshatra":

    #     return {}

    swe_path = Configuration.swe_path()
    date_and_time = str(datetime.datetime.now())[:19]
    panchang_dictionary = SweCommands.find_panchang(swe_path, date_and_time)

    result = req.get("queryResult")
    parameters = result.get("parameters")
    angam = parameters.get("nakshatra")

    nakshatra = panchang_dictionary[angam]

    speech = "Current nakshatra is %s" % nakshatra
    source = "Panchangam"

    output = {'speech': speech, "displayText": speech, "source": source}

    return output


if __name__ == '__main__':
    port = 5000
    print "Starting app in port %s" % port
    app.run(debug=True, port=port, host='127.0.0.1')

请求:

**{
 "queryResult": {
  "fulfillmentMessages": [
   {
    "text": {
     "text": [
      ""
     ]
    }
   }
  ],
  "allRequiredParamsPresent": true,
  "parameters": {
   "nakshatra": "nakshatra"
  },
  "languageCode": "en",
  "intentDetectionConfidence": 0.6725314,
  "action": "nakshatra",
  "intent": {
   "displayName": "PanchangFind",
   "name": "projects/my-project-1536557558293/agent/intents/6d1d46bf-3787-48cd-9b45-0766d5f2b107"
  },
  "queryText": "What is nakshatra"
 },
 "originalDetectIntentRequest": {
  "payload": {}
 },
 "session": "projects/my-project-1536557558293/agent/sessions/08857865-1d08-2eef-5d4f-83b92107f09b",
 "responseId": "2137da9d-23a9-4735-aec2-7adb7ae52d85-9cc28bb4"
}**

答复:

**{
 "displayText": "Current nakshatra is Shravana",
 "speech": "Current nakshatra is Shravana",
 "source": "Panchangam"
}**

但它不会显示在对话框流面板中?是否必须为对话框流面板设置任何参数才能在对话框流中接收响应。请告诉我。你知道吗


Tags: importjsonappgetmakeportreswebhook
1条回答
网友
1楼 · 发布于 2024-05-14 19:45:26

我得到了答案。当我们在对话框流中使用V1时,响应键是displayText。但在V2中,响应键是fulfillmentText。当我在这个名称中添加response键时,它能够检测输出。你知道吗

相关问题 更多 >

    热门问题