Code-based Actions: Sending Emails from the bot
Using the Cloud Code Actions you send emails to the users from the bot. You might need to send emails to the users in the following cases:
- When your bot fails to understand a question.
- Send data to a specific inbox.
- Send a confirmation email to a user.
You can send an email using the toolbelt.email() method.
Sending HTML
Flow Actions support sending (limited) HTML-formatted emails with custom email templates.
async payload => {
toolbelt.email({
to: '[email protected]',
message: '<div><h1>Example Title</h1><p>This is an HTML example</p></div>'
})
}
Sending data with emails
Cloud code supports JavaScript template tags that allow you to send information, like parameters inside the payload.
Send an email with the name of a user:
async payload => {
toolbelt.email({
to: '[email protected]',
message: `Some data extracted: ${payload.user.name}`
})
}
Send an email with a param:
async payload => {
toolbelt.email({
to: '[email protected]',
message: `Some data extracted: ${payload.params.city[0].value}`
})
}
Send all parameters combining a custom formatted email with dynamic data:
async payload => {
const {
params
} = payload
// Generate HTML for the params
let paramsHtml = ''
const paramNames = Object.keys(params)
for (let i = 0; i \< paramNames.length; i++) {
const paramName = paramNames[i]
// Get the param
const param = params[paramName]
// Add the param name to the HTML
paramsHtml += `
<p>
<strong>${paramName}:</strong>
<div>${param.map(values => values.value).join(', ')}</div>
</p>`
}
toolbelt.email({
to: '[email protected]',
subject: `Collected ${paramNames.length} params`,
message: `
<h2>We found the following data!</h2>
<p>This is just a small example</p>
<p>${paramsHtml}</p>`
})
}
Send only parameters that have a value:
The code below first gives the parameters a "-" value. Next, it checks if the parameter should be overwritten.
async payload => {
var email = "-"
var address = "-"
var phone ="-"
if(Array.isArray(payload.params.email)) {
email = payload.params.email[email.length - 1].value
}
if(Array.isArray(payload.params.address)) {
address = payload.params.address[address.length - 1].value
}
if(Array.isArray(payload.params.phone)) {
phone = payload.params.phone[phone.length - 1].value
}
const subject = `Chatbot Handover`
toolbelt.email({
to: '[email protected]',
subject,
message: `Someone asked for a handover <br>
E-mail:\t${email}<br>
Adres:\t${address}<br>
Phone:\t${phone}<br>`
})
}
As you can see, if only the parameters for the email and the address exist, the phone number will have a "-".
Updated about 1 year ago