我有一个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);
});
但它不工作,因为当我打电话回答1意图。我有一个警告。
Payload is not defined
在你的代码的顶部,你有以下语句。
const {Card, Suggestion} = require('dialogflow-fulfillment');
在这里,你只导入了Card和Suggestion类,你应该把Payload类也加进去。
const {Card, Suggestion, Payload} = require('dialogflow-fulfillment');
这样做之后,警告就会消失。