Overview
The chat skill is written to allow conversations with Andi. This skill template will help you write chat skills in PrecisionLender. You can modify the code in the template or use it as a reference when writing your skill. This article will walk through creating a chat skill.
In this Article
Should I Run
The shouldIRun is used by the Andi skills platform to determine if the given skill's current context needs to be executed.
export async function shouldIRun(skillContext: ISkillContext): Promise<ShouldIRunResponseType> {
Uses the getNaturalLanguageEvent power to get an INaturalLanguageEvent typed event.
var event: INaturalLanguageEvent = skillContext.powers.andi.event.getNaturalLanguageEvent();
Returns false if not a NaturalLanguageEvent.
if (!event) {
return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
}
Gets the Top Scoring Intent using the getTopScoringIntent power.
let topScoringIntent: INaturalLanguageUnderstandingResult = skillContext.powers.andi.chat.getTopScoringIntent();
Verifies that the top scoring intent match the skill's configured nlpSettings.
if (topScoringIntent.intent === "my-company-intent" && topScoringIntent.nluIntentSource === "my-company-intent-source") {
Tells Andi to run the skill with a score so she can determine priority.
return skillContext.powers.andi.shouldIRun.shouldIRunTrue(topScoringIntent.intentScore);
}
Tells Andi not to run the skill.
return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
}
In total, the shouldIRun code is as follows:
import { ISkillContext, ShouldIRunResponseType, INaturalLanguageEvent, INaturalLanguageUnderstandingResult } from "andiskills";
// shouldIRun is used by the andi skills platform to determine if the given
// skill's current context needs to be executed.
export async function shouldIRun(skillContext: ISkillContext): Promise<ShouldIRunResponseType> {
var event: INaturalLanguageEvent = skillContext.powers.andi.event.getNaturalLanguageEvent();
// return false if not a NaturalLanguageEvent
if (!event) {
return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
}
let topScoringIntent: INaturalLanguageUnderstandingResult = skillContext.powers.andi.chat.getTopScoringIntent();
// does the top scoring intent match the skill's configured nlpSettings
if (topScoringIntent.intent === "my-company-intent" && topScoringIntent.nluIntentSource === "my-company-intent-source") {
return skillContext.powers.andi.shouldIRun.shouldIRunTrue(topScoringIntent.intentScore);
}
return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
}
Run
This is main execution of the skill which is called after the Andi skills platforms determines if it should run.
export async function run(skillContext: ISkillContext) : Promise<ISkillActivity> {
Gets user-defined data from the skillConfig.
const articleUrl = skillContext.skillConfig && skillContext.skillConfig.articleUrl;
if (articleUrl) {
return getDialogueResponseWithTags(articleUrl);
}
else {
Uses the sendSimpleHeader power to send a simple message back to the user.
return skillContext.powers.andi.chat.sendSimpleHeader("Test answer to Andi question.");
}
}
function getDialogueResponseWithTags(articleUrl) {
Builds and sends a Dialog response with a tag, using the sendDialogueResponseWithTags power.
const tag: DialogueTag = {
name: "Test dialogue tag",
tagType: FieldTagTypes.Info,
text: "Here is a test help article.",
onClick: [{
name: "openNewTab",
args: [articleUrl]
}]
};
return DialogueResponsePowers.sendDialogueResponseWithTags({
message: "I found this helpful article:",
tags: [tag]
});
}
In total, the run code is as follows:
import { DialogueResponsePowers, DialogueTag } from "@andi/powers";
import { FieldTagTypes, ISkillContext, ISkillActivity } from "andiskills";
// this is main execution of the skill which is called after the andi
// skills platforms determines if it should run
// consult andi skills documentation for more details
export async function run(skillContext: ISkillContext) : Promise<ISkillActivity> {
const articleUrl = skillContext.skillConfig && skillContext.skillConfig.articleUrl;
if (articleUrl) {
return getDialogueResponseWithTags(articleUrl);
}
else {
return skillContext.powers.andi.chat.sendSimpleHeader("Test answer to Andi question.");
}
}
function getDialogueResponseWithTags(articleUrl) {
const tag: DialogueTag = {
name: "Test dialogue tag",
tagType: FieldTagTypes.Info,
text: "Here is a test help article.",
onClick: [{
name: "openNewTab",
args: [articleUrl]
}]
};
return DialogueResponsePowers.sendDialogueResponseWithTags({
message: "I found this helpful article:",
tags: [tag]
});
}