-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
APP-3031 Activity API Documentation #202
Merged
thibauult
merged 6 commits into
finos:master
from
thibauult:APP-3031_Activity-API-Documentation
Sep 9, 2020
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50c2420
APP-3031 Activity API Documentation
thibauult 2b7fdba
APP-3031 Finalized Activity API Documentation
thibauult db8f208
APP-3031 Fixed @el-youness comments
thibauult a2821f6
APP-3031 Snyk issue: Fixed Jackson version in legacy BDK
thibauult 5c57f43
APP-3031 Trigger Snyk
thibauult 52993f9
Merge remote-tracking branch 'upstream/master' into APP-3031_Activity…
thibauult File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,172 @@ | ||
# Activity API | ||
|
||
The Activity API is an abstraction built on top of the Datafeed's [_Real Time Events_](https://developers.symphony.com/restapi/docs/real-time-events). | ||
An Activity is basically a user interaction triggered from the chat. | ||
|
||
At the moment, two different kinds of activities are supported by the BDK: | ||
The Activity API is an abstraction built on top of the Datafeed's [_Real Time Events_](https://developers.symphony.com/restapi/docs/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`, `MIM` or `Chatroom` | ||
- **Form Activity**: triggered when a user replies to an [_Elements_](https://developers.symphony.com/symphony-developer/docs/overview-of-symphony-elements) form message | ||
|
||
Using the Activity API will help you to make your bot interactions easier and faster to implement. | ||
## Activity Registry | ||
The central component for activities is the [`ActivityRegistry`](../symphony-bdk-core/src/main/java/com/symphony/bdk/core/activity/ActivityRegistry.java). | ||
This component is used to either add or retrieve activities. It is accessible from the `SymphonyBdk` object: | ||
|
||
```java | ||
public class Example { | ||
|
||
public static void main(String[] args) throws Exception { | ||
// Create BDK entry point | ||
final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); | ||
// Access to the registry for activities | ||
final ActivityRegistry registry = bdk.activities(); | ||
} | ||
} | ||
``` | ||
|
||
## Command Activity | ||
A command activity is triggered when a message is sent in an `IM`, `MIM` or `Chatroom`. | ||
A command activity is triggered when a message is sent in an `IM`, `MIM` or `Chatroom`. This is the most basic interaction | ||
between a end-user and the bot. Here are some command activity examples: | ||
|
||
``` | ||
$ @BotMention /buy # (1) | ||
$ /buy 1000 goog # (2) | ||
$ I want to say hello to the world # (3) | ||
``` | ||
1. the bot is mentioned followed by a [_slash_](#slash-command) command | ||
2. a command with parameters, the bot is not mentioned | ||
3. any message that contains 'hello' can be a command | ||
|
||
### How to create a Command Activity | ||
|
||
```java | ||
public class CommandActivityExample { | ||
public class Example { | ||
|
||
public static void main(String[] args) { | ||
// TODO | ||
public static void main(String[] args) throws Exception { | ||
// setup SymphonyBdk facade object | ||
final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("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; | ||
} | ||
} | ||
``` | ||
1. the [`ActivityMatcher`](../symphony-bdk-core/src/main/java/com/symphony/bdk/core/activity/ActivityMatcher.java) | ||
allows detecting if the activity logic has to be executed or not. In this case, it will execute `onActivity(CommandContext)` | ||
each time a message that contains "hello" is sent in a stream where the bot is also a member | ||
2. this is where the command logic has to be implemented | ||
3. define activity information | ||
|
||
### Slash Command | ||
A _Slash_ command can be used to directly define a very simple bot command such as: | ||
``` | ||
$ @BotMention /command | ||
$ /command | ||
``` | ||
|
||
#### How to create a Slash Command | ||
> :information_source: a Slash cannot have parameters | ||
|
||
```java | ||
public class SlashCommandExample { | ||
public class Example { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
// setup SymphonyBdk facade object | ||
final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("config.yaml")); | ||
|
||
public static void main(String[] args) { | ||
// TODO | ||
bdk.activities().register(new SlashCommand("/hello" /*(1)*/, true /*(2)*/, context /*(3)*/ -> { | ||
log.info("Hello slash command triggered by user {}", context.getInitiator().getUser().getDisplayName()); // (2) | ||
})); | ||
|
||
// finally, start the datafeed loop | ||
bdk.datafeed().start(); | ||
} | ||
} | ||
``` | ||
1. `/hello` is the command name | ||
2. `true` means that the bot has to be mentioned | ||
3. the command callback provides the | ||
|
||
## Form Activity | ||
A form activity is triggered when an end-user reply or submit to an _Elements_ form. | ||
|
||
### How to create a Form Activity | ||
For this example, we will assume that the following Elements form has been posted into a room: | ||
```xml | ||
<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> | ||
``` | ||
1. the formId is "**hello-form**" | ||
2. the form has one unique `<text-field>` called "**name**" | ||
3. the has one action button called "**submit**" | ||
|
||
```java | ||
public class FormActivityExample { | ||
public class Example { | ||
|
||
public static void main(String[] args) throws Exception { | ||
// setup SymphonyBdk facade object | ||
final SymphonyBdk bdk = new SymphonyBdk(loadFromSymphonyDir("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>"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
// TODO | ||
@Override | ||
protected ActivityInfo info() { | ||
final ActivityInfo info = ActivityInfo.of(ActivityType.FORM); | ||
info.setName("Hello Form Reply Activity"); | ||
return info; | ||
} | ||
} | ||
``` | ||
1. The `ActivityMatcher` here ensure that activity logic is triggered only when the form "**hello-form**" has been | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
submitted from action button "**submit**" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from the action button "submit" |
||
2. The activity context allows to directly retrieve form values. Here the "**name**" `<text-field>` value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably forgot to finish the sentence.