BDK 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 and bot project generated by the Symphony Generator makes it super easy to get started!
To begin let's open up the code generated for you by the Symphony Generator in your favorite IDE. Navigate to the BotApplication.java file:
Note:
As you can see below on line 32, we are leveraging the Activities API to register a slash command ("/gif"). While this functionality is provided out of the box, we will be building a new datafeed listener to keep it simple at first. For a more in depth look at the provided activity and Activities API, continue here.
packagecom.symphony.java;importcom.symphony.bdk.core.SymphonyBdk;importcom.symphony.bdk.core.service.datafeed.RealTimeEventListener;importcom.symphony.bdk.core.service.message.model.Message;importcom.symphony.bdk.gen.api.model.V4Initiator;importcom.symphony.bdk.gen.api.model.V4UserJoinedRoom;importcom.symphony.bdk.template.api.Template;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importstaticcom.symphony.bdk.core.config.BdkConfigLoader.loadFromClasspath;importstaticcom.symphony.bdk.core.activity.command.SlashCommand.slash;importstaticjava.util.Collections.emptyMap;importstaticjava.util.Collections.singletonMap;/** * Simple Bot Application. */publicclassBotApplication { /** The Logger */privatestaticfinalLogger log =LoggerFactory.getLogger(BotApplication.class);publicstaticvoidmain(String[] args) throwsException {// Initialize BDK entry pointfinalSymphonyBdk bdk =newSymphonyBdk(loadFromClasspath("/config.yaml"));// Register a "slash" activitybdk.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 submissionbdk.activities().register(newGifFormActivity(bdk.messages()));// Subscribe to 'onUserJoinedRoom' Real Time Eventbdk.datafeed().subscribe(newRealTimeEventListener() { @OverridepublicvoidonUserJoinedRoom(V4Initiator initiator,V4UserJoinedRoom event) {finalString 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 loopbdk.datafeed().start(); }}
To build a conversational workflow, add the following datafeed listener (onMessageSent) to your main bot class:
The above snippet is listening for all incoming messages that contain "/hello". To see a full list of datafeed listeners provided by the BDK, navigate here. If the message received by the bot contains "/hello", the bot will respond by sending a message to the stream or conversation in context. Run your bot and let's see it in action:
As you can see, it's super easy to access the message, room, and user context from an incoming event. The V4Initiator and V4MessageSent classes provide convenience methods that make it easy to orchestrate workflows. For a more detailed overview of how to leverage the BDK's message, room, and user management classes, continue here.
2. Next Steps
Above, we showed you a simple example of how to leverage the BDK to build a conversational bot. To see more advanced usage of the BDK in order to build complex financial workflows, continue on to our dedicated BDK Certification course: