gotify / server

A simple server for sending and receiving messages in real-time per WebSocket. (Includes a sleek web-ui)
https://gotify.net
Other
11.13k stars 620 forks source link

Cant use emoji with sqlite3 #632

Closed Leproide closed 7 months ago

Leproide commented 8 months ago

Can the issue be reproduced with the latest available release?

Yes

Which one is the environment gotify server is running in?

I need to send notification from powershell script on gotify server. Cant use emoji in the notification, is for the charset? I use sqlite basic db, how i can change or fix the problem in another way?

My code (now without emoji or not work):

# Set the console output encoding to UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Function Send-Gotify {
    Param([Parameter(Mandatory=$true)][String]$Message)

    $GotifyServer = "http://censored"
    $GotifyPort = "Censored"  # Replace with your Gotify port
    $GotifyToken = "censored"
    $CustomText = "Test"

    $RequestBody = @{
        title = "$GotifyTitle $CustomText"
        message = $Message
    } | ConvertTo-Json

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    try {
        $Uri = "$GotifyServer`:$GotifyPort/message"
        Write-Host "Sending Gotify message to $Uri"
        $Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers @{
            "X-Gotify-Key" = $GotifyToken
        } -Body $RequestBody -ContentType 'application/json'

        Write-Host "Gotify message sent successfully. Response: $($Response | ConvertTo-Json -Depth 5)"
    } catch {
        Write-Host "Failed to send Gotify message. Error: $_"
    }
}

# Get the last event with ID 190
$A = Get-WinEvent -MaxEvents 1 -FilterHashTable @{Logname = "Veeam Agent"; ID = 190}

if ($A) {
    $Message = $A.Message
    $EventTime = $A.TimeCreated

    # Format the event date (not time)
    $FormattedEventDate = $EventTime.ToString("dd-MM-yyyy")

    # Check if the event message contains "Success", "Failed", or "Warning"
    if ($Message -match "Success") {
        $GotifyTitle = "[V] $CustomText"  # Checkmark ASCII code
    } elseif ($Message -match "Failed") {
        $GotifyTitle = "[X] $CustomText"  # Cross ASCII code
    } elseif ($Message -match "Warning") {
        $GotifyTitle = "[!] $CustomText"  # Warning ASCII code
    }

    # Check if the event date is equal to the current date
    if ($FormattedEventDate -eq (Get-Date).ToString("dd-MM-yyyy")) {
        $EventTimeString = "Event Time: $($EventTime.ToString("dd-MM-yyyy HH:mm:ss"))"

        # Check if the event is older than 10 minutes
        $TimeDifference = (Get-Date) - $EventTime
        if ($TimeDifference.TotalMinutes -gt 10) {
            exit
        }

        # Concatenate custom string, event time, and message
        $MessageToSend = "$EventTimeString`r`n$Message"

        # Send the message via Gotify
        Send-Gotify $MessageToSend
    } else {
        exit
    }
} else {
    exit
}

Insert emoji/html not work

# Set the console output encoding to UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Function Send-Gotify {
    Param([Parameter(Mandatory=$true)][String]$Message)

    $GotifyServer = "http://censored"
    $GotifyPort = "censored"  # Replace with your Gotify port
    $GotifyToken = "censored"
    $CustomText = "Test"

    # Define HTML entities for real emojis
    $CheckmarkEmoji = "✅"  # Checkmark emoji
    $CrossEmoji = "❌"  # Cross emoji
    $WarningEmoji = "⚠"  # Warning emoji

    $RequestBody = @{
        title = "$GotifyTitle $CustomText"
        message = $Message
    } | ConvertTo-Json

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    try {
        $Uri = "$GotifyServer`:$GotifyPort/message"
        Write-Host "Sending Gotify message to $Uri"
        $Response = Invoke-RestMethod -Uri $Uri -Method Post -Headers @{
            "X-Gotify-Key" = $GotifyToken
        } -Body $RequestBody -ContentType 'application/json'

        Write-Host "Gotify message sent successfully. Response: $($Response | ConvertTo-Json -Depth 5)"
    } catch {
        Write-Host "Failed to send Gotify message. Error: $_"
    }
}

# Get the last event with ID 190
$A = Get-WinEvent -MaxEvents 1 -FilterHashTable @{Logname = "Veeam Agent"; ID = 190}

if ($A) {
    $Message = $A.Message
    $EventTime = $A.TimeCreated

    # Format the event date (not time)
    $FormattedEventDate = $EventTime.ToString("dd-MM-yyyy")

    # Check if the event message contains "Success", "Failed", or "Warning"
    if ($Message -match "Success") {
        $GotifyTitle = "$CheckmarkEmoji $CustomText"  # Checkmark emoji
    } elseif ($Message -match "Failed") {
        $GotifyTitle = "$CrossEmoji $CustomText"  # Cross emoji
    } elseif ($Message -match "Warning") {
        $GotifyTitle = "$WarningEmoji $CustomText"  # Warning emoji
    }

    # Check if the event date is equal to the current date
    if ($FormattedEventDate -eq (Get-Date).ToString("dd-MM-yyyy")) {
        $EventTimeString = "Event Time: $($EventTime.ToString("dd-MM-yyyy HH:mm:ss"))"

        # Check if the event is older than 10 minutes
        $TimeDifference = (Get-Date) - $EventTime
        if ($TimeDifference.TotalMinutes -gt 1000) {
            exit
        }

        # Concatenate custom string, event time, and message
        $MessageToSend = "$EventTimeString`r`n$Message"

        # Send the message via Gotify
        Send-Gotify $MessageToSend
    } else {
        exit
    }
} else {
    exit
}
LaurenceJJones commented 7 months ago

Do you see an error or is that it does not render the emoji?

Because by default the render content is text/plain you can define markdown https://gotify.net/docs/msgextras#clientdisplay

however, I dont know if that would fix the issue as it not exactly markdown

jmattheis commented 7 months ago

Directly use the Unicode character instead of the HTML entity ✅. E.g.

$ echo '{"message": "✅"}' | http POST ':8080/message?token=AATn3SSC7z7Sadd'

image

Leproide commented 7 months ago

Tnx, the problem was the coding of the script, it was not respected by the text editor I used