Symphony Developer - building a chat bot using BDK 2.0

Note: Before beginning the steps in this article, you must first complete the steps in Symphony Developer - Configure a Bot for BDK 2.0

 

BDK 2.0 is a library of tools and intelligent API bindings that provides a 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. 

 

First, open up the code generated by the Symphony Bot Generator in an Integrated Development Environment (IDE) of your choice, and then navigate to the BotApplication.java file:

BotApplication.java
package com.symphony.java;

import com.symphony.bdk.core.SymphonyBdk;
import com.symphony.bdk.core.service.datafeed.RealTimeEventListener;
import com.symphony.bdk.core.service.message.model.Message;
import com.symphony.bdk.gen.api.model.V4Initiator;
import com.symphony.bdk.gen.api.model.V4UserJoinedRoom;
import com.symphony.bdk.template.api.Template;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.symphony.bdk.core.config.BdkConfigLoader.loadFromClasspath;
import static com.symphony.bdk.core.activity.command.SlashCommand.slash;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;

/**
* Simple Bot Application.
*/
public class BotApplication {

/** The Logger */
private static final Logger log = LoggerFactory.getLogger(BotApplication.class);

public static void main(String[] args) throws Exception {

// Initialize BDK entry point
final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml"));

// Register a "slash" activity
bdk.activities().register(slash("/gif", false, context -> {
Template template = bdk.messages().templates().newTemplateFromClasspath("/templates/gif.ftl");
bdk.messages().send(context.getStreamId(), Message.builder().template(template).build());
}));

// Register a "formReply" activity that handles the Gif category form submission
bdk.activities().register(new GifFormActivity(bdk.messages()));

// Subscribe to 'onUserJoinedRoom' Real Time Event
bdk.datafeed().subscribe(new RealTimeEventListener() {

@Override
public void onUserJoinedRoom(V4Initiator initiator, V4UserJoinedRoom event) {
final String userDisplayName = event.getAffectedUser().getDisplayName();
Template template = bdk.messages().templates().newTemplateFromClasspath("/templates/welcome.ftl");
bdk.messages().send(event.getStream(),
Message.builder().template(template, singletonMap("name", userDisplayName)).build());
}
});

// finally, start the datafeed read loop
bdk.datafeed().start();
}
}

 

This code leverages the Activities API to register a new slash command ("/gif"). While this functionality is provided 'out of the box', a new datafeed listener will be built here for simplicity

 

To build a conversational workflow, add the onMessageSent datafeed listener to the main bot class:

bdk.datafeed().subscribe(new RealTimeEventListener() {
@Override
public void onMessageSent(V4Initiator initiator, V4MessageSent event) {
if (event.getMessage().getMessage().contains("/hello")){
bdk.messages().send(event.getMessage().getStream(), String.format("<messageML>Hello %s </messageML>", initiator.getUser().getDisplayName()));
}
}
});


The code above is listening for all incoming messages that contain "/hello".
If the message received by the bot contains "/hello", the bot will respond by sending a message to the stream or conversation (Figure 1):

 

1.png

Figure 1 Bot Response

 

Note: See a full list of datafeed listeners provided by the BDK 2.0 here

 

As demonstrated, the V4Initiator and V4MessageSent classes provide simple and convenient methods that make it easy to orchestrate workflows.

 

See a more detailed overview of how to leverage BDK 2.0 message, room and user management classes here.