logo

NJP

Creating Custom MS Teams Cards

Import · May 25, 2022 · article

Today we were presented the challenge of:

"We want an MS Teams notification sent to every member of an assignment group whenever a new incident is assigned to that group"

I will probably write a separate post about the investigation into that but during the investigation into it I discovered something I thought was really cool.

Sending a MS Team message from a script

ServiceNow has an API for firing teams messages that can be called form a custom flow action, business rule, wherever you like!

The Script include is called MSTeamsOutboundMessages but unfortunately its locked down. But looking into some other scipts I found that MSTeamsOutboundMessages().sendDirectMessage() takes three arguments:

  • sys_id of the user who should receive the teams message
  • the string "adaptiveCard"
  • a JSON which is what tells teams what to display. The JSON is part of "Adaptive Cards" which you can read all about here https://adaptivecards.io/. There is even a GUI designer for creating cards found here https://adaptivecards.io/designer/

Here is an example:

var inc_number = "INC007"
var send_to = '00004934db486850e2bd49a239961949'
var priority = "P1"
var SD = "Short Description"
var record_id = "000024e1be38d906da0a798bd4bcbe9"

var card =
{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "size": "Large",
            "weight": "Bolder",
            "text": "Incident has been assinged to your queue.\n ",
            "wrap": true
        },
        {
            "type": "TextBlock",
            "size": "Medium",
            "weight": "Bolder",
            "text": priority + " " + inc_number,
            "wrap": true
        },
        {
            "type": "TextBlock",
            "text": SD,
            "wrap": true
        },
        {
            "type": "ActionSet",
            "actions": [
                {
                    "type": "Action.OpenUrl",
                    "title": "View Ticket details",
                    "url": "https://xxxx.service-now.com/nav_to.do?uri=%2Fincident.do%3Fsys_id%3D" + record_id
                }
            ]
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.3"
}

var outBoundMessage = new MSTeamsOutboundMessages();
var response = outBoundMessage.sendDirectMessage(send_to, MSTeamsConstants.OUTBOUND_MESSAGE_TYPE.ADAPTIVE_CARD, card2);

We can create a card that looks like

image

Doing something useful with that information

Now we know how to send cards we can create a custom Flow ActionInputs

image

Then we add a scripted step

imageWe need to add our input values

image

And our script

(function execute(inputs, outputs) {
var card =
{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "size": "Large",
            "weight": "Bolder",
            "text": "Incident has been assigned to your queue.\n ",
            "wrap": true
        },
        {
            "type": "TextBlock",
            "size": "Medium",
            "weight": "Bolder",
            "text": inputs.number + " \n " + inputs.priority,
            "wrap": true
        },
        {
            "type": "TextBlock",
            "text": inputs.short_description,
            "wrap": true
        },
        {
            "type": "ActionSet",
            "actions": [
                {
                    "type": "Action.OpenUrl",
                    "title": "View Ticket details",
                    "url": "https://xxxxx.service-now.com/nav_to.do?uri=%2Fincident.do%3Fsys_id%3D" + inputs.record_id
                }
            ]
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.3"
}

var outBoundMessage = new MSTeamsOutboundMessages();
var response = outBoundMessage.sendDirectMessage(inputs.user_id, MSTeamsConstants.OUTBOUND_MESSAGE_TYPE.ADAPTIVE_CARD, card);

})(inputs, outputs);

Then we can test using image

image

An providing the the user in the "send to id" field has their account linked to MS Teams they should get a card

image

image

View original source

https://www.servicenow.com/community/now-platform-articles/creating-custom-ms-teams-cards/ta-p/2311591