The Activity API is an abstraction built on top of the Datafeed's Real Time Events. An Activity is basically a user interaction triggered from the chat. Two different kinds of activities are supported by the BDK:
- Command Activity: triggered when a message is sent in an
IM,MIMorChatroom - Form Activity: triggered when a user replies to an Elements form message
The central component for activities is the ActivityRegistry.
This component is used to either add or retrieve activities. It is accessible from the SymphonyBdk object:
public class Example {
public static void main(String[] args) throws Exception {
// Create BDK entry point
final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml"));
// Access to the registry for activities
final ActivityRegistry registry = bdk.activities();
}
}A command activity is triggered when a message is sent in an IM, MIM or Chatroom. This is the most basic interaction
between an end-user and the bot. Here are some command activity examples:
- the bot is mentioned followed by a slash command:
$ @BotMention /buy
- a command with parameters, the bot is not mentioned:
$ /buy 1000 goog
- any message that contains 'hello' can be a command:
$ I want to say hello to the world
public class Example {
public static void main(String[] args) throws Exception {
// setup SymphonyBdk facade object
final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml"));
// register Hello Command within the registry
bdk.activities().register(new HelloCommandActivity());
// finally, start the datafeed loop
bdk.datafeed().start();
}
}
class HelloCommandActivity extends CommandActivity<CommandContext> {
@Override
protected ActivityMatcher<CommandContext> matcher() {
return c -> c.getTextContent().contains("hello"); // (1)
}
@Override
protected void onActivity(CommandContext context) {
log.info("Hello command triggered by user {}", context.getInitiator().getUser().getDisplayName()); // (2)
}
@Override
protected ActivityInfo info() {
final ActivityInfo info = ActivityInfo.of(ActivityType.COMMAND); // (3)
info.setName("Hello Command");
return info;
}
}- the
ActivityMatcherallows detecting if the activity logic has to be executed or not. In this case, it will executeonActivity(CommandContext)each time a message that contains "hello" is sent in a stream where the bot is also a member - this is where the command logic has to be implemented
- define activity information
A Slash command can be used to directly define a very simple bot command such as:
$ @BotMention /command
$ /command
ℹ️ a Slash cannot have parameters
public class Example {
public static void main(String[] args) throws Exception {
// setup SymphonyBdk facade object
final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml"));
bdk.activities().register(new SlashCommand("/hello", // (1)
true, // (2)
context -> { // (3)
log.info("Hello slash command triggered by user {}", context.getInitiator().getUser().getDisplayName());
}));
// finally, start the datafeed loop
bdk.datafeed().start();
}
}/hellois the command nametruemeans that the bot has to be mentioned- the command callback provides the
CommandContextthat allows to retrieve some information about the source of the event, or the event initiator (i.e. user that triggered the command)
A form activity is triggered when an end-user reply or submit to an Elements form.
For this example, we will assume that the following Elements form has been posted into a room:
<messageML>
<h2>Hello Form</h2>
<form id="hello-form"> <!-- (1) -->
<text-field name="name" placeholder="Enter a name here..."/> <!-- (2) -->
<button name="submit" type="action">Submit</button> <!-- (3) -->
<button type="reset">Reset Data</button>
</form>
</messageML>- the formId is "hello-form"
- the form has one unique
<text-field>called "name" - the has one action button called "submit"
public class Example {
public static void main(String[] args) throws Exception {
// setup SymphonyBdk facade object
final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml"));
// register Hello FormReply Activity within the registry
bdk.activities().register(new HelloFormReplyActivity(bdk.messages()));
// finally, start the datafeed loop
bdk.datafeed().start();
}
}
class HelloFormReplyActivity extends FormReplyActivity<FormReplyContext> {
private final MessageService messageService;
public HelloFormReplyActivity(MessageService messageService) {
this.messageService = messageService;
}
@Override
protected ActivityMatcher<FormReplyContext> matcher() {
return c -> "hello-form".equals(c.getFormId()) && "submit".equals(c.getFormValue("action")); // (1)
}
@Override
protected void onActivity(FormReplyContext context) {
final String message = "Hello, " + context.getFormValue("name") + "!"; // (2)
this.messageService.send(context.getSourceEvent().getStream(), "<messageML>" + message + "</messageML>");
}
@Override
protected ActivityInfo info() {
final ActivityInfo info = ActivityInfo.of(ActivityType.FORM);
info.setName("Hello Form Reply Activity");
return info;
}
}- The
ActivityMatcherensures that activity logic is triggered only when the form withid"hello-form" has been submitted from the action button "submit" - The activity context allows to directly retrieve form values. Here the "name"
<text-field>value