我只通过对话流得到一个whatsapp答案,而不是多个回复



如何在whatsApp中获得多个"文本答案"?当我在意图中添加多个"文本响应"时,它们在 dialogFlow 控制台中正常工作。

但是当我在whatsapp上重复相同的问题时,我只得到一个答案框,而不是我创建的3个答案框。

我正在使用twilio与whatsapp API进行通信。我还使用Horoku云服务来托管应用程序。

一切正常。但是我只收到一个消息框,而不是在whatsapp中收到多个消息框。 我认为问题是我的python代码"app.py"。

app.py

@app.route("/") #just to test Heroku cloud services
def hello():
return "Hello, World!"
@app.route("/sms", methods=['POST'])
def sms_reply():
"""Respond to incoming calls with a simple text message."""
# Fetch the message
msg = request.form.get('Body')
phone_no = request.form.get('From')
reply = fetch_reply(msg, phone_no)
# Create reply
resp = MessagingResponse()
resp.message(reply)
enter code here
return str(resp)
<小时 />

utils.py

import dialogflow_v2 as dialogflow
dialogflow_session_client = dialogflow.SessionsClient()
PROJECT_ID = "weather-husgcf"
def detect_intent_from_text(text, session_id, language_code='pt-BR'):
session = dialogflow_session_client.session_path(PROJECT_ID, session_id)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = dialogflow_session_client.detect_intent(session=session, query_input=query_input)
return response.query_result
def fetch_reply(query, session_id):
response = detect_intent_from_text(query, session_id)
return response.fulfillment_text

https://i.imgur.com/a/b2QSYUB "屏幕截图">

Twilio开发者布道者在这里。

fetch_reply方法中,你调用query_resultfulfillment_text属性。根据文档履行文本已弃用/遗留QueryResult

要向用户发音或显示在屏幕上的文本。注意:这是一个遗留字段,应首选履行消息。

fullfillmentMessages属性定义为消息对象的列表。因此,要返回所有 3 条消息,您的代码可能应该遍历将它们添加到响应中的消息,如下所示:

def fetch_reply(query, session_id):
response = detect_intent_from_text(query, session_id)
return response.fulfillment_messages

然后,您的路由应如下所示:

@app.route("/sms", methods=['POST'])
def sms_reply():
"""Respond to incoming calls with a simple text message."""
# Fetch the message
msg = request.form.get('Body')
phone_no = request.form.get('From')
replies = fetch_reply(msg, phone_no)
# Create reply
resp = MessagingResponse()
for reply in replies:
resp.message(reply.text)
return str(resp)

我还没有测试过这个,只是从对话流文档中工作。让我知道它是否有帮助。

一个古老的问题,但这是答案(对话流 v2(。

假设您有某种sendMessage(mobile_num, text)函数,您可以像这样迭代fulfillment_messages

for message in response.query_result.fulfillment_messages:
sendMessage(mobile_num, message.text.text[0])

从网络钩子中,您可以得到如下所示的 json:

{
"queryText": string,
"languageCode": string,
"speechRecognitionConfidence": number,
"action": string,
"parameters": {
object
},
"allRequiredParamsPresent": boolean,
"cancelsSlotFilling": boolean,
"fulfillmentText": string,
"fulfillmentMessages": [
{
"text": {
"text": [
"Some text"
]
}
},
{
"text": {
"text": [
"Some more text"
]
}
},
],
"webhookSource": string,
"webhookPayload": {
object
},
"outputContexts": [
{
object (Context)
}
],
"intent": {
object (Intent)
},
"intentDetectionConfidence": number,
"diagnosticInfo": {
object
},
"sentimentAnalysisResult": {
object (SentimentAnalysisResult)
}
}

其中fulfillmentMessages是一个数组,你必须迭代。

希望这有帮助。

相关内容

最新更新