Bot Developer Kit for Python
Overview
The BDK for Python is Symphony's preferred tooling for Python developers to build bots. It is a library of tools and API bindings that provides simplified configuration and authentication, intuitive message and room management, customizable message templating and an activities API that makes it easy to build bot workflows.
The BDK for Python Github repo can be found here: @finos/symphony-bdk-python
The BDK for Python Certification course can be found here: learn.symphony.com/bundles/python-bot-developer
Getting Started
Getting Started with BDKAuthentication
Authenticating your bot is made simple when using the BDK for Python. Once you have your bot and Symphony environment properly configured, the generated code provides an out of the box implementation for authenticating your bot:
By instantiating a new SymphonyBdk
instance with your config.yaml
file, the BDK loads in your config and authenticates your bot. Once authenticated, your bot is ready to leverage the REST APIs in order to create rich automations and workflows on Symphony.
Note: You must have a corresponding service or bot account setup on your Symphony instance before authenticating. For more information navigate to the Creating a Bot User guide.
OBO Authentication
BDK for Python also supports OBO (On-Behalf-Of) pattern of authentication, allowing an authenticated bot + extension application to perform operations on behalf of a given user. The BDK's implementation makes it easy to perform the following operations on behalf of a given user:
List the streams of a given user
Initiate connection requests to and determine connection status with other users
Get the presence state of other connected users
Initiate IMs and MIMs with other users
Send messages and attachments
Set the context user's own presence
Please follow our 'Getting Started with OBO' guide using the link here.
The guide will cover all of the prerequisites needed for OBO and how to enable & upload the OBO extension application, the required permissions and how to ensure the OBO authentication process will work successfully.
To leverage an OBO based workflow, simply instantiate an OBO Session in your bot project. The BDK allows you to instantiate your OBO session from a username or user ID. Once authenticated bots can perform any of the OBO workflows listed above:
Managing Multiple Bots
BDK for Python makes it easy to manage multiple bot instances within a single project. As long as you have unique configuration files that correspond to different service accounts, you can manage multiple bot instances from a centralized source. To do so, simply instantiate multiple bot instances of the SymphonyBDK
class within your bot project:
Datafeed Management
The BDK also provides a DatafeedService
interface that makes it easier than ever for bots to manage real-time messages and events. The DatafeedService
interface provides the following methods for your bot to use:
Method | Descriptions |
| Start the bot's datafeed |
| Stop the bot's datafeed |
| Subscribe a custom event listener class. Inside this class is where the bulk of your business logic goes. |
| Unsubscribe from a custom event listener class. |
For bots to listen to incoming events and messages, bots must subscribe to a custom RealTimeEventListener
. This RealTimeEventListener
class must implement eventType methods (e.g. onMessageSent()
) along with custom business logic inside.
When a user sends a bot a message, the bot will pick up the event from the datafeed and check to see if an implemented eventType method matches the eventType (MESSAGESENT
) of the inbound event. If there is a corresponding eventType method registered, the bot will execute the business logic inside of this eventType method. Otherwise the bot will not perform an action and will continue to listen for inbound events from the datafeed. An example implementation is provided out of the box by the BDK:
Below is a full list of methods provided by the RealTimeEventListener
class and their corresponding eventTypes. Implement the following methods in order to listen for a given Symphony event:
Method | Event Type |
| MESSAGESENT |
| INSTANTMESSAGECREATED |
| MESSAGESUPPRESSED |
| ROOMCREATED |
| ROOMUPDATED |
| ROOMDEACTIVATED |
| ROOMACTIVATED |
| USERREQUESTEDTOJOINROOM |
| USERJOINEDROOM |
| USERLEFTROOM |
| ROOMMEMBERPROMOTEDTOOWNER |
| ROOMMEMBERDEMOTEDFROMOWNER |
| CONNECTIONREQUESTED |
| CONNECTIONACCEPTED |
| SYMPHONYELEMENTSACTION |
| SHAREDPOST |
For more information on the Symphony datafeed continue here:
DatafeedOrchestrating Workflows with BDK for Python
A Symphony workflow can be thought of as a sequence of operations or a repeatable pattern of activities that are organized together in order to transform data, provide a service, or process information. Each of these operations or activities may be completed by a single user, shared between a bot and a user, or shared between multiple actors including bots, users, and even third party systems.
By providing an intuitive Activities API, the BDK for Python makes it simple to define a set of discrete operations or activities for different actors in your system to execute. Ultimately, these activities constitute the building blocks for a powerful Symphony workflow automation.
Once you have defined a discrete set of activities for different actors in your system to execute, the next step is to organize them together in an intelligent way.
Activities API
BDK for Python provides an Activities API, an interface that makes it easy to manage user-to-bot interactions or activities. Specifically, the Activities API provides easy access to message and room context, initiator metadata, and an intuitive way to interact with the datafeed, making it easy for bots to listen and reply to different Symphony events. The methods and logic provided by the Activities API allows for granular control over the entire user-to-bot interaction. This encapsulated logic is easily reused, forming the discrete building blocks of a Symphony workflow automation.
Registering Activities
In order to register activities for your bot instance, you must leverage the ActivityRegistry
class:
There are two different types of activities supported by the BDK:
Command Activity: an activity triggered when a message is sent in an IM, MIM, or Chatroom.
Form Activity: an activity triggered when a user replies to an Elements form message.
Command Activities
A command-based activity is triggered when a message is sent in an IM, MIM, or Chatroom. Using the Activities API allows developers to register commands in the following formats:
@bdk-bot /buy
(Slash command with a bot @mention)
/buy 1000 goog
(Slash command without a bot @mention)
Listen for the word
'hello'
(Not a Slash command - Listen for a specific word)
Note: If you choose to create your own CommandActivity
class, you must implement the matcher()
and on_activity()
methods provided by the AbstractActivity
class. For more information on the implementation of the CommandActivity
class, continue here.
Form Activities
The Activities API also makes it easy for Bots to listen for elements form submissions. Assume the following elements form has been posted into a room with the following attributes:
form
id
= "hello-form"<text-field>
name = "name"form contains an action button
In order to register a form activity or listen for an incoming elements form submission, bots must register a class that extends the FormReplyActivity
class:
Note: If you wish to create your own FormReplyActivity
class, you must implement the methods matcher() and on_activity()
methods provided by the AbstractActivity
class. For more information on the implementation for the FormReplyActivity
class, continue here.
As shown above, the Activities API makes it simple to manage incoming commands, elements form submissions, and access message context making it easy to manage bot-user interactions and create custom workflows.
User, Message & Room Management
As shown above, the BDK for Python makes it easy to create a datafeed and listen for events through the RealTimeEventListener
class. In addition, this class makes it easy to access user, message, and room data in context. Each eventType method is implemented with instances of V4Initiator
and V4MessageSent
objects:
Use the V4Initiator
class methods to access the the user data in context:
Method | User Attribute |
| User ID |
| First Name |
| Last Name |
| Display Name |
| |
| Username |
Use the V4MessageSent
class methods to access message data in context:
Method | Attribute |
| Message ID |
| Message Timestamp |
| Message Text |
| Shared Message |
| Message Data |
| Message Attachments |
Use the V4MessageSent
class methods to access stream data in context:
Method | Attribute |
| Stream ID |
| Stream Type |
| Room Name |
| Room Members |
| External |
| Cross Pod |
Managing Context through Activities API
The Activities API also makes it easy to access relevant user, message, and stream data in context. CommandActivity
classes have access to to this data through the CommandContext
class. This class is instantiated with instances of V4Initiator
and V4MessageSent
objects. Bots are able access to the user, message, and stream data in context through the same methods shown above. Leverage these methods within the on_activity()
method shown below:
FormActivity classes have access to relevant user, form, and stream data through the FormReplyContext
class. This class is instantiated with instances of the V4Initiator
and V4SymphonyElementsAction
class. The V4SymphonyElementsAction
class provides the following methods to access form data in context:
Method | Attribute |
| Elements Stream ID |
| Elements Message ID |
| Elements Form ID |
| Elements Form Values |
Message Templating
The BDK for Python also supports Jinja message templating. In order to use message templating, you must leverage the Jinja template engine. Below is an example:
The corresponding Jinja template is shown below:
Using templating you can also create Element Forms. Below is an example of a price enquiry form template:
Last updated