How to send a message to Teams Chat on a tag value change?

Hello everyone,

I'm trying to send a message to a MS Teams chat whenever a tag value changes, and I've run into a strange issue.

I've successfully set up a Tag Value Change Script to trigger a Gateway Message Handler, and the script itself is running correctly. The handler's only job is to send a simple message to Teams via a Workflows.

The interesting part is that the exact same code works perfectly when I run it from the Script Console. However, when it's triggered by the Tag script and executed in the Gateway Message Handler, the message never gets sent, and no errors are logged.

Is there something I might be missing about the execution environment of a Gateway script versus the Script Console? I'm a beginner,any help would be greatly appreciated!

Here is my Message Handler code:

def handleMessage(payload):
		
		logger = system.util.getLogger("Teams_Notification")
		webhook_url = "https://prod-02.westus.logic.azure.com:xxxx..xxxx"
		payload_teams = {
			"type": "message",
			"attachments": [
				{
					"contentType": "application/vnd.microsoft.card.adaptive",
					"content": {
						"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
						"type": "AdaptiveCard",
						"version": "1.2",
						"body": [
							{
								"type": "TextBlock",
                            "text": "value changed",
                            "size": "Medium",
                            "weight": "Bolder"
       					}
                    	]	
                	}
            	}
        	]
     	}
		json_data = json.dumps(payload_teams)
		
		try:
			client = system.net.httpClient()
			response = client.post(
            url=webhook_url,
            headers={"Content-Type": "application/json"},
            data=json_data
          )
			status_code = response.getStatusCode()
			if status_code != 200:
				logger.error("Teams fail HTTP code:%s" % status_code)
			else:
				logger.info("Teams successed HTTP code:%s" % status_code)
		except Exception as e:
			logger.error("API fail:%s" % str(e))

Might be related to this.

Yes.

The script console executes in designer scope, which is a varient of Vision Client Scope. A Gateway Script (so a Tag Value Change Script) executes in Gateway Scope. Functions often have different signatures dependent upon the scope they are called in.

Also, your Error handling is not correct.

except Exception as e will only catch Jython errors, it will not catch Java errors. You should import Throwable from java.lang import Throwable an make another except clause except Throwable, t. You can hand the entire throwable to the logger.

This should definately be a Gateway Event Script, maybe Tag Change. I would also move the script to a project library script.

3 Likes

In addition to @lrose's excellent advice, you also don't need these two lines.

If you pass a dictionary as the data parameter, system.net.httpClient will automatically encode it as JSON and send the appropriate content-type header.

Also, after you move this logic to a project library script, move the system.net.httpClient() initialization out of the main function body.

3 Likes