The first step is to download the original sample application from GitHub. Follow the instructions in the Readme file in the repository to get this application running on your local machine and/or on the BlueMix service. Make sure you have the original application working correctly before you go on to make any changes.
Modify the code below to reference the username and password of an Gmail account you have access to (or create a new account) and then add it near the top of the app.js file.
var nodemailer = require ('nodemailer');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'chatbot@gmail.com',
pass: 'secretpassword'
}
});
var mailOptions = {
from: 'chatbot@gmail.com',
to: 'name@email.com',
subject: 'Sending Email from the chatbot',
text: 'That was easy!'
}
If you use a gmail email and two factor authentication in Gmail you have to get an app password for your gmail address. Google are worried you will share your Gmail password with people so they give you a special password just for your app that has limited powers.
Next update the Watson application's package.json to say that you want to install the nodemailer package and force the server to use a version of node.js that supports nodemailer i.e.:
"dependencies": {
...,
"nodemailer": ">0.3.x"
},
...
"engines": {
"node": ">= 6.9.x",
"npm": "> 5.30.x"
}
Having done this, the next step is to change the conversation flow in the Watson Conversation Service so that when the user says ‘send an email’, we will set a context variable called 'email' to say an email should be set. (A production version would probably send different emails to different addresses depending upon context, but we will stick to a simple example for now)
If your WCS has an intent that you want to send an email. This contains example user utterances like
#sendEmail
Can you email for me?
email dave important info
Ping dave an email to tell him to do important business things
Send an email to find my package
Send an email reminder message
Message Dave a mail to tell him the stuff he needs to do
In WCS dialog tab we want a node that recognises this intent and sets a context variable to signal to the node application to send an email.
In the json editor of the dialog node add a context variable for the email.
{
"context": {
"email": "TRUE"
},
"output": {
"text": {
"values": [
"Ill email david now"
],
"selection_policy": "sequential"
}
}
}
Next, you need to change app.js to say that when this ‘send an email' context variable is send it. At the start of the function updateMessage add in the following code check to see if the context variable email from WCS' response has been set and send an email if it has.
if(response.context.email){
response.context.email=null;//set send variable to null so we don't send email every time
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
You can download the complete sample from here
This code just sends the same email every time. In practice you would probably have to include some information from the conversation in the mail. Usually this would something from context variables. You would add in the email information the user had told the chatbot such as the value in response.context.query
I am not a node developer and this code needs some error correction and security checks added to it. But for a quick demo it should allow you to show your chatbot emailing.
This comment has been removed by a blog administrator.
ReplyDeleteI have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. Common Spanish phrases
ReplyDeleteI have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. Learn spanish
ReplyDeleteYou have a good point here!I totally agree with what you have said!!Thanks for sharing your views...hope more people will read this article!!! Spanish present tense
ReplyDelete