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!
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.
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.
Following your suggestions, I've modified the code and moved the main logic to a project library. However, I still haven't solved the problem where the script works correctly from the Script Console, but not from the tag value change script. Is this an Ignition limitation, or are there related settings on the Gateway that I can adjust? Or is there something else in my script that needs to be improved?
Project Library script:
# Core logic for sending Teams messages.
from java.lang import Throwable
# Initialize HTTP client once for performance.
client = system.net.httpClient()
def send_teams_message(message_payload):
"""
Sends a message to a Teams workflow.
"""
# Teams Webhook URL.
webhook_url = "https://prod-145.westus.logic.azure.com:xxxx...xxxx"
try:
response = client.post(
url=webhook_url,
data=message_payload
)
status_code = response.getStatusCode()
# Log status.
if status_code == 202:
system.util.getLogger("TeamsWebhook").info("Message sent successfully!")
else:
system.util.getLogger("TeamsWebhook").warn("Failed to send message. HTTP Status Code: " + str(status_code))
except Throwable, t:
# Log all errors.
system.util.getLogger("TeamsWebhook").error("Webhook connection or execution failed!", t)
Tag value change script:
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
# Only run the script if the tag's value has actually changed.
if str(currentValue.value) != str(previousValue.value):
message_text = "Tag '" + tagPath + "' s value has changed! New value is: " + str(currentValue.value)
payload = {
"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": "Ignition Alert: Tag state changed",
"size": "Medium",
"weight": "Bolder"
},
{
"type": "TextBlock",
"text": message_text,
"wrap": True
}
]
}
}
]
}
system.util.project.teams_integration.send_teams_message(payload)
To aide in troubleshooting, right-click on a tag which has this script configured, click on View Tag Diagnostics, I'm guessing you will will see an error next to Value Changed.
Because you really haven’t changed anything, (even if you were successfully calling the library script). Scripts in the Project Library, assume the scope from which they are called. This means that you would still have a scope discrepency.
Long story short, testing scripts in the Script Console, that are intended to run in Gateway Scope, is just not a good practice. It leads to confusion just like this.
I just wanted to thank everyone for the assistance! The problem is solved. The key was figuring out that I needed to change the message format . Appreciate all the insights!