Dialogflow新手求助:如何让聊天机器人自动转未知问题至邮箱人工处理?
Hey there! As someone who’s built a handful of FAQ and troubleshooting bots with Dialogflow, I can walk you through exactly how to set up this automatic email forwarding for unanswered questions. Here’s the step-by-step breakdown:
1. Lean on Dialogflow’s Default Fallback Intent
First, make sure you’re using Dialogflow’s built-in Default Fallback Intent—this is the intent that triggers whenever your bot can’t match the user’s question to any of your existing intents.
- Head to your Dialogflow agent’s Intents page.
- Locate the "Default Fallback Intent" (it’s there by default; if not, create one and mark it as the fallback intent).
- Enable Fulfillment for this intent—this tells Dialogflow to run custom code when it hits a question it can’t answer, which is where we’ll handle sending the email.
2. Set Up Fulfillment with the Inline Editor (Beginner-Friendly)
You’ll need to use Dialogflow’s Fulfillment feature to run code that sends the email. For new users, the Inline Editor (Node.js) directly in Dialogflow is the easiest route:
- Go to the Fulfillment tab in your agent.
- Toggle on the Inline Editor option.
- First, add the
nodemailerpackage (a popular Node.js tool for sending emails) to yourpackage.jsondependencies:"dependencies": { "actions-on-google": "^2.13.0", "dialogflow": "^1.2.0", "nodemailer": "^6.9.7" } - Save the file and wait for the dependencies to install (it’ll take a few seconds).
3. Write the Code to Send Emails
Update the index.js file in the Inline Editor to handle the fallback intent and send the email. Here’s a working example you can tweak:
const functions = require('firebase-functions'); const { WebhookClient } = require('dialogflow-fulfillment'); const nodemailer = require('nodemailer'); // Configure your email service (adjust this for your provider) const transporter = nodemailer.createTransport({ service: 'Gmail', // Swap with SendGrid, Outlook, or another service if needed auth: { user: 'your-email@example.com', pass: 'your-app-password-or-email-pass' // For Gmail with 2FA, use an App Password (see note below) } }); exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { const agent = new WebhookClient({ request, response }); // Handle unanswered questions and send email function forwardUnansweredQuestion(agent) { const userQuestion = agent.query; const sessionId = agent.session.split('/').pop(); const timestamp = new Date().toLocaleString(); // Build the email content const mailOptions = { from: 'your-email@example.com', to: 'your-inbox@example.com', // Your email where you want to receive the questions subject: `Unanswered Dialogflow Question - Session ${sessionId}`, html: ` <h3>New Unresolved User Question</h3> <p><strong>User's Question:</strong> ${userQuestion}</p> <p><strong>Session ID:</strong> ${sessionId}</p> <p><strong>Timestamp:</strong> ${timestamp}</p> ` }; // Send the email and reply to the user transporter.sendMail(mailOptions, (error, info) => { if (error) { console.log(error); agent.add("Sorry, I couldn't process your request right now. Please try again later."); } else { console.log('Email sent: ' + info.response); agent.add("Hmm, I don't have an answer for that right now. I've sent your question to our team, and someone will reach out to you soon!"); } }); } // Map the fallback intent to our handler function let intentMap = new Map(); intentMap.set('Default Fallback Intent', forwardUnansweredQuestion); agent.handleRequest(intentMap); });
4. Key Tips for Email Services
- Gmail Users: If you have 2FA enabled, skip using your regular password—create an App Password instead. Go to your Google Account > Security > App Passwords to generate one.
- Alternative Services: If you don’t want to use Gmail, use SendGrid or Mailgun. Just update the
transporterconfiguration with their SMTP details (you’ll find these in their dashboard). - Collect User Contact Info: If you want to follow up directly, modify the fallback intent to first ask the user for their email or phone number. Add a follow-up intent to collect that info, then include it in the email content.
5. Test It Out
Save your code, then head back to the Dialogflow Console and type a question your bot can’t answer. You’ll get the automated response, and the question should land in your email inbox within a minute.
内容的提问来源于stack exchange,提问作者Yeung Andy

