-
-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: forms.toast() does not support Unicode characters. (Enhancement) #2484
Comments
this is a bug of the go-toast app that pyrevit uses to show the toasts. The core of the project is a single go module that calls powershell, so it could be turn into a python script more or less easily. We're open to contributions! |
A few GPT-4o prompts later import subprocess
import time
from xml.etree.ElementTree import Element, SubElement, tostring
# Global script template
SCRIPT_TEMPLATE = """
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$APP_ID = "{app_id}"
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml([xml]@"
{xml_content}
@")
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast)
"""
# Mapping for audio friendly names to ms-winsoundevent strings
AUDIO_MAPPING = {
"default": "ms-winsoundevent:Notification.Default",
"im": "ms-winsoundevent:Notification.IM",
"mail": "ms-winsoundevent:Notification.Mail",
"reminder": "ms-winsoundevent:Notification.Reminder",
"sms": "ms-winsoundevent:Notification.SMS",
"silent": "silent",
"looping_alarm": "ms-winsoundevent:Notification.Looping.Alarm",
"looping_call": "ms-winsoundevent:Notification.Looping.Call"
}
def build_toast_xml(title, message, icon=None, activation_type="protocol",
activation_arguments=None, actions=None, audio="default",
loop=False, duration="short"):
if duration not in {"short", "long"}:
raise ValueError("Invalid duration: must be 'short' or 'long'")
audio = AUDIO_MAPPING.get(audio.lower(), AUDIO_MAPPING["default"])
toast = Element("toast", {
"activationType": activation_type,
"launch": activation_arguments or "",
"duration": duration
})
visual = SubElement(toast, "visual")
binding = SubElement(visual, "binding", {"template": "ToastGeneric"})
if icon:
SubElement(binding, "image", {"placement": "appLogoOverride", "src": icon})
if title:
SubElement(binding, "text").text = "<![CDATA[{}]]>".format(title)
if message:
SubElement(binding, "text").text = "<![CDATA[{}]]>".format(message)
if audio == "silent":
SubElement(toast, "audio", {"silent": "true"})
else:
audio_element = SubElement(
toast, "audio", {"src": audio, "loop": "true" if loop else "false"}
)
if actions:
actions_element = SubElement(toast, "actions")
for action in actions:
SubElement(actions_element, "action", {
"activationType": action.get("type", ""),
"content": action.get("label", ""),
"arguments": action.get("arguments", "")
})
return tostring(toast, encoding="unicode")
def send_notification(app_id, title, message, icon=None, activation_type="protocol",
activation_arguments=None, actions=None, audio="default",
loop=False, duration="short"):
app_id = app_id or "Windows App"
# Generate the XML content
xml_content = build_toast_xml(title, message, icon, activation_type,
activation_arguments, actions, audio, loop, duration)
# Format PowerShell script using the global template
script = SCRIPT_TEMPLATE.format(app_id=app_id, xml_content=xml_content)
try:
subprocess.Popen(["powershell", "-NoProfile", "-NonInteractive", "-Command", script],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
print("Error sending notification: {0}".format(str(e)))
except subprocess.TimeoutExpired:
print("Notification script timed out.")
# Example Usage
if __name__ == "__main__":
send_notification(
app_id="Example App",
title="My notification",
message="Some message about how important something is...",
icon="path/to/icon.png",
actions=[
{"type": "protocol", "label": "Open Maps", "arguments": "bingmaps:?q=sushi"},
{"type": "protocol", "label": "Another Action", "arguments": ""}
],
audio="mail",
loop=False,
duration="short"
) This is completely untested, feel free to try it, fix it and return back with a better version 😉 I've hit the limit for the free gpt-4o prompts, I asked how to further improve the code, here's the answer:
|
As I mentioned in #2493 this is no easy fix on the python side because subprocess doesn't handle unicode properly. We should implement this on the .net/C# side, but I have no incentives to work on that. |
@sanzoghenzo, thank you for the detailed answers. |
✈ Pre-Flight checks
🐞 Describe the bug
All text fields of function forms.toast() do not support Unicode characters.
Please add support.
⌨ Error/Debug Message
♻️ To Reproduce
No response
⏲️ Expected behavior
No response
🖥️ Hardware and Software Setup (please complete the following information)
Additional context
No response
The text was updated successfully, but these errors were encountered: