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))