Overview
The email skill is written to send an email when an opportunity is saved in PrecisionLender. The email will be sent to the designated recipients in the skill code, and will contain the opportunity id and a link to the opportunity. This article will walk through the email skill template.
Note
We recommend reviewing the Email Power article and the Handlebars Templating Language article before utilizing the email skill Template. These articles provide more details on leveraging these features of the Andi Platform.
In this Article
ShouldIRun
This template triggers a 'run' on a configured frequency which can be one of the following intervals.
- Weekly
- Daily
- Hourly
- Every 5 minutes
- Every Minute
The configuration is specified in Andi Skills Builder and the actual values for the configuration are added on the Skills Details page of the skill once it's deployed.
ShouldIRun - Code
import { ISkillContext, IShouldIRunResponse } from "andiskills";
// shouldIRun is used by the andi skills platform to determine if the given
// skill's current context needs to be executed.
export function shouldIRun(skillContext: ISkillContext): Promise {
const frequency = skillContext.skillConfig.emailFrequency;
const andiPowers = skillContext.powers.andi;
let shouldRun: boolean = false;
if (frequency === "Weekly") {
shouldRun = andiPowers.event.checkWeeklySundayMidnightEvent();
}
else if (frequency === "Daily") {
shouldRun = andiPowers.event.checkDailyMidnightEvent();
}
else if (frequency === "Hourly") {
shouldRun = andiPowers.event.checkHourlyIntervalEvent();
}
else if (frequency === "5-Minute") {
shouldRun = andiPowers.event.checkFiveMinuteIntervalEvent();
}
else {
shouldRun = andiPowers.event.checkOneMinuteIntervalEvent();
}
return shouldRun ? andiPowers.shouldIRun.shouldIRunTrue() : andiPowers.shouldIRun.shouldIRunFalse();
}
Configure Frequency
Run
Once the Andi Skills platform has determined that skill should be run based on the configuration and `ShouldIRun` code it will then execute the `Run` method of the skill.
The first part is a definition of an HTML table template that will be used to generate the final output of the email. It includes a simple `#each` Handlebars template statement which will loop through each item in the data that will be bound to this template by the power.
HTML table template
Following the template is an the creation of HTML data that will get feed to the EmailPower along with the template to generate the final email output.
HTML Data
// Above template expects this data shape.
// This data is set inline here but can also come from other sources like API call or DataPowers call.
const htmlData = {
rows: [
{
title: "THE GREAT GATSBY",
details: "Author: F. Scott Fitzgerald",
},
{
title: "BRAVE NEW WORLD",
details: "Author: Aldous Huxley",
},
{
title: "THE SOUND AND THE FURY",
details: "Author: William Faulkner",
},
{
title: "CATCH-22",
details: "Author: Arthur Koestler",
},
],
};
Now that the template and data are defined that needs to added to the `EmailBody` which is one of two parameters that will get feed to `EmailPowers`. This parameter is what the `EmailPower` uses to produce the final HTML output.
// Compose EmailBody using above data, it requires htmlBody & htmlData
const emailBody: EmailBody = {
htmlBody,
htmlData,
template: "default", // Default Email wrapper template that includes header/footer will be used. You can use 'none' if your own HTML is sufficient.
};
With the `EmailBody` specified next step is to determine the subject, who will be receiving the email and what if any attachments need to be included. In order to use this template it will be necessary to replace the addresses in `to` and `cc`.
Email Options
// Compose EmailOptions with a text attachment.
// Attachment in this case is an inline text content. You can use csv, json, excel, word, txt etc. type of file data. You can load such files using DataPowers.
const emailOptions: EmailOptions = {
subject: "Test email - “Greatest Book Ever Written”",
// If your organization is using email whitelist, the email address must belong to one of the whitelisted domains.
to: ["youremail@yourdomain.com"],
cc: ["youremail2@yourdomain.com"],
attachments: [
{
name: "notes.txt",
content: "Literary critics, historians, avid readers, and even casual readers will all have different opinions on which novel is truly the “greatest book ever written.” Is it a novel with beautiful, captivating figurative language? Or one with gritty realism? A novel that has had an immense social impact? Or one that has more subtly affected the world? Here is a list of 4 novels that, for various reasons, have been considered some of the greatest works of literature ever written.",
},
// A storage file can also be used as an attachment. You can embed an image by attaching it and referencing it by its contentId within the HTML.
// {
// name: "books.png",
// content: await DataPowers.get("books.png", { type: DataType.File, parseOptions: "buffer" }),
// contentId: "books.png"
// }
],
};
The last step sends out the email with the specified `EmailBody` and `EmailOptions` and returns a run complete signal the end of the skill run.
// Send email using EmailPowers. This email will be put on a queue and sent as soon as possible.
await EmailPowers.send(emailBody, emailOptions);
return skillContext.powers.andi.batchJob.returnRunComplete();