Building an Interactive Bot using the BDK 2.0

Prerequisites

Complete the BDK 2.0 Bot Configuration guide:

1. Dive into the Code

The BDK 2.0 is a library of tools and intelligent API bindings that provides an ultra simplified configuration and authentication setup, intuitive message and room management, customizable message templating, and a new activities API that makes it easy to facilitate bot workflows. The BDK 2.0 and bot project generated by the Symphony Bot Generator makes it super easy to get started!
To begin let's open up the code generated for you by the Symphony Bot Generator in your favorite IDE. Navigate to the BotApplication.java file:
BotApplication.java
1
package com.symphony.java;
2
3
import com.symphony.bdk.core.SymphonyBdk;
4
import com.symphony.bdk.core.service.datafeed.RealTimeEventListener;
5
import com.symphony.bdk.core.service.message.model.Message;
6
import com.symphony.bdk.gen.api.model.V4Initiator;
7
import com.symphony.bdk.gen.api.model.V4UserJoinedRoom;
8
import com.symphony.bdk.template.api.Template;
9
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
13
import static com.symphony.bdk.core.config.BdkConfigLoader.loadFromClasspath;
14
import static com.symphony.bdk.core.activity.command.SlashCommand.slash;
15
import static java.util.Collections.emptyMap;
16
import static java.util.Collections.singletonMap;
17
18
/**
19
* Simple Bot Application.
20
*/
21
public class BotApplication {
22
23
/** The Logger */
24
private static final Logger log = LoggerFactory.getLogger(BotApplication.class);
25
26
public static void main(String[] args) throws Exception {
27
28
// Initialize BDK entry point
29
final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml"));
30
31
// Register a "slash" activity
32
bdk.activities().register(slash("/gif", false, context -> {
33
Template template = bdk.messages().templates().newTemplateFromClasspath("/templates/gif.ftl");
34
bdk.messages().send(context.getStreamId(), Message.builder().template(template).build());
35
}));
36
37
// Register a "formReply" activity that handles the Gif category form submission
38
bdk.activities().register(new GifFormActivity(bdk.messages()));
39
40
// Subscribe to 'onUserJoinedRoom' Real Time Event
41
bdk.datafeed().subscribe(new RealTimeEventListener() {
42
43
@Override
44
public void onUserJoinedRoom(V4Initiator initiator, V4UserJoinedRoom event) {
45
final String userDisplayName = event.getAffectedUser().getDisplayName();
46
Template template = bdk.messages().templates().newTemplateFromClasspath("/templates/welcome.ftl");
47
bdk.messages().send(event.getStream(),
48
Message.builder().template(template, singletonMap("name", userDisplayName)).build());
49
}
50
});
51
52
// finally, start the datafeed read loop
53
bdk.datafeed().start();
54
}
55
}
Copied!
Let's take a closer look at the code responsible for sending Symphony Elements in our BotApplication.java:
1
// Register a "slash" activity
2
bdk.activities().register(slash("/gif", false, context -> {
3
Template template = bdk.messages().templates().newTemplateFromClasspath("/templates/gif.ftl");
4
bdk.messages().send(context.getStreamId(), Message.builder().template(template).build());
5
}));
6
7
// Register a "formReply" activity that handles the Gif category form submission
8
bdk.activities().register(new GifFormActivity(bdk.messages()));
Copied!
Here we are using the Activities API to register a new slash command that listens to "/gif". To learn more about creating your own slash commands or how to leverage the Activities API, continue here. If an incoming message contains ("/gif), the bot builds a new message template which is provided out of the box for you:
resources/templates/gif.ftl
1
<messageML>
2
<h2>Gif Generator</h2>
3
<form id="gif-category-form">
4
5
<text-field name="category" placeholder="Enter a Gif category..."/>
6
7
<button name="submit" type="action">Submit</button>
8
<button name="test" type="action">Test</button>
9
<button type="reset">Reset Data</button>
10
11
</form>
12
</messageML>
13
Copied!
The above freemarker template contains messageML that represents a Symphony Element. To learn more about Symphony Elements, continue here. When a message is sent to the bot containing "/gif" the following Symphony Element is sent to the user in the conversation or stream in context:
Bots need someway to capture the data submitted within this form. Bots can easily do so by registering a new type of Activity class:
1
// Register a "formReply" activity that handles the Gif category form submission
2
bdk.activities().register(new GifFormActivity(bdk.messages()));
Copied!
Open up your GifFormActivity class. Here you will see that GifFormActivity extends the FormReplyActivity class. A form activity is only triggered when an end-user replies or submits an Elements form. To learn more about creating your own FormReplyActivity classes, continue here.
1
public class GifFormActivity extends FormReplyActivity<FormReplyContext> {
2
3
private final MessageService messageService;
4
5
public GifFormActivity(MessageService messageService) {
6
this.messageService = messageService;
7
}
8
9
@Override
10
public ActivityMatcher<FormReplyContext> matcher() {
11
return context -> "gif-category-form".equals(context.getFormId())
12
&& "submit".equals(context.getFormValue("action"));
13
}
14
15
@Override
16
public void onActivity(FormReplyContext context) {
17
final String category = context.getFormValue("category");
18
final String message = "<messageML>Received category '" + category + "'</messageML>";
19
this.messageService.send(context.getSourceEvent().getStream(), Message.builder().content(message).build());
20
}
21
22
@Override
23
protected ActivityInfo info() {
24
return new ActivityInfo().type(ActivityType.FORM)
25
.name("Gif Display category form command")
26
.description("\"Form handler for the Gif Category form\"");
27
}
28
}
Copied!
Inside of the GifFormActivity class you will see an ActivityMatcher matcher() method:
1
@Override
2
public ActivityMatcher<FormReplyContext> matcher() {
3
return context -> "gif-category-form".equals(context.getFormId())
4
&& "submit".equals(context.getFormValue("action"));
5
}
Copied!
Using the context variable, your bot can access information about the context of the form submission including the form values and the form Id. To learn more about using FormReplyActivities, continue here.
Inside of the matcher() function, the bot is performing a validation check. If the formId of the submitted form is equal to "gif-category-form", then the bot calls the onActivity() trigger function and executes its business logic. If there is not a match, the bot does nothing and continues to listen for incoming events. As we see in our gif.ftl template above, the formId matches, so the following onActivity() trigger function executed:
1
@Override
2
public void onActivity(FormReplyContext context) {
3
final String category = context.getFormValue("category");
4
final String message = "<messageML>Received category '" + category + "'</messageML>";
5
this.messageService.send(context.getSourceEvent().getStream(), Message.builder().content(message).build());
6
}
Copied!

2. Run your Bot

Run your bot and execute the following to see it in action:

3. Next Steps

Above, we showed you a simple example of how to leverage the BDK 2.0 Symphony Elements to build an interactive bot. To understand more advanced usage of the BDK 2.0, continue on to our dedicated BDK 2.0 Certification course:
Last modified 10mo ago