gpt4 book ai didi

javascript - 将富消息集成到 Dialogflow 实现中

转载 作者:行者123 更新时间:2023-12-01 16:25:56 24 4
gpt4 key购买 nike

我有一个 Dialogflow 聊天机器人的实现代码,我希望能够发送(以及稍后接收和分析)丰富的消息。由于我对 javascript 编程有点陌生,所以我想知道如何使用实现代码发送丰富的消息。我尝试为 function answer1Handler 做一个。

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
var answers = [];

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

function welcome(agent) {
agent.add(`Welcome to my agent!`);
}

function fallback(agent) {
agent.add(`I didn't understand FULLFILMENT`);
agent.add(`I'm sorry, can you try again? FULLFILMENT`);
}

function answer1Handler(agent){
agent.add('Intent answer1 called');
const answer = agent.parameters.number;
answers.push(answer);

const payload = {
"slack": {
"attachments": [
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Compared to most people in the UK :uk:, how optimistic are you about life?* Poll by <fakeLink.toUser.com|Mihailo>"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":cold_sweat: *I’m feeling more negative about everything; less free and more pessimistics*"
},
"accessory": {
"value": "1",
"type": "button",
"text": {
"emoji": true,
"type": "plain_text",
"text": "Write 1"
}
},
"block_id": "1"
},
{
"accessory": {
"value": "2",
"type": "button",
"text": {
"emoji": true,
"type": "plain_text",
"text": "Write 2"
}
},
"block_id": "2",
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":expressionless: *More positive about the quality of life, but worrying more about the NHS and the security situation*"
}
}
]
}
]
}
}

agent.add(
new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true})
);
}

intentMap.set('RhymingWord', rhymingWordHandler);
intentMap.set('answer1', answer1Handler);

agent.handleRequest(intentMap);
});

但是当我调用 answer1 意图时它不起作用。我有一个警告:

Payload is not defined

最佳答案

在代码的顶部,您有以下语句:

const {Card, Suggestion} = require('dialogflow-fulfillment');

在这里你只导入了 Card 和 Suggestion 类,你还应该添加 Payload 类:

const {Card, Suggestion, Payload} = require('dialogflow-fulfillment');

完成此操作后,警告应该会消失。

关于javascript - 将富消息集成到 Dialogflow 实现中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61485984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com