- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 01-07-2021 04:07 AM
In this example, I am creating a Virtual Agent topic which would send incident details to a Slack channel. The user's reply in slack would be sent back to Virtual Agent chat.
1. Create a Slack App and add it to the Slack Channel.
- When we create an App, we would be given a Token. this token is needed to communicate with Slack
- Once an App is created we need to add the App to the channel.
2. Create REST APIs for a collection of Slack Methods
- create an Outbound Integration - REST Messages
3. Create a HTTP methods to Post the Incident Details to channel
- we use chat.postMessage endpoint of Slack APIs. (chat.write scope should be given for the app to use this endpoint)
4.Create a HTTP methods to get reply from channel
- we are trying to get the thread reply here, so using the conversations.replies Endpoint
channel - Slack channel
ts - thread_ts that is available in chat.postMessage response
5. Create a Virtual Agent topic:
- duplicate an existing Virtual Agent topic.
- This topic would display list of incident and lets you comment on a selected incident.
a. Create a Script variable so as to store the thread_ts that is needed to get the reply
b. Send message to Slack.
- In a Script Bot response, we add our script to call the chat.postMessage method that we have created in step 3.
Ex:
var r = new sn_ws.RESTMessageV2('x_237151_slack_va.Slack APIs ', 'Post Msg');
r.setStringParameterNoEscape('msg', 'Incident - ' + gr.number + ' has been updated with a comment.' + "<@userID> Please reply with the progress");
var response = r.execute();
var responseBody = JSON.parse(response.getBody());
var httpStatus = response.getStatusCode();
vaVars.thread_id = responseBody["ts"];
c. Get reply from Slack
- In a Script Bot response, we add our script to call the method in created in step 4
Ex:
(function execute() {
var responseText = '';
try {
var r = new sn_ws.RESTMessageV2('x_237151_slack_va.Slack APIs ', 'Get status from Slack');
r.setStringParameterNoEscape('timestamp', vaVars.thread_id);
var response = r.execute();
var responseBody = JSON.parse(response.getBody());
var httpStatus = response.getStatusCode();
responseText= responseBody["messages"].pop()["text"];
}
catch(ex) {
var message = ex.message;
responseText = "Could not get response from User";
}
return responseText;
})()
Only the last response to the thread would be displayed back to the Virtual Agent.
Example of how the conversation would look on Virtual Agent and Slack
Slack:
This is a simple implementation of Slack in ServiceNow. there are also other ways through
Hope this helps!