The Einstein Bots Platform API allows you to add Einstein Bots into any conversation channel. We’ve created an open-source Java SDK and Channel Connector to give you the tools you need to easily integrate Einstein Bots into your conversational channels on top of the existing digital engagement channels supported by Service Cloud.
In our previous post, we showed how to use the SDK’s session-managed client to simplify your implementation by automatically managing sessions for your conversation channel.
In this post, we’ll explore the Channel Connector framework and its features. You’ll learn how easy it is to build a channel connector application for Einstein Bots using the Spring Boot framework. Additionally, you’ll learn how to monitor the health of your application, check your application metrics, and more.
Quick Recap of the Einstein Bots SDK
The Channel Connector framework is built on top of the Einstein Bots SDK. Let’s do a quick recap of the benefits of using the SDK:
- It abstracts the complexities of authentication and HTTP communication, and provides a simple Java API to connect to Einstein Bots
- It provides model classes that automatically serialize/deserialize JSON payloads to Java objects
- It provides a session-managed client to automatically manage sessions
Introducing the Channel Connector framework for Einstein Bots
Einstein Bots can be integrated with conversation channels, such as Webchat, Facebook, WhatsApp, or Slack. To implement this integration, you’ll need to build a channel connector application that takes care of the interactions. For example, if you build a connector application for Slack, the application will need to listen for messages on a Slack channel, forward the received messages to your Einstein Bot, and send the bot’s response back to Slack.
Although the Einstein Bots SDK does a lot of heavy lifting for you, to build a complete channel connector application, you would need to implement the following features :
- Create an instance of
BasicChatbotClient
orSessionManagedChatbotClient
and manage its life cycle - Read all configurations, such as
orgId
andbotId
from the application properties, and use them to createChatbotClient
instances - Monitor the health and metrics of your channel connector application
Luckily, you don’t have to implement all this yourself if using Spring Boot, as we’ve built a Channel Connector framework to help you out.
The Einstein Bots Channel Connector framework is a Spring Boot Starter that simplifies building external channel connectors for an Einstein Bot. The framework uses the Einstein Bot SDK and sets up dependencies like caching, authentication, and metrics using a Spring auto-configuration. Here is a quick overview of the benefits using the Channel Connector framework:
Auto-configuration
The Spring beans are auto-configured using the application.properties
.
Authentication mechanism
If all OAuth properties are specified in the application properties, the Channel Connector framework configures the Einstein Bots SDK to use the JWT Bearer OAuth flow for AuthMechanism
.
Caching mechanism
If sfdc.einstein.bots.cache.redis-url
is included in the application properties, the SDK uses a Redis cache. Otherwise, the in-memory cache is implemented.
Basic and session-managed bot clients
If you configure all required properties in application.properties
, both BasicChatbotClient
and SessionManagedChatbotClient
are auto-wired into Spring beans with default configurations.
Bot health indicator
The Spring’s health indicator BotsHealthIndicator
is auto-configured to allow you to check the bot’s runtime health status. More information on how to do this later in the blog.
Custom implementation support
You can provide your own implementation of Spring beans instead of using the default one (e.g., if you want to use your own cache mechanism or use your own authentication mechanism). When a custom implementation is found, it will be used to auto-wire Spring beans instead of the default implementation provided by the Channel Connector framework.
Metrics
The framework uses the Spring Micrometer library to collect the metrics. You can publish the metrics to most of the popular monitoring systems, such as New Relic, Prometheus, and Graphite, using Spring Micrometer.
Who the Channel Connector is for
The industry-standard way to manage dependencies and object lifecycle is to use a dependency injection framework like Google Guice or Spring. We decided to use one of the most popular application development frameworks, Spring Boot, to build the Channel Connector framework.
If you are able to use Spring Boot for your connector application, you can use the Einstein Bot Channel Connector. If you need to use some other application development framework, or manage dependencies by yourself, use the Einstein Bot SDK directly as a Java library.
How to use the Channel Connector Framework
Enough theory — let’s write some code using the Channel Connector framework. In this section, you’ll learn how to create a new channel connector application, customize it to your needs, and monitor its health.
Create a channel connector app
To create your channel connector application, simply run the following Maven command. Modify the command to include archetypeVersion
to the most recently released version of the Einstein Bot Channel Connector Archetype. Also, set package
, groupId
, and artifactId
to the names of your choice.
Understand the project structure
A new directory, myapp-channel-connector
, will be created following the basic project skeleton shown below. The directory name and directory structure after src/main/java
will be different based on the values you used when running the previous command.
The created pom.xml
will include all required dependencies, including the Channel Connector framework and the Einstein Bots SDK.
The created ConnectorApplication
class, annotated with @SpringBootApplication
, will be the entry point to start the connector Spring Boot application.
A README.md
will be included with instructions on how to run the application.
Configure the application
To configure the application, you will need to create an application.properties
file in the src/main/resources
directory with all the required properties. You can copy the example properties from src/main/resources/application.example.properties
and update them based on your environment. You will also need to place your OAuth private key in src/main/resources
. You can refer to our user guide to see a complete list of application properties and how to find their values.
Start the application
Once you complete the configuration, you can start the app by running Spring Boot with this Maven command.
After running the command, you’ll have a basic application running without having written a single line of code!
Next, let’s use auto-wired Spring beans to send messages to the bot.
Build a user interface to communicate with your bot
Let’s say that we want to build a simple UI (like the screenshot below) to play around with bot requests/responses.
The UI will accept the user’s message, send it to the bot, and show the bot’s response. The frontend can be built using HTML. We will also need a REST controller to process the request on the server side.
We can create a Spring REST controller and use @Autowired
annotations to inject Spring beans with the chatbot client implementation. The controller code will look like this:
The Channel Connector framework initializes the @Autowired
properties, such as chatbotClient
, including its dependencies like AuthMechanism
and Cache,
based on the configurations defined in application.properties
. In addition, we can refer to any application property using the Spring @Value
annotation.
SessionManagedChatbotClient
is a class provided by the Einstein Bot SDK that you can use to send messages to the bot.
The full working example code of this ui-connector is available under the examples dir in the einstein-bot-channel-connector repo.
Check the application health
The channel connector includes a class called BotsHealthIndicator
which implements Spring’s HealthIndicator. You can go to http://<host>:<port>/<baseurl>/actuator/health
to check the health of the application. You will get a JSON response indicating status
as UP
or DOWN
like this:
You can also programmatically check health by auto-wiring BotsHealthIndicator
and calling the .health()
method.
View metrics
The Spring Micrometer library is used for instrumentation and many foundational metrics are already collected. You can find all collected metrics at http://<host>:<port>/<baseurl>/actuator/metrics
.
Publish metrics
Spring Micrometer can publish metrics to all popular monitoring systems, such as New Relic, Prometheus, Graphite, and more. To publish the metrics to an external monitoring system, refer to the instructions for your monitoring system in the Micrometer documentation. See NewRelicMetricsExportAutoConfiguration for an example of publishing the metrics to New Relic.
Summary
To recap, you have learned how to build an application using the Einstein Bots Channel Connector Framework. In addition, you know how to check the health of the application and monitor metrics. To learn more, refer to the Einstein Bot Channel Connector user guide and explore our open source example applications.
About the author
Rajasekar Elango is a Principal Software Engineer at Salesforce working on the Einstein Bots Platform. You can follow him on LinkedIn or Twitter.