Embedding Watson Conversation Service into your Salesforce apps has never been this easy
Embedding Watson Conversation Service into your Salesforce apps has never been this easy

 

Recently, the IBM Watson Salesforce SDK was released, making it easier than ever to integrate Watson services into your Apex code (see SDK announcement blog here). Currently, the SDK supports the Watson Conversation and Discovery services.

In this blog, we’ll talk a bit about the draw of conversational systems, introduce the Watson Conversation service, and cover how to get started using its capabilities in your own projects.

Benefits of Conversational Systems

Enterprises have multiple tools to access a variety of information about the business processes, its customers, its products, etc. Each one typically requires some learning curve to understand how to best utilize it. However, new employees may not have crossed the learning curve, and experienced employees might face their own roadblocks: being on site with a customer, talking on the phone, attending a meeting, or just needing to quickly access some information without having the benefit of opening up the correct systems, going through the necessary steps, etc. In any case, employees benefit from a faster way to get access to their enterprise systems.

A conversational interface fits this solution by allowing a user to simply ask their question as they would if they were talking to another human, and get the expected information back without having to navigate through all the enterprise systems. This is also ideal for users on the go who may spend a lot of time on mobile devices.

IBM Watson Conversation Capabilities

Watson Conversation Service is designed to create a chat based application that covers a business domain. Think of it like your own personal assistant for your business. Many existing virtual assistants can cover a wide range of topics, at a very shallow level, but Watson Conversation is designed to allow developers to go very deep on the topics that are relevant to their business domain. Using a combination of intents, entities, and context to understand queries, Watson Conversation can drive the dialog to have a discussion about a given topic.

A typical use case would be to create a conversational service that can answer a customer’s common questions, like checking account balances, getting product information, or troubleshooting errors. For more detailed information about the Watson Conversation service, and more resources to get started using it, check out the documentation.

Get Hands-On!

Now that you know a bit about the Watson Conversation service, it’s time to try it out. Before getting into any code, you’ll need to set up your service instance in IBM Cloud.

IBM Cloud Setup

  1. Login or create a free IBM Cloud account to get access to the many Watson services.
  2. Navigate to the Catalog and create a free instance of the Conversation service.
  3. Click on “Service Credentials” on the left and take note of the username and password that show up on the resulting page. You’ll need these for connecting to the service in your Apex code.
  4. Go back to the “Manage” page and click on “Launch tool”. There will be a sample workspace here that we will use for this blog. Click on it to get everything set up.
  5. Go back to the workspaces page and view the details of our sample workspace by clicking on the three options dots in the top right corner. Note the workspace ID, which will be the other credential needed for accessing the service.

Instantiating the Service in Apex

Now that you’re all set up, you can start using the service in your Apex code. The Watson Salesforce SDK will allow you to quickly get started, beginning with the installation instructions here.

With the SDK, getting an Apex instance of your service can be done with just one or two lines of code. If you’re familiar with developing on Salesforce, you can use Named Credentials using the instructions here. Otherwise, you can copy and paste the code below, substituting your username and password you received in IBM Cloud.

IBMConversationV1 conversation = new IBMConversationV1(IBMConversationV1.VERSION_DATE_2017_05_26);
conversation.setUsernameAndPassword('USERNAME', 'PASSWORD');

Now that we have an instance of Conversation, we can use its functionality. For this blog, we’re going to demonstrate using the service in the context of a personal assistant. The sample workspace we saw earlier has done the hard work of setting up sample data to simulate a smart car assistant, and we’ll use that to show how we can programmatically interact with the assistant through the use of the SDK.

Talking to the Service

We’re going to simulate a small conversation with our smart car assistant by sending three messages. The Conversation service will respond appropriately to each one, and will be able to follow chains of conversation with the Context object. Below is the code for the messages and initial context:

List<String> messages = new List<String> {
  'Hello!',
  'Play some music for me.',
  'Jazz sounds great.'
};
IBMConversationV1Models.Context context = null;

Now, we need to loop through the messages and send them one by one. After each exchange, we’ll update the context and repeat, printing the dialog as we go:

for (String message : messages) {
  IBMConversationV1Models.InputData input = new IBMConversationV1Models.InputDataBuilder()
    .text(message)
    .build();
  IBMConversationV1Models.MessageOptions options = new IBMConversationV1Models.MessageOptionsBuilder()
    .workspaceId('WORKSPACEID') // Place your workspace ID here!
    .input(input)
    .context(context)
    .build();
  IBMConversationV1Models.MessageResponse response = conversation.message(options);

  String reply = response.getOutput().getText().get(0);
  System.debug(String.format('MESSAGE: {0}', new String[]{ message }));
  System.debug(String.format('RESPONSE: {0}\n', new String[]{ reply }));

  context = response.getContext();
}

In the Salesforce Developer Console, you can put all this code together and execute it anonymously. If you look at the resulting logs, filtering for debug messages, you should get the following messages:

MESSAGE: Hello!
RESPONSE: Hi. It looks like a nice drive today. What would you like me to do?

MESSAGE: Play some music for me.
RESPONSE: Sure thing! Which genre would you prefer? Jazz is my personal favorite.

MESSAGE: Jazz sounds great.
RESPONSE: Great choice! Playing some jazz for you.

Congratulations! You’ve successfully interacted with a smart car assistant using Watson Conversation and the Watson Salesforce SDK.

Next Steps

The Watson Conversation Service is a powerful service to build a conversational application. Sign up for an IBM Cloud account to get started. Visit the Watson Conversation “Getting Started” to learn more about how to customize your virtual assistant.

About the Authors

Logan Patiño is a software engineer on the Watson Developer Experience team, and is part of the development team for the Watson Salesforce SDK. He and his team focus on making the end-to-end experience of using Watson as easy as possible for developers. He can be reached at loganpatino10@ibm.com.

Mitchell Mason is a Senior Offering Manager on the Watson Conversation Service at IBM. He focuses on improving the service based on user feedback, and discussion is always welcome. You can reach out to him directly at mmason@us.ibm.com. He also runs a slack developer community that is open to anyone to join and participate to discuss AI, Watson, and get support on Watson services. Join the community using the following URL wdc-slack-inviter.mybluemix.net

Join The Discussion

Your email address will not be published. Required fields are marked *