Professional Documents
Culture Documents
Table of Contents
Overview ....................................................................................................................................................................... 3
Supported Versions ....................................................................................................................................................... 3
Disclaimer ...................................................................................................................................................................... 3
IBM Bluemix Account .................................................................................................................................................... 4
Create a Watson REST Message .................................................................................................................................... 5
Create a Bot User .......................................................................................................................................................... 7
Scoped App Design Considerations ............................................................................................................................... 8
Design #1 – Async Business Rule with Sync Outbound REST ..................................................................................... 8
Design #2 – Sync Business Rule with Async Outbound REST ................................................................................... 11
Connecting Watson Dialogs to ServiceNow Actions ................................................................................................... 14
Create an Action table ............................................................................................................................................. 14
Relate an Action to a Dialog node ........................................................................................................................... 14
Executing Actions ..................................................................................................................................................... 14
Other Concerns ........................................................................................................................................................... 16
Chat Message Destination ....................................................................................................................................... 16
Watson Conversation Context Management .......................................................................................................... 16
Chat Entry Point ....................................................................................................................................................... 17
Chat Bot Impersonation ........................................................................................................................................... 19
Appendix A .................................................................................................................................................................. 20
Code: Chat Entry Point ............................................................................................................................................. 20
PRB1066179: GlideLiveMessageBroadcaster is unavailable to scoped applications ............................................... 22
Overview
This paper provides recommendations to build Virtual Agent bots on the ServiceNow Platform that integrate with
the IBM Watson Conversation Service. All chat interactions will occur through the Connect chat client provided by
ServiceNow.
Throughout this document, callouts for advantages and disadvantages of each approach will be noted. It is up to
you to decide which approach makes sense for your organization given your specific needs and criteria for success.
Supported Versions
This whitepaper copyrighted @2017, supports the ServiceNow Platform for releases Jakarta, Istanbul and Helsinki.
The IBM Watson Conversation service is at version 2017-05-26. All versions beyond this should also be compatible.
Disclaimer
ServiceNow is changing the way people work. With a service-orientation toward the activities, tasks and processes
that make up day-to-day work life, we help the modern enterprise operate faster and be more scalable than ever
before. The content covered on this whitepaper
The updates in this document represent our integration specs and assumptions, only as of the date of this
whitepaper. We undertake no obligation, and do not intend to update the forward-looking changes, to review or
confirm expectations on installations completed. The integrations with IBM, the IBM logo, Watson,
Bluemix and ibm.com are trademarks or registered trademarks of International Business Machines Corporation in
the United States, other countries, or both.
Further information on these and other factors that could affect our technical outcomes are not intended to be
included.
Take note of a few things in this screenshot.
1. ${workspaceId} parameter – The Workspace Id of your specific Watson Conversation workspace.
2. ${version} parameter – The version of the Watson API you want to use.
3. Content-Type – Let the Watson service know we are sending the payload as JSON.
NOTE: You may hardcode these parameters instead if you do not need them to be programmatic in
your scenario.
2. Watson Service REST Credentials
Under the Authentication tab, choose Basic authentication type and create a new Basic auth profile to
hold your Watson Service credentials as found in step 3a above.
3. HTTP Method
Create a new HTTP Method to handle the POST request to the Watson Conversation service. Most of the
parameters are inherited from the parent REST Message record, but you will need to specify the Content
field of the POST payload as required by the Watson Conversation Service API docs.
The ${message} and ${context} parameters will be provided at runtime via scripting code. Examples are
provided in the sections below.
Build your app
1. Business Rule
Start by creating an async insert business rule on the live_message table. This is the entry point for your
virtual agent application code. For best practices consider putting all of your application code in a Script
Include instead of directly in this business rule.
a. Set a low priority value here to get faster perceived experience for your users.
2. Sync Outbound REST call
Within your virtual agent application code, make an outbound REST call to the IBM Watson Conversation
service endpoint. See the following example code:
This code takes the existing REST Message object you created earlier, sets some parameters, and executes
the outbound REST call immediately via the r.execute() call. Your script is now blocked while it waits for
an HTTP response object. This send function also gives an example return object and one possible
method for handling REST errors.
3. Sending a response back to the Connect chat client
Once your application code has the Watson response and finished any other processing it can proceed to
forward a response message back to the end user. This is done by using the LiveFeedMessage script
include object and calling its postMessage(data) method.
4. Business Rule
When posting a Live Feed message back to the user from behind an async business rule an extra step is
required in order for that message to be relayed to the frontend user. Create another business rule using
the following configurations:
• Table: Live Feed Message [live_message]
• Active: true
• Advanced: true
• When: before
• Order: 25
• Insert: true
• Filter Conditions: Profile is <choose your bot user>
• Actions: Chat To true
This will set the “Chat” (chat_message) field to true; as this can not be set via the LiveFeedMessage API.
This will indicate to Connect to forward that chat message along to the frontend user.
Considerations
Advantages Disadvantages
Build your app
1. Business Rule
Start by creating an insert Business Rule on the live_message table. This is the entry point for your virtual
agent application code. For best practices consider putting all of your application code in a script include
instead of directly in this business rule.
2. Async Outbound REST call
Within your virtual agent application code, make an outbound REST call to the IBM Watson Conversation
service endpoint. See the following example code:
This code takes the existing REST Message object you created earlier, sets some parameters, and executes
the outbound REST call asynchronously via the r.executeAsync() call. This send function also gives an
example return object and one possible method for handling REST errors.
NOTE: A background job polls the ecc_queue table every 2 seconds (based on the property
glide.scheduler.sleeptime) for outbound REST jobs to execute. Depending on how many items
are in the queue your request might not execute immediately. This can add a delay in the chat
experience that your users can perceive as a slow response from the bot.
In order for your application to process the asynchronous Watson response you will need to wait for the
response to return by using response.waitForResponse(6). ServiceNow recommends 6s; adjust as needed
from your own testing.
3. Send a response back to the Connect chat client
Once your application code has the Watson response and has finished any other processing it can proceed
to forward a response message back to the end user. This is done by using the LiveFeedMessage script
include object and calling its postMessage(data) method.
Considerations
Advantages Disadvantages
This is how you can extend your bot’s functionality based on the current dialog node in the Watson conversation
flow.
Relate an Action to a Dialog node
Once you create a bot action, copy its sys_id, log into your IBM Bluemix account, and open the Conversation
workspace tool. Once you find the dialog node in the tree you wish to relate the action too, open the node’s JSON
response object and create a context variable referencing the action’s sys_id.
For example:
Executing Actions
When you make the REST call to Watson to process the client’s input message, it runs through its dialog tree and
stop on a matching dialog node. That node’s context is part of the returned response object of the resulting REST
call. This is where your virtual agent application code should parse the context response for action sys_ids, look up
those actions, and execute those scripts to perform those actions.
NOTE: This is only example code:
Other Concerns
Other items to consider when developing your virtual agent application.
Chat Message Destination
Special logic is needed to determine who is talking to the virtual agent and to keep the virtual agent from
answering its own messages. Use the following logic to help determine when your virtual agent should handle an
incoming chat message from your business rule.
NOTE: This is only example code.
In this example code, BOT.USER_ID is a constant referencing the sys_id of your bot’s sys_user. Feel free to
implement this however you like, but the take-away is that we do not want the bot to process messages coming
from itself.
Additionally, we do want the bot to respond to @mention annotations or only to messages in groups in which the
bot is actively participating.
Watson Conversation Context Management
When interfacing with the Watson Conversation service your application will need to maintain the context of each
conversation. This context JSON object will need to be passed to the Watson Conversation service for each request
and then be updated by the resulting context object when the response returns from Watson. Your application
should persist the context JSON object in a table which can be related to the current Connect Chat conversation.
A simple table is needed to store the Watson JSON context and reference to the related live_group_profile
conversation. Add any additional fields, references, or structures needed by your app.
NOTE: Example data model
Option #1 – Opening a Connect chat floating window
This option allows you to open a floating Connect chat window over top the current UI of your instance. This can
be triggered from any mechanism you like, but we will demonstrate this from a UI Action.
NOTE: Example code
Option #2 – Opening the full screen Connect chat
If you prefer to open a new full screen window for your chat experience, then use the following script to do so. It
will perform the same steps as above but launch Connect chat in a new tab.
NOTE: Example code
Chat Bot Impersonation
When taking actions within your application those actions will be reflected as the originating chat user. Currently
there is no way for a scoped app to impersonate another user; e.g. execute the actions as the bot user.
Appendix A
Code: Chat Entry Point
The following script include code can be used to create a conversation and open a new chat window with the
current user and the virtual agent bot. Use this script include from any means needed from your application’s UI.
Overview
1. Creates a new group, or reuses an existing group from the live_group_profile table to be used for
this chat interaction
2. Adds the bot user to the group using the bot user’s sys_id
3. If using the connect chat window then set the frame_state to open for all live_group_members of
the live_group_profile used for the chat interaction. This flag determines if the connect window
will open automatically at the start of the interaction.
4. Send a welcome message from the bot to the user using the function
liveFeedMessage.postMessage. This function is also responsible for publishing the AMB message
that causes the connect chat window to open. *
5. If using the connect full screen chat page, then construct and return the redirect URL for page.
Known Issue
There is a known bug with Helsinki and Istanbul that prevent the chat bot from updating the connect window from
a scoped application, causing the chat to appear as if it has hanged. The LiveFeedMessage postMessage
function can insert the chat messages into the live_messages table, however it cannot update the AMB
queue, which is used by connect to determine when it should pull data and update the chat interface.
PRB1066179: GlideLiveMessageBroadcaster is unavailable to scoped applications
See the next Appendix A section for a workaround which may be included into your scoped app.
Script Include
var ClickToChatService = Class.create();
ClickToChatService.prototype = {
initialize: function() {
this.botUserSysId = null;
},
/**
* Get the Live Profile sys_id of the given user
* LiveGroups use live_profiles not user sys_ids
*
* @param String userSysId
*/
getLiveProfileId: function(userSysId) {
var gr = new GlideRecord('live_profile');
gr.addQuery('document', userSysId);
gr.query();
if(gr.next()) {
return gr.getUniqueValue();
}
return null;
},
/**
* Find the existing liveGroupMessage, or
* create a new one
*
* @param String botUserName the username of the bot sys_user record
* @return String lgpId - the Live Group Profile sysId
*/
getLiveGroupProfileId: function(botUserName) {
var gr = new GlideRecord('live_group_profile');
gr.addQuery('name', gs.getUserDisplayName() + ',' + botUserName);
gr.query();
var lgpId;
if(! gr.next()) {
// create group profile
var lgp = new GlideRecord('live_group_profile');
lgp.initialize();
lgp.name = gs.getUserDisplayName() + ',' + botUserName;
lgpId = lgp.insert();
} else {
lgpId = gr.getUniqueValue();
}
return lgpId;
},
/**
* Add a member to a live group if they are
* not already a member of the group
*
* @param String lgpId Live Group Profile ID
* @param String liveProfileId Live Profile ID of the user
* @return void
*/
createMemberInLGM: function(lgpId, liveProfileId){
var gr = new GlideRecord('live_group_member');
gr.addQuery('group', lgpId );
gr.addQuery('member', liveProfileId);
gr.query();
if(! gr.next()) {
// create live group member for end user
var user = new GlideRecord('live_group_member');
user.initialize();
user.group = lgpId;
user.member = liveProfileId;
user.state = 'Admin';
user.frame_state = 'open';
user.insert();
}
},
/**
* Sets the framestate as open for all users in the live group
*
* @param String groupId Live Group Profile sys_id
* @return void
*/
openFrameState: function(groupId){
var gr = new GlideRecord('live_group_member');
gr.addQuery('group', groupId);
gr.query();
while (gr.next()) {
gr.frame_state = 'open';
gr.update();
}
},
/**
* @param groupId
* @param message
* @param fromProfileId
* @return void
*/
sendMessage: function(groupId, message, fromProfileId){
var data = {
message : message,
group_id : groupId,
to_profile : null,
is_worknotes : 'false',
from_profile : fromProfileId
};
/**
* Entry point to the script include
*
* @param botUserSysId
* @param redirect boolean true will return a redirect URL
* @return String or void
*/
execute: function(botUserSysId, redirect) {
this.botUserSysId = botUserSysId;
if(useRedirect)
return gs.getProperty("glide.servlet.uri") + '$c.do#/chat/' + lgpId;
},
type: 'ClickToChatService'
};
PRB1066179: GlideLiveMessageBroadcaster is unavailable to scoped applications
You may choose to help customers work around this bug by creating a global script include to proxy your scoped
app messages to the broadcaster. This global script include can only be included into your scoped app if you are
NOT publishing it on the ServiceNow Store, but delivering it directly to your customers.
If you are publishing your scoped app to the ServiceNow Store then you will need to provide a different
mechanism to deliver this update to your customers which is external to your app. Please work with the store
certification team and your customers on providing this global update outside of the ServiceNow Store publication
process.