Skip to main content

Apply Opt-in Flow to Chatbot

Opt-in is a very important safeguard for enterprise, especially when the brand is running a marketing campaign with prizes. In most cases, users will need to opt-in or agree to a set of terms & conditions. A well-written terms will clear any disputes originated from the chatbot game because it set the expectation, rules & regulations for players or users.

Another important use case is to send WhatsApp Template Message from WABA to users. The rules state that a business must get the consent from their users before sending any push messages. For details, you may refer to the opt-in requirement in WhatsApp Overview.

These use cases require an opt-in conversation flow, a record of the opt-in user data, as well as a way to filter out who is opted-in in our member page & push panel.

The following object(s) and method(s) will be used in the application of data source:

Expected Outcome

There are 4 types of expected outcome:

  1. User agrees on the terms of service and redirected to the starting message of the chatbot service. At the same time, the user is tagged with "Agree TNC" and also has a record on the Opt-in Record data source.
User opts-in
User opts-in
  1. Any users who accepted the T&C previously would not be asked to agree to the terms of service the next time they greet the chatbot.
User greets chatbot again
User greets chatbot again
Member tag Accept TNC is added to user
Member tag Accept TNC is added to user
User is recorded in the Opt-in Record Data Source
User is recorded in the Opt-in Record Data Source
  1. User disagrees on the terms of service and redirected back to the T&C message.
User opts-out
User opts-out
  1. User types something randomly to trigger the fail safe and redirected back to the T&C message.
User triggers failsafe
User triggers failsafe

Sample Tree Structure

Opt-in Flow Sample Tree Structure
Opt-in Flow Sample Tree Structure

Create a Tree Node - TNC

Enter the Bot Builder here to create your tree node.
  1. Create a Tree Node and rename it as "TNC".

  2. Create a Response for stating the terms of service and define what constitutes as agreement to the T&C.

Response with T&C
Response with T&C
  1. Save the Tree Node.

Create a Data Source - Opt-in Record

  1. Create a new data source.
Add a New Data Source
Add a New Data Source
  1. Import this sample Opt-in Record to the Data Source.
Opt-in Record
Opt-in Record
  1. Copy the Data Source ID.

Create a Tree Node - TNC Agree

  1. Create a Tree Node and rename it as "TNC Agree".

  2. Create a Trigger for detecting keywords that contitutes as agreement. Rename the Trigger as "Agree Only". You may take reference to the following condition:

return this.messageEvent.type === "TEXT" && /^(1|agree|εŒζ„|y)$/i.test(this.messageEvent.data.text)
Trigger for Agreement
Trigger for Agreement
  1. Create the 1st Pre-action for setting the status of user agreeing to T&C in the member data. Rename it as "Set User Agree TNC Status". You may refer to the following code:
return new Promise((resolve) => {
this.lodash.set(this.member, "tags", this.lodash.get(this.member, "tags", []).filter(tag => tag !== "Disagree TNC"))
this.lodash.set(this.member, "botMeta.tempData.userAgreeTNC", true)

resolve({
member: this.member
})
})
Set User Status to Agree T&C
Set User Status to Agree T&C
tip

Please make sure the tag in the array (i.e. Disagree TNC) is the same as the one set in Create a Tree Node - TNC Disagree.

  1. Create the 2nd Pre-action for upserting the user opt-in record to the Data Source. You may refer to the following code:
return new Promise(async(resolve) => {
await this.upsertDataToDataSource({
dataSourceId: "Insert Data Source ID Here",
data: {
"Member ID": this.member._id,
"Name": this.member.firstName,
"Whatsapp Number": this.member.externalId,
"Opt-in": "Y"
}
})

resolve({
member: this.member
})
})
Upsert to Opt-in Record Data Source
Upsert to Opt-in Record Data Source
tip

Please remember to set the dataSourceId with the Data Source ID you created for Opt-in Record. You can change the data's naming (i.e. the array of data, e.g. "Member ID") to fit your Data Source column headers too.

  1. Create a response for stating to users that the opt-in has been completed.
Opt-in Success Message
Opt-in Success Message
  1. You may choose to toggle on "Redirect" back to the start of chatbot service, so that user can continue the conversation seamlessly.
Redirect to Start of Chatbot Service
Redirect to Start of Chatbot Service
  1. Toggle on "Member Tagging" and set the tag as "Agree TNC".
Set Member Tag as Agree TNC
Set Member Tag as Agree TNC
  1. Save the Tree Node.

Create a Tree Node - TNC Disagree

  1. Create a Tree Node and rename it as "TNC Disagree".
Trigger for Disagreement
Trigger for Disagreement
  1. Create a Trigger for detecting keywords that constitutes as disagreement. Rename the Trigger as "Disagree Only". You may take reference to the following condition:
return this.messageEvent.type === "TEXT" && /^(2|disagree|δΈεŒζ„|n)$/i.test(this.messageEvent.data.text)
  1. Create a response for stating to users that the opt-in has failed.
Opt-in Fail Message
Opt-in Fail Message
  1. You may choose to toggle on "Redirect" back to the start of the opt-in flow, so that user can decide for the second time.
Redirect to TNC
Redirect to TNC
  1. Toggle on "Member Tagging" and set the tag as "Disagree TNC".
Set Member Tag as Disagree TNC
Set Member Tag as Disagree TNC
  1. Save the Tree Node.

Create a Tree Node - TNC failsafe

  1. Create a Tree Node and rename it as "TNC failsafe".

  2. Create a Trigger for any text, any payload or any media. Rename the Trigger as "Type all".

Trigger for Any Text, Payload & Media
Trigger for Any Text, Payload & Media
  1. Create a response for stating to users that the chatbot does not comprehend user's input.
Fail Safe Message
Fail Safe Message
  1. Toggle on "Redirect" back to the start of the opt-in flow, so that user can input for the second time.
Redirect to TNC
Redirect to TNC
  1. Save the Tree Node.

Create a Global Node - Greetings Keywords

  1. Create a Global Node and rename it as "Greetings Keywords". This Global Node is to determine if the user has opted-in before. If the member tag "Agree TNC" is present, it means the user has opted in and then the system will redirect user to the start of chatbot service. Otherwise, the system will redirect user to the Tree Node - "TNC".

  2. Create a Trigger for detecting greetings keywords. Rename the Trigger as "Text: Greetings".

Trigger for Greetings User Input
Trigger for Greetings User Input
  1. Toggle on "Redirect" and copy the following code to the "Advanced" tab:
return new Promise (resolve => {
const memberTagArray = this.lodash.get(this.member, "tags", [])
if (memberTagArray.includes("Agree TNC")) {
resolve({
tree: "Insert Tree ID here or this.node.tree", // Tree ID of the start of chatbot service
nodeCompositeId: "Insert Node Composite ID Here", // Node Composite ID of the start of chatbot service
runPreAction: true,
sendResponse: true,
runPostAction: true,
})
}
resolve({
tree: this.node.tree,
nodeCompositeId: "Insert Node Composite ID Here", // Node Composite ID of TNC
runPreAction: true,
sendResponse: true,
runPostAction: true,
})
})

Redirect User Based on Opt-in Status
Redirect User Based on Opt-in Status
tip

For this section, you may set which Tree Node the chatbot should redirect to. Insert a Tree ID if it is not in the same Opt-in Flow tree. Otherwise, type this.node.tree.

  1. Save the Global Node and see if you can produce the expected outcome.