首页 > 解决方案 > 如何使用 Dialogflow ES Fulfillment 触发向人类发送电子邮件

问题描述

我在我的编辑器中使用fulfillment 和nodemailer,并想向我的个人电子邮件地址发送一封电子邮件。当我尝试集成到 Dialogflow Messenger Beta 版时,我的聊天机器人无法理解响应。

我的输出:

User: "I would like to send an email to the support service."
Bot: "Sorry. I do not understand your question."

正确的输出:

 User: "I would like to send an email to the support service."
 Bot: "What is your name?"
 User: "Henry"
 Bot: "What is your email?"
 User: "henry65@gmail.com"
 Bot: "Thanks Henry. We will get back to you as soon as possible."

index.js

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'james641@gmail.com',
        pass: 'test@1567'
    }
});

 
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`);
    agent.add(`I'm sorry, can you try again?`);
  }
  
  function sendEmailHandler(agent){
     const { email, name } = agent.parameters;
     const question = agent.parameters.question;
     const mailOptions = {
           from: "James", // sender address
           to: email, // list of receivers
           subject: "Email from Chatbot", // Subject line
           html: "<p>Name:</p> " + name + "<p>Email:</p>" + email + "<p>Questions:</p>" + question
     };
    
    transporter.sendMail(mailOptions, function (err, info) {
           if(err)
           {
               console.log(err);
           }
    });
  }

  // // Uncomment and edit to make your own intent handler
  // // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
  // // below to get this function to be run when a Dialogflow intent is matched
  // function yourFunctionHandler(agent) {
  //   agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
  //   agent.add(new Card({
  //       title: `Title: this is a card title`,
  //       imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
  //       text: `This is the body text of a card.  You can even use line\n  breaks and emoji! `,
  //       buttonText: 'This is a button',
  //       buttonUrl: 'https://assistant.google.com/'
  //     })
  //   );
  //   agent.add(new Suggestion(`Quick Reply`));
  //   agent.add(new Suggestion(`Suggestion`));
  //   agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
  // }

  // // Uncomment and edit to make your own Google Assistant intent handler
  // // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
  // // below to get this function to be run when a Dialogflow intent is matched
  // function googleAssistantHandler(agent) {
  //   let conv = agent.conv(); // Get Actions on Google library conv instance
  //   conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
  //   agent.add(conv); // Add Actions on Google library responses to your agent's response
  // }
  // // See https://github.com/dialogflow/fulfillment-actions-library-nodejs
  // // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('sendEmail_intent', sendEmailHandler);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

我对 Dialogflow 还是很陌生,所以我对 webhook 和履行不是那么精通。请帮我。

标签: dialogflow-eswebhooksnodemailerdialogflow-es-fulfillment

解决方案


实现用例的一种方法是激活默认回退意图并启用该意图的实现。但是,您必须确保在回退意图中收集电子邮件地址。

  • 在 Dialogflow 上,webhook 由默认回退触发,它提供问题和 sessionID,然后将其保存在数据库中。
  • 然后回退方法请求一个电子邮件地址。
  • 最后,用户提供一封电子邮件,intent 触发另一个 webhook,它使用电子邮件地址更新原始记录并将请求发送到收件箱。

为了避免这种麻烦,您可以将 Dialoglfow 与任何第三方平台集成,例如Kommunicate,并直接从仪表板发送电子邮件通知。当触发默认回退意图或将人工代理分配给对话时,将自动发送一封电子邮件。

PS:我为 Kommunicate 工作。


推荐阅读