Hierarchy

  • PromptPowers

Index

Methods

Static getAllUserChoices

Static getAllUserInput

  • Gets the full list of user input (prompt or form responses) entered during the current dialogue with the skill.

    example

    In the example the state of all the choices are used to create a conversation state

    // Ensure you import PromptPowers
    import { DialogueInputType } from "andiskills";
    import { DialogueResponsePowers, DialogueResponse, DialogueTag, DialoguePrompt, PromptPowers } from "@andi/powers";
    
    //Defined Choices
    enum ChoiceIds {
       MoneyBackChoiceYes = "MoneyBackChoiceYes",
       MoneyBackChoiceNo = "MoneyBackChoiceNo",
       MoneyBackChoiceNotSure = "MoneyBackChoiceNotSure",
       DepositsChoiceNo = "DepositsChoiceNo",
       DepositsChoiceYesMoney = "DepositsChoiceYesMoney"
    }
    
    function buildConversationState(): ConversationState {
        const allUserInput = PromptPowers.getAllUserInput();
    
        let choiceOne = undefined;
        let choiceTwo = undefined;
    
        //Build the conversation state object based on the user's past choices
        allUserInput.forEach(input => {
            if (input.type === DialogueInputType.Prompt) {
                const choice = input.userChoices as Choice;
                switch (choice.choiceId) {
                    case ChoiceIds.MoneyBackChoiceYes:
                    case ChoiceIds.MoneyBackChoiceNo:
                    case ChoiceIds.MoneyBackChoiceNotSure:
                        choiceOne = choice.choiceId;
                        break;
                    case ChoiceIds.DepositsChoiceYesGoats:
                    case ChoiceIds.DepositsChoiceNo:
                    case ChoiceIds.DepositsChoiceYesMoney:
                        choiceTwo = choice.choiceId;
                        break;
                }
            }
            else if (input.type === DialogueInputType.Form) {
                choiceOne = input.responses.choiceOne;
                choiceTwo = input.responses.choiceTwo;
            }
        });
    
       return {
           choiceOne: choiceOne,
           choiceTwo: choiceTwo
       };
    }
    

    Returns DialogueInputData[]

Static getLatestUserChoice

Static getLatestUserInput

  • Gets the latest user input (a prompt or form response) during the current dialogue with the skill.

    example

    In the example choices are defined as enum values and demonstrating PromptPowers.getLatestUserInput() how new prompts could be returned by the skill.

    // Ensure you import PromptPowers
    import { DialogueInputType } from "andiskills";
    import { DialogueResponsePowers, DialogueResponse, DialogueTag, DialoguePrompt, PromptPowers } from "@andi/powers";
    
    // Defined Choices
    enum ChoiceIds {
       MoneyBackChoiceYes = "MoneyBackChoiceYes",
       MoneyBackChoiceNo = "MoneyBackChoiceNo",
       MoneyBackChoiceNotSure = "MoneyBackChoiceNotSure",
       DepositsChoiceNo = "DepositsChoiceNo",
       DepositsChoiceYesMoney = "DepositsChoiceYesMoney"
    }
    
    // Use the last choice the user selected to determine the next prompt response
    function getNextResponse(skillContext: ISkillContext): Promise<ISkillActivity> {
        const latestInput = PromptPowers.getLatestUserInput();
        let choiceId;
        if (latestInput.type === DialogueInputType.Prompt) {
            const latestChoice = latestInput.userChoices as Choice;
            // Each choice has a unique choiceId, this can be used to determine which prompt to send to the user
            choiceId = latestChoice.choiceId;
        }
        else if (input.type === DialogueInputType.Form) {
            choiceId = latestInput.responses.moneyBack;
        }
        switch (choiceId) {
            case ChoiceIds.MoneyBackChoiceYes:
            case ChoiceIds.MoneyBackChoiceNo:
            case ChoiceIds.MoneyBackChoiceNotSure:
                return getMoneyBackChoicePrompt();
            case ChoiceIds.DepositsChoiceNo:
            case ChoiceIds.DepositsChoiceYesMoney:
                return getFinalResponse();
            default:
                throw new Error("No matching response paths: " + JSON.stringify(latestChoice));
        }
    }

    Returns DialogueInputData | null

Static isPromptResponseEvent

  • isPromptResponseEvent(): boolean
  • Returns whether the current event is the result of a user's response to a prompt.

    example

    In this example isPromptResponseEvent is used in shouldIRun to determine if the skill should run using prompts.

    import { ISkillContext, ShouldIRunResponseType, INaturalLanguageEvent, INaturalLanguageUnderstandingResult } from "andiskills";
    import { PromptPowers } from "@andi/powers";
    
    // 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> {
    // Is this event a response to an in progress conversation thread
    if (PromptPowers.isPromptResponseEvent()) {
         return skillContext.powers.andi.shouldIRun.shouldIRunTrue();
     }
     // If not, check if a user is requesting a new conversation thread
     else {
         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();
    }

    Returns boolean

Static sendDialogueComplete

  • Sends a response that indicates that a user's dialogue with a skill is complete.

    example

    This example demonstrates sending the the final dialogue of a conversation.

    //Ensure you import PromptPowers
    //import { DialogueResponsePowers, DialogueResponse, DialogueTag, DialoguePrompt, PromptPowers } from "@andi/powers";
    
    //Once a user has completed all selections, return the final response
    function getFinalResponse(): Promise<ISkillActivity> {
        //Build a conversation state object with the user's past choices
        const conversationState = buildConversationState();
    
        //Calculate the operationResult and set a url to provide the user with additional information
        let message = "Sorry, this Loan is not approved.";
        let tagUrl = undefined;
        if (conversationState.choiceOne === ChoiceIds.MoneyBackChoiceYes &&
            conversationState.choiceTwo === ChoiceIds.DepositsChoiceYesMoney) {
            message = "Great! Loan approved.";
            tagUrl = "https://www.example.com/loan_approved/";
        }
    
        //Optional: a tag can be included with the text response
        if (tagUrl) {
            const tag: DialogueTag = {
                name: "additionalInformationTag",
                tagType: FieldTagTypes.Info,
                text: "Learn more about the business",
                onClick: [{
                    name: "openNewTab",
                    args: [tagUrl]
                }]
           };
           const dialogueResponse: DialogueResponse = { message: message, tags: [tag] };
           return PromptPowers.sendDialogueComplete(dialogueResponse);
        }
        else {
            const dialogueResponse: DialogueResponse = { message: message, tags: [] };
            return PromptPowers.sendDialogueComplete(dialogueResponse);
        }
    }

    Parameters

    • dialogueResponse: DialogueResponse

      Contains the message to be shown to the user, and any tags that would be relevant to the user as a result of the dialogue.

    Returns Promise<ISkillActivity>

Static sendDialogueResponseWithForm

  • Sends a dialogue response along with a form for the user to respond to. Responses to the form's prompts will return back to the same skill that sent the form.

    example

    This is an example of sending a form with a select prompt and a text prompt.

    //Ensure you import PromptPowers
    //import { FormPromptType, ISkillActivity } from "andiskills";
    //import { DialogueResponse, PromptPowers, FormOptions } from "@andi/powers";
    
    function getPaybackChoicePrompt(): Promise<ISkillActivity> {
        const dialogueResponse: DialogueResponse = { message: "Why was this opportunity lost?", tags: [] };
    
        const formOptions: FormOptions = {
            prompts: [
                {
                    type: FormPromptType.Select,
                    key: "reasonLost",
                    label: "Please select one of the following reasons:",
                    selected: "other",
                    options: [
                        { label: "Spread", value: "spread" },
                        { label: "Fees", value: "fees" },
                        { label: "Other", value: "other" }
                    ]
                },
                {
                    type: FormPromptType.Text,
                    key: "additionalInformation",
                    label: "Please enter any additional information:"
                }
            ],
            metadata: {
                id: "reason-closed-form"
            }
        };
    
        return PromptPowers.sendDialogueResponseWithForm(dialogueResponse, formOptions);
    }

    Parameters

    Returns Promise<ISkillActivity>

Static sendDialogueResponseWithPrompt

  • Sends a dialogue response along with a prompt for the user to respond to. Responses to the prompt will return back to the same skill that sent the prompt.

    throws

    {Error} When there are no choices on the prompt, the choice IDs are not unique, or if there is no text to display on a choice for a single/multi-choice prompt.

    example

    This is an example of sending a Choice prompt as part of a Dialogue.

    //Ensure you import PromptPowers
    //import { DialogueResponsePowers, DialogueResponse, DialogueTag, DialoguePrompt, PromptPowers } from "@andi/powers";
    
    //The first prompt requests a user to select the first number
    function getPaybackChoicePrompt(): Promise<ISkillActivity> {
        const dialogueResponse: DialogueResponse = { message: "Is the client going to pay the money back?", tags: [] };
    
        const choiceOne: Choice = { choiceId: ChoiceIds.MoneyBackChoiceYes, text: "Yes" };
        const choiceTwo: Choice = { choiceId: ChoiceIds.MoneyBackChoiceNo, text: "No", clickedStyle: ClickedStyle.negative };
        const choiceThree: Choice = { choiceId: ChoiceIds.MoneyBackChoiceNotSure, text: "Not Sure" };
        const dialoguePrompt: DialoguePrompt = { promptType: PromptType.singleChoice, availableChoices: [choiceOne, choiceTwo, choiceThree] };
    
        return PromptPowers.sendDialogueResponseWithPrompt(dialogueResponse, dialoguePrompt);
    }

    Parameters

    • dialogueResponse: DialogueResponse

      Contains the primary message shown to the user and any tags to send as part of the prompt.

    • dialoguePrompt: DialoguePrompt

      Contains:

      1. promptType -- The type of prompt (single choice, multiple choice, or free text)
      2. promptStyle -- A style to be applied to the prompt if applicable. This field is optional; if not set, the style will be determined automatically
      3. availableChoices -- The list of choices the user can respond with. Each choice should have a unique ID to identify the choice when the response comes back to the skill. The choice should also contain the text that will be shown to the user for single and multi-choice prompts, and an optional styling to be applied if applicable.

    Returns Promise<ISkillActivity>

Generated using TypeDoc