Send mention to MS Teams about new assignment

🚧

Pre-requisites

Users should have a custom field MSTeamsID

📘

How to apply this rule?

992

🚧

Warning: sample URLs!

Please make sure to replace https://graph.microsoft.com/v1.0/teams/3e165363-bb23/channels/19:[email protected]/messages with your MS Teams API URL and {access_token} with the [MS Teams Access Token] (doc:examples-integration-with-ms-teams#how-to-get-ms-teams-api-access-token)

{
  "pipeline": [
    {
      "type": "source:targetprocess:EntityChanged",
      "entityTypes": [
        "assignment"
      ],
      "modifications": {
        "created": true,
        "deleted": false,
        "updated": false
      }
    },
    {
      "or": [
        {
          "and": [
            {
              "value": {
                "type": "constant",
                "value": "Request"
              },
              "target": {
                "name": "Name",
                "type": "field",
                "target": {
                  "name": "EntityType",
                  "type": "field",
                  "target": {
                    "name": "Assignable",
                    "type": "field",
                    "target": {
                      "type": "pipelineBlockOutput"
                    }
                  }
                }
              },
              "operator": {
                "type": "is"
              }
            },
            {
              "value": {
                "type": "constant",
                "value": "Support Person"
              },
              "target": {
                "name": "Name",
                "type": "field",
                "target": {
                  "name": "Role",
                  "type": "field",
                  "target": {
                    "type": "pipelineBlockOutput"
                  }
                }
              },
              "operator": {
                "type": "is"
              }
            }
          ]
        }
      ],
      "type": "filter:Relational"
    },
    {
      "type": "action:JavaScript",
      "script": "const api = context.getService(\"targetprocess/api/v2\");\nconst user = await api.getByIdAsync(\"User\", args.Current.GeneralUser.Id, {\n  select: `{fullname,MSTeamsID}`\n});\nif (!user.mSTeamsID) {\n  console.log(\"mSTeamsID field is not set for \" + user.fullname);\n}\nconst utils = require(\"utils\");\nreturn utils.sendHttpRequest(\"https://graph.microsoft.com/v1.0/teams/3e165363-bb23/channels/19:[email protected]/messages\",\n  {\n    method: \"POST\",\n    headers: {\n      \"Authorization\": \"Bearer {access_token}\"\n    },\n    body: {\n      body: {\n        \"contentType\": \"html\",\n        \"content\": `<a href=\"${args.AccountBaseUrl}entity/${args.Current.Assignable.Id}\">#${args.Current.Assignable.Id}: ${args.Current.Assignable.Name}</a> Assigned to <at id=\"0\">${user.fullname}</at>`\n      },\n      \"mentions\": [\n        {\n          \"id\": 0,\n          \"mentionText\": `${user.fullname}`,\n          \"mentioned\": {\n            \"user\": {\n              \"displayName\": `${user.fullname}`,\n              \"id\": `${user.mSTeamsID}`,\n              \"userIdentityType\": \"aadUser\"\n            }\n          }\n        }\n      ]\n    }\n  }\n);"
    }
  ]
}