Create Your First Bot

Introduction

In this tutorial, you will learn how to setup a chatbot that will qualify leads and sign them up to your Mailchimp contact list.
At the end of this tutorial, here is what you will be able to:
    Get information from the end-user
    Subscribe this user to an existing Mailchimp contact list
    Ask users for feedback on their experience, and send a custom answer based on their response

Writing your CSML flow

A CSML flow always starts in the start step, in this step, we introduce the chatbot. Note that Typing(2000) needs to be preceded by the say keyword, this will show the three little jumping dots on the screen, suggesting the chatbot is typing, this action will last 2 seconds (2000 milliseconds).
1
start:
2
say Typing(2000)
3
say "Hi πŸ€“, I just need a few informations in order to add you to my mailing list."
4
goto firstname
Copied!
We can then move on to the next step in which we will start asking for user informations.

Asking questions and remembering answers

With CSML, in order to wait for the user to reply to a question (or anything else for that matter), we use the keyword hold, the chatbot then waits for the user to reply. After answering, the user input will be placed in the event variable.
Note: the event variable is scoped to the current step, which basically means that event will be wiped off the memory once the chatbot goes out of the current step.
In order to remember what the user said, we can ask the chat to remember nlFirstname = event, and nlFirstname will then be accessible anywhere, anytime.
1
firstname:
2
say Typing(2000)
3
say "What's your firstname?"
4
hold
5
​
6
remember nlFirstname = event
7
goto lastname
Copied!
Let's move to the next step : lastname
First let's have a look at the Button(...) component: it has two parameters, a title="" that represents what will be displayed inside the button, and an accept=[] parameter that is a list of all the words that correspond to this button. This way, if a user types "NA", CSML will understand it as being the same as a click on this button. Lastly, we set this button as variable btnLastname.
Now that our button is saved, we then create a Question component, add the preset button and hold.
As the user answers, we check if the input matches the button or what it accepts ans remember the appropriate nlLastname.
1
lastname:
2
say Typing(2000)
3
do btnLastname = Button(
4
title = "I don't want to share my lastname",
5
accept = ["No", "No", "no", "NA", "N/A", "na", "n/a"]
6
)
7
​
8
say Question(title="What's your lastname?", buttons=[btnLastname])
9
hold
10
​
11
if (event.match(btnLastname)) remember nlLastname = "N/A"
12
else remember nlLastname = event
13
goto email
Copied!
The same goes with the email, only this time we're checking if the user input contains a @, if it doesn't we start the step again, otherwise we remember the email and move on to save everything.
1
email:
2
say Typing(2000)
3
say "What's your email address?"
4
hold
5
​
6
// does the user's input look like an email address?
7
if (event.is_email()) {
8
remember nlEmail = event
9
goto save
10
}
11
​
12
// otherwise the input is probably not a valid email address
13
// we need to ask them again
14
say "That's not a valid email address 😱"
15
goto email
Copied!

Sending data to Mailchimp

Thanks to our apps directory, you can install the Mailchimp app in a few clicks (you'll just need to get your API Region, API key and contact list ID). The app needs an option parameter that encloses all the informations that Mailchimp needs.
Note that App(...) needs to be used in a do statement.
1
save:
2
// prepare the arguments required by the mailchimp app
3
do options = {
4
"listId": "d8acdcc85f",
5
"email": nlEmail,
6
"firstname": nlFirstname,
7
"lastname": nlLastname,
8
}
9
// execute the app
10
do mailchimpResponse = App("mailchimp", action="subscribeToList", options=options)
11
​
12
say Typing(2000)
13
say "I have added you to our newsletter subscription list !"
14
​
15
goto feedback
Copied!
Now it's time to get some feedback from the user!

NLP coming into play

We want to use the SAP Conversational AI to find out if the user's input is positive, neutral or negative. The API returns this as sentiments. If you want to see the full response from the API, you can print it using say "{{sapcaiResponse}}"
1
feedback:
2
say "How did you like this newsletter onboarding?"
3
hold
4
​
5
// query the NLP service
6
do sapcaiResponse = App("sap/cai", text=event)
7
​
8
// this contains the sentiment analysis of the query
9
say sapcaiResponse.results.sentiment
10
​
11
if (Find("positive", in=sapcaiResponse.results.sentiment)) {
12
say "Thank you so much for your kind words πŸ˜‡."
13
}
14
else if (Find("negative", in=sapcaiResponse.results.sentiment)) {
15
say "I'd love to know what went wrong, please join our slack channel and let us know."
16
}
17
else {
18
say "I am sure you'll get to love CSML :D"
19
}
20
​
21
goto end
Copied!

That's it !

You've done it, you've created your first CSML bot from scratch ! Here is the full code:
1
start:
2
say Typing(2000)
3
say "Hi πŸ€“, I just need a few informations in order to add you to my mailing list."
4
goto firstname
5
​
6
​
7
firstname:
8
say Typing(2000)
9
say "What's your firstname?"
10
hold
11
​
12
remember nlFirstname = event
13
goto lastname
14
​
15
​
16
lastname:
17
say Typing(2000)
18
do btnLastname = Button(
19
title = "I don't want to share my lastname",
20
accept = ["No", "No", "no", "NA", "N/A", "na", "n/a"]
21
)
22
​
23
say Question(title="What's your lastname?", buttons=[btnLastname])
24
hold
25
​
26
if (event.match(btnLastname)) remember nlLastname = "N/A"
27
else remember nlLastname = event
28
goto email
29
​
30
​
31
email:
32
say Typing(2000)
33
say "What's your email address?"
34
hold
35
​
36
// does the user's input look like an email address?
37
if (event.is_email() {
38
remember nlEmail = event
39
goto save
40
}
41
​
42
// otherwise the input is probably not a valid email address
43
// we need to ask them again
44
say "That's not a valid email address 😱"
45
goto email
46
​
47
​
48
feedback:
49
say "How did you like this newsletter onboarding?"
50
hold
51
​
52
// query the NLP service
53
do sapcaiResponse = App("sap/cai", text=event)
54
​
55
// this contains the sentiment analysis of the query
56
say sapcaiResponse.results.sentiment
57
​
58
if (Find("positive", in=sapcaiResponse.results.sentiment)) {
59
say "Thank you so much for your kind words πŸ˜‡."
60
}
61
else if (Find("negative", in=sapcaiResponse.results.sentiment)) {
62
say "I'd love to know what went wrong, please join our slack channel and let us know."
63
}
64
else {
65
say "I am sure you'll get to love CSML :D"
66
}
67
​
68
goto end
69
​
70
​
71
save:
72
// prepare the arguments required by the mailchimp app
73
do options = {
74
"listId": "d8acdcc85f",
75
"email": nlEmail,
76
"firstname": nlFirstname,
77
"lastname": nlLastname,
78
}
79
// execute the app
80
do mailchimpResponse = App("mailchimp", action="subscribeToList", options=options)
81
​
82
say Typing(2000)
83
say "I have added you to our newsletter subscription list !"
84
​
85
goto feedback
Copied!
If you have any question, please come ask us on Slack!
Last modified 7mo ago