Building the FAQs Activity

Overview

Now that we have a Sheet to support the FAQ activity, we can build that activity. The activity itself is not that complex. It is comprised of just 8 steps. However, it may be a little daunting to some users and requires essential work with the LUA scripting language. If that's not you, you will want a developer to help out.

Building the FAQ Activity

Add A New Activity

Click the 'New Activity' button to launch the Activity marketplace. Select the 'Blank activity' template and then add it to your Flow.

Rename the activity to 'FAQs'

Add the Steps

Add a Step and Name it FAQs Start

This step aims to determine if the user has already asked a question. If they have not, it will transition them to a step where they are prompted to ask their question. Otherwise, they will be routed to have their question processed, and, hopefully, answered!

This step will have no message for the user because it is used to transition the user to one of two other steps.

  • Toggle off the Wait for the user to execute actions
  • Add a Code action. The code you will use is below:
-- Check if userQuestion exists in the Context

if Context.check("userQuestion") then
    -- If the user has already asked a question then search for an answer
    Workflow.branch("SEARCH FOR AN ANSWER STEP ID")
else
    -- If the user has not already asked a question then get the user question
    Workflow.branch("GET USER QUESTION STEP ID")
end
478

FAQs Start Step side panel detail

  • Save the FAQs Start step, we will come back and complete this code after we build a few more steps.

Add the "Get user question step"

Add another step to the canvas and name it "Get user question
In the side panel, enter a message that prompts the user for their question.

  • Next, add a Variable action.
  • In the left-hand dropdown, add a variable named userQuestion
  • In the right-hand dropdown, select {{userMessage}}
    This will store the user's question in a variable called {{userQuestion}}
  • Add a Go to action, and in the dropdown, type in Search for an answer. Because this step does not yet exist, use the Add step button to create the new step
  • Save the Get user question step
478

FAQ Activity - Get User Question Side Panel Detail

Configure the "Search for an answer" step

Like FAQ Start, this step's job is to process information and transition the user to an appropriate step.
In this case, the step will use semantic search to compare the user's question against the list of FAQs and send them to one of three steps based on the search results.

  • Select the Search for an answer step on the canvas
  • Toggle off Wait for the user to execute actions
  • Add a code action
  • Enter the code below into the code editor
-- Enter sheet ID
sheetId = 'ENTER SHEET ID HERE'

-- Run FAQ search
result = FAQ.searchQuestion(Context.get('userQuestion'), sheetId)

-- If a single answer is found
if result.singleMatch then
    Context.set("FAQsMatchQuestion", result.matchQuestion)
    Context.set("FAQsMatchAnswer", result.matchAnswer)
    Workflow.branch("FOUND ANSWER STEP ID")
end

-- If multiple answers are found
if result.multipleMatch then 
    Context.set("FAQsQuestions", result.faqs)
    Workflow.branch("FOUND MULTIPLE ANSWERS STEP ID")
end

-- If no answers are found
if result.noMatch then 
    Workflow.branch("FOUND NO ANSWERS STEP ID") 
end
  • Save the step
  • Go to the sheets tab and copy your sheet ID
699

FAQ Activity - Sheet ID

  • Go back to the Flow canvas and the Search for an answer step
  • update the sheetId variable value from "ENTER YOUR SHEET ID HERE" to the ID you copied from your sheet
    Example
    sheetId='61e87cc34b7680865bf32afc'
820

FAQs Activity - Search for Answer Code Detail

  • Save the "Search for an answer" step. We will come back and configure the code after we create a few more steps.

Finish Configuring the "FAQ Start" Step

Now that we have created the Get user question step and the Search for an answer step, we can finish configuring the FAQ Start step. We are configuring the branching or transitions based on whether or not the Flow already has a question saved in userQuestion.

Every step in a Flow has a unique, numerical ID, and this is the information we need for the Workflow.branch() functions.

1094

FAQs Activity - Finding Step IDs

  • Look at your Get user question step. There will be a numerical ID on the step. Note this ID. Do the same for the Search for an answer step. In this example, the relevant step IDs are 21 and 22

  • Select the FAQ Start step and expand the code editor for the Code action we created earlier.

-- Check if userQuestion exists in the Context

if Context.check("userQuestion") then
    -- If the user has already asked a question then search for an answer
    Workflow.branch(22)
else
    -- If the user has not already asked a question then get the user question
    Workflow.branch(21)
end
  • On line 5, update Workflow.branch() with the step ID for Search for an answer. In the example, that Step ID is 22
  • On line 8, update Workflow.branch() with the step ID for Get user question. In the example, that Step ID is 21
  • Save the step
478

FAQs Activity - Search for Answers Side Panel

Create the "Found multiple answers" Step

  • Add a new step and name it "Found multiple answers"
  • Add the following text to a message
We found the following questions that might help you 👇

{{#each FAQsQuestions}}
	{{index}}) {{title}}
{{/each}}

Please, type the number of the question you want answered.

Feel free to update the copy to match your own, unique voice.
The handlebars code will create a numbered list of the answers found for the user to select from.

  • Add a Code action to evaluate the user response and transition the user
-- Get user message
message = Context.get("userMessage")
--Debug.print("message" .. message)

-- Get only the number from the user message
-- "I want option 2" returns "2"
selection = String.onlyNumbers(message)
--Debug.print("selection" .. selection)
-- Get questions saved in previous step
questions = Context.get("FAQsQuestions")
--Debug.print(questions)

-- Ensure the Context variable exists
if Context.check("FAQsQuestions") then
    -- Convert JSON to Lua Table (Object)
    questions = JSON.decode(questions)
    --Debug.print(questions)
    
    -- Loop through the questions searching if the number entered by user
    -- matches any of the questions
    for key, question in pairs(questions) do
        -- If question index matches number entered by user
        if question.index == selection then
            Context.set("FAQsMatchId", question.id)
            Context.set("FAQsMatchQuestion", question.title)
            Context.set("FAQsMatchAnswer", question.content)
            --Debug.print("going to answer")
            Workflow.branch("FOUND ANSWER STEP ID") 
        end
    end
end
  • Save the step; you will finish configuring it in a later step
455

FAQs Activity - Found Multiple Answers Messages

455

FAQs Activity - Found Multiple Answers Actions

Add the "Found answer" Step

  • Add a new Step and name it "Found answer"
  • Enter the following text in the message field
Q: {{FAQsMatchQuestion}}

A: {{FAQsMatchAnswer}}

The copy above uses handlebar notation to write out the relevant question and answer.

  • Add "Buttons" to the message to determine if the user's question was answered or not

  • Save the step now, you will finish configuring it in a later step

455

FAQs Activity - Found Answer Messages

455

FAQs Activity - Found Answer Actions

Create the "Found no answers" Step

  • Create a new step and name it "Found no answers"
  • In the Message field, write a message that lets the user know that a suitable answer was not found, and they can either go back to the menu or ask a new question.

We didn't find an answer to your question. Please, try again with a different question or type 'exit' to go back to the menu

  • Add a Code action to delete the previous question from the user's context
Context.delete("userQuestion")
  • Add a Conditional action to determine if the user wants to exit back to the menu
  • Use the Contains keywords condition and use a value of exit
  • If the user did type exit then transition them (via the go-to) back to the menu
  • Enable the fallback (if no condition was met) and add a Save variable action to save the new user question
  • Set {{userMessage}} to {{userMessage}} to save the current message from the user as the new question
  • Transition the user back to the Search for an answer step
  • Save the Found no answer step
455

FAQs Activity - Found No Answer Message

455

FAQs Activity - No Answer Found Actions

Finish Configuring the "Found multiple answers" Step

  • Get the step ID for the "Found answer" step
  • Select the Found multiple answers step
  • Update the code action with the step ID for the "Found answer" step - 31 in the case of our example Flow
-- Get user message
message = Context.get("userMessage")
--Debug.print("message" .. message)

-- Get only the number from the user message
-- "I want option 2" returns "2"
selection = String.onlyNumbers(message)
--Debug.print("selection" .. selection)
-- Get questions saved in previous step
questions = Context.get("FAQsQuestions")
--Debug.print(questions)

-- Ensure the Context variable exists
if Context.check("FAQsQuestions") then
    -- Convert JSON to Lua Table (Object)
    questions = JSON.decode(questions)
    --Debug.print(questions)
    
    -- Loop through the questions searching if the number entered by user
    -- matches any of the questions
    for key, question in pairs(questions) do
        -- If question index matches number entered by user
        if question.index == selection then
            Context.set("FAQsMatchId", question.id)
            Context.set("FAQsMatchQuestion", question.title)
            Context.set("FAQsMatchAnswer", question.content)
            --Debug.print("going to answer")
            Workflow.branch(24) 
        end
    end
end
  • Save the step

Create Steps to Capture FAQs Feedback

  • Select the Found answer step
  • Add a Condition action
  • Use Contains keywords to evaluate if the user selected the Yes button and transition them to a new step called Successfully answered
  • Add another Condition action
  • Use Contains keywords to evaluate if the user selected the No button and transition them to a new step called Unsuccessfully answered
  • Save the step
  • Arrange the new steps on your canvas, so they are visible and easily selectable

Configure the "Successfully answered" Step

  • Confirm for the user that the Flow received their response by responding with a positive message
    Glad we could help you!

  • Add a button message that allows the user to go back to the menu or ask another question

  • Add a code action to delete the current userQuestion variable

Context.delete("userQuestion")
  • Add a Contains keywords Condition to transition the user to the menu if they selected the menu button
  • Enable the fallback and use Save Variable to store the current {{{userMessage}} in {{userQuestion}} and transition the user back to Search for an answer
  • Save the step
455

FAQs Activity - Question Successfully Answered Messages

455

FAQs Activity - Question Successfully Answered Actions

Configure the "Unsuccessfully answered" Step

  • Confirm for the user that the Flow received their response by responding with a positive message
    Sorry we could not find an answer to your question. 😔
  • Add a button message that allows the user to go back to the menu or ask another question
  • Add a code action to delete the current userQuestion variable
  • Add a Contains keywords Condition to transition the user to the menu if they selected the menu button
  • Enable the fallback and use Save Variable to store the current {{{userMessage}} in {{userQuestion}} and transition the user back to Search for an answer
  • Save the step

Add a Fallback Step for the Found multiple answers step

Sometimes, a user will do something unanticipated. In this case, a user may send a message that does not match one of the available answers in Found multiple answers, we want to create a fallback that re-routes the user back to the Found multiple answers step.

  • Select the Found multiple answers step
  • Add a Contains keywords Condition
  • Grab the Condition handle and drag it above the Code action (remember, actions are processed from top to bottom, and we want this condition to be evaluated before the Code action)
  • Set the contains keyword value to 'none' and transition the user to the Unsuccessfully answered step
  • Enable the Fallback action
  • Create a new step called "FAQs no valid option fallback" via the Go To inside the Fallback action
  • Save the step

Configure the "FAQs no valid option fallback" Step

  • Select the FAQs no valid option fallback step
  • Enter a message letting the user that their message didn't match any of the listed answers
  • Switch the Wait for the user to execute actions toggle to off
  • Add a Go To action that transitions the user back to the Found multiple answers step
  • Save the step

Don't forget to connect your new FAQs activity to the rest of the Flow via its menu and/or a global action

Publish your Flow and test the FAQs activity

478

FAQs Activity - No Valid Option Provided Step Side Panel

Considerations