Example use cases:
- Sending emails when your bot fails to understand a question
- Sending data to a specific inbox
- Send a confirmation email to a user
You can send an email using the toolbelt.email()
method.
Sending HTML
We support sending (limited) HTML formatted email with custom emails.
async payload => {
toolbelt.email({
to: 'foo@bar.com',
message: '<div><h1>Example Title</h1><p>This is an HTML example</p></div>'
})
}
Sending data with e-mails
Cloud code supports JavaScript template tags that allows you to send information, like parameters inside the payload.
Sending an email with the name of a user
async payload => {
toolbelt.email({
to: 'foo@bar.com',
message: `Some data extracted: ${payload.user.name}`
})
}
Sending an email with a param
async payload => {
toolbelt.email({
to: 'foo@bar.com',
message: `Some data extracted: ${payload.params.city[0].value}`
})
}
Sending 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: 'foo@bar.com',
subject: `Collected ${paramNames.length} params`,
message: `
<h2>We found the following data!</h2>
<p>This is just a small example</p>
<p>${paramsHtml}</p>`
})
}
Sending only parameters that have a value
Sometimes not all parameters you want to send actually 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: 'foo@bar.com',
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 "-".