Integrating ServiceNow with Microsoft Teams

I was recently tasked to develop an integration with Microsoft Teams for pushing notifications of new, and scheduled changes plus high-priority incidents into a Microsoft Teams team.

This is a rather trivial REST integration accomplished with a combination of an Outbound REST Message, and a Insert/Update Business Rule(s).

From the ServiceNow side, creating an integration like this is a simple matter of creating a new Outbound REST Message, and associated HTTP Methods that actually control what is sent to each endpoint. For our integration with Microsoft Teams, each channel can have an assortment of "Connectors", I used the team's "Incoming Webhook" type, which allows a JSON-encoded payload.

In order to create the formatting for these easily, I used MessageFlow card playground which allows you to input the JSON payload and see what the result would look like in MS Teams.

{
	"sections": [{
		"facts": [{
			"name": "Change Number",
			"value": "${factChangeRecord}"
		}, {
			"name": "Affected Service",
			"value": "${factService}"
		}],
		"potentialAction": [{
			"@context": "http://schema.org",
			"@type": "ViewAction",
			"name": "${actionText}",
			"target": ["${actionLink}"]
		}]
	}, {
		"activityText": "${cardText}"
	}],
	"summary": " ",
	"themeColor": "${cardColor}",
	"title": "${cardTitle}"
}

From the ServiceNow side, creating a new Outbound REST Integration is relatively easy, start by creating a new Outbound REST Message, from:
"System Web Service" -> "Outbound" -> "REST Message"
Create the new message for the channel you would like to post the message to, setting the "Endpoint" field as the one provided from the Teams integration.

After this is created, you will see a new "HTTP Method"  under the Message's Related Lists, you will need a single POST-type method, you can modify the one created or create a new one.

Within this Method, you need to set the "Content" field to the JSON-payload created earlier. Once you have saved this, you can use the "Auto-generate variables" to automatically convert the "${cardText}" notation within the content into variables that can be substituted for record data within the integration.

You will see that there are now 7 variable substitutions in the Related list, you can then set test values for each of these that will be used when you use the "Test" Related Link.

The integration can be used within any server-side script execution, such as a Business Rule or a Workflow script. The code required to call the integration can be auto-generated by using the "Preview Script Usage" also on the HTTP Method-definition, an example follows.

try {
    var r = new sn_ws.RESTMessageV2('Microsoft Teams ', 'Default GET');
    r.setStringParameterNoEscape('actionText', 'Test Action');
    r.setStringParameterNoEscape('factChangeRecord', 'CHG000000');
    r.setStringParameterNoEscape('actionLink', 'https://google.com');
    r.setStringParameterNoEscape('factService', 'Email & Calendaring');
    r.setStringParameterNoEscape('cardTitle', 'Testing REST Message');
    r.setStringParameterNoEscape('cardColor', '#000000');
    r.setStringParameterNoEscape('cardText', 'Body Content');
    //override authentication profile
    //authentication type ='basic'/ 'oauth2'
    //r.setAuthentication(authentication type, profile name);
    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
} catch(ex) {
	var message = ex.getMessage();
}