Message to MS Teams when a Request got new comment

806806

Rule config:

[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "request"
    ],
    "modifications": {
      "created": false,
      "deleted": false,
      "updated": [
        "Comments"
      ]
    }
  },
  {
    "or": [
      {
        "and": [
          {
            "value": {
              "type": "pipelineBlockOutput"
            },
            "target": {
              "name": "MS Teams URL",
              "type": "field",
              "target": {
                "name": "Project",
                "type": "field",
                "target": {
                  "type": "pipelineBlockOutput"
                }
              }
            },
            "operator": {
              "type": "exists"
            }
          }
        ]
      }
    ],
    "type": "filter:Relational"
  },
  {
    "type": "action:JavaScript",
    "script": "const entityId = args.ResourceId;\nconst entityName = args.Current.Name;\nconst entityType = args.Current.EntityType.Name;\nconst projectId = args.Current.Project.Id;\nconst api = context.getService(\"targetprocess/api/v2\");\nconst projectTeamsURL = await api.queryAsync(\"Request\", {\n  select: \"project.MSTeamsURL\",\n  where: `project.id=${projectId}`\n});\n//MS Teams webhook URL\nconst teamsURL = projectTeamsURL[0];\nconsole.log(teamsURL);\n\nconst http = context.getService('http');\nconst notification = await http.postAsync(teamsURL, {\n  body: {\n    \"@context\": \"https://schema.org/extensions\",\n    \"@type\": \"MessageCard\",\n    \"themeColor\": \"0072C6\",\n    \"title\": \"URGENT--BCBS DAP Ticket\", //Notification title\n    \"text\": `${entityType} #${entityId} - \"${entityName}\" was changed.`, //Notification description\n    \"potentialAction\": [\n      {\n        \"@type\": \"OpenUri\",\n        \"name\": \"Open in Targetprocess\", //Button title\n        \"targets\": [\n          { \"os\": \"default\", \"uri\": \"https://austinovich.tpondemand.com/entity/\" + entityId } //Entity URL\n        ]\n      }\n    ]\n  }\n});\n\nif (!notification) {\n  console.log(\"Sending Notification failed.\");\n}\nconsole.log(`Notification has been sent for ${entityType} #${entityId} - \"${entityName}\"`);"
  }
]