jellyfin / jellyfin-plugin-webhook

GNU General Public License v3.0
130 stars 41 forks source link

Using Ntfy Template: "invalid request: request body must be valid JSON" #173

Open nothing2obvi opened 1 year ago

nothing2obvi commented 1 year ago

The Ntfy template does not work as is. I saw the README file that said to use the root URL of the ntfy server, but when doing that I get no message at all and the logs indicate that the request body is not a valid JSON. As per the README I also tried editing the request header to include an authorization header for an admin ntfy user, though I don't need to do so given the permissions I've set for the "jellyfin" topic, and that didn't solve the problem either.

I also edited the logging.default.json (couldn't find logging.json), hence the [DBG] in the logs.

today at 10:37:19 PM[22:37:19] [DBG] [60] Jellyfin.Plugin.Webhook.Destinations.Generic.GenericClient: SendAsync Body: {        
today at 10:37:19 PM    "topic": "jellyfin",
today at 10:37:19 PM    "tags": ["octopus"],
today at 10:37:19 PM        "title": "harry Playback Started",
today at 10:37:19 PM        "priority": 3,
today at 10:37:19 PM        "attach": "my.jellyfin.server/Items/f6ec7b26-d5f6-bdb3-bbdd-30db82b0fbfc/Images/Primary",
today at 10:37:19 PM                "message": "harry started playing:\n\nMr. Osomatsu (2015)\nS02E08 - Synthesis / Jyushimatsu and the Dolphin / Totoko and Nyaa\nDayon uses Dekapan's latest invention to merge various people together into new beings, with a fiendish end goal in mind. Jyushimatsu becomes determined to get a job at a sea park as an actual dolphin, receiving strict training from a female trainer. Meanwhile, Totoko and Nyaa encounter each other at Chibita's oden stand.\n\non my-server-name",
today at 10:37:19 PM    
today at 10:37:19 PM    
today at 10:37:19 PM    
today at 10:37:19 PM}
today at 10:37:19 PM[22:37:19] [WRN] [44] Jellyfin.Plugin.Webhook.Destinations.Generic.GenericClient: Notification failed with POST request to http://10.0.0.94:5780/: {        
today at 10:37:19 PM    "topic": "jellyfin",
today at 10:37:19 PM    "tags": ["octopus"],
today at 10:37:19 PM        "title": "harry Playback Started",
today at 10:37:19 PM        "priority": 3,
today at 10:37:19 PM        "attach": "https://my.jellyfin.server/Items/f6ec7b26-d5f6-bdb3-bbdd-30db82b0fbfc/Images/Primary",
today at 10:37:19 PM                "message": "harry started playing:\n\nMr. Osomatsu (2015)\nS02E08 - Synthesis / Jyushimatsu and the Dolphin / Totoko and Nyaa\nDayon uses Dekapan's latest invention to merge various people together into new beings, with a fiendish end goal in mind. Jyushimatsu becomes determined to get a job at a sea park as an actual dolphin, receiving strict training from a female trainer. Meanwhile, Totoko and Nyaa encounter each other at Chibita's oden stand.\n\non my-server-name",
today at 10:37:19 PM    
today at 10:37:19 PM    
today at 10:37:19 PM    
today at 10:37:19 PM}
today at 10:37:19 PM[22:37:19] [WRN] [44] Jellyfin.Plugin.Webhook.Destinations.Generic.GenericClient: Notification failed with response status code BadRequest: {"code":40024,"http":400,"error":"invalid request: request body must be valid JSON"}
nothing2obvi commented 1 year ago

I was able to get it to work by removing the {{#if_equals ItemType 'Episode'}} section from "PlaybackStart" and "PlaybackStop" and leaving only the first or second "message" part, removing a comma at the end. It looks something like below, in case anyone runs into the problem I had. I only kept the "PlaybackStart" and "PlaybackStop" sections because that's all I wanted; I also removed "Overview" and "ServerName."

Lastly, I wanted to point out that there is a typo in the Ntfy template. In the "PlaybackStart" section it says "stopped" where it should say "started."

Although I got it to work, I'd like to leave this issue open because the template provided doesn't work as is.

For all item types except Movies

{        
    "topic": "jellyfin",
    "tags": ["octopus"],
    {{#if_equals NotificationType 'PlaybackStart'}}    
        "title": "{{{NotificationUsername}}} Playback Started",
        "priority": 3,
        "attach": "{{{ServerUrl}}}/Items/{{{ItemId}}}/Images/Primary",
        "message": "{{{NotificationUsername}}} started playing:\n\n{{{SeriesName}}} ({{Year}})\nS{{SeasonNumber00}}E{{EpisodeNumber00}} - {{{Name}}}" 
    {{/if_equals}}

    {{#if_equals NotificationType 'PlaybackStop'}}  
        "title": "{{{NotificationUsername}}} Playback Stopped",
        "priority": 3,
        "attach": "{{{ServerUrl}}}/Items/{{{ItemId}}}/Images/Primary",
        "message": "{{{NotificationUsername}}} stopped playing:\n\n{{{SeriesName}}} ({{Year}})\nS{{SeasonNumber00}}E{{EpisodeNumber00}} - {{{Name}}}"  
    {{/if_equals}}    

}

For Movies

{        
    "topic": "jellyfin",
    "tags": ["octopus"],
    {{#if_equals NotificationType 'PlaybackStart'}}    
        "title": "{{{NotificationUsername}}} Playback Started",
        "priority": 3,
        "attach": "{{{ServerUrl}}}/Items/{{{ItemId}}}/Images/Primary",
        "message": "{{{NotificationUsername}}} started playing:\n\n{{{Name}}} ({{Year}})" 
    {{/if_equals}}

    {{#if_equals NotificationType 'PlaybackStop'}}  
        "title": "{{{NotificationUsername}}} Playback Stopped",
        "priority": 3,
        "attach": "{{{ServerUrl}}}/Items/{{{ItemId}}}/Images/Primary",
        "message": "{{{NotificationUsername}}} stopped playing:\n\n{{{Name}}} ({{Year}})"
    {{/if_equals}}    

}
hardwareadictos commented 1 year ago

Edit:

I tested the original template for ntfy and i don't get the malformed json thing, i get this:

Jellyfin-MTM01 | [12:05:31] [INF] [70] Emby.Server.Implementations.ScheduledTasks.TaskManager: IntervalTrigger fired for task: Webhook Item Added Notifier
Jellyfin-MTM01 | [12:05:31] [INF] [70] Emby.Server.Implementations.ScheduledTasks.TaskManager: Queuing task ItemAddedScheduledTask
Jellyfin-MTM01 | [12:05:31] [INF] [70] Emby.Server.Implementations.ScheduledTasks.TaskManager: Executing Webhook Item Added Notifier
Jellyfin-MTM01 | [12:05:31] [DBG] [70] Jellyfin.Plugin.Webhook.Notifiers.ItemAddedNotifier.ItemAddedManager: ProcessItemsAsync
Jellyfin-MTM01 | [12:05:31] [INF] [70] Emby.Server.Implementations.ScheduledTasks.TaskManager: Webhook Item Added Notifier Completed after 0 minute(s) and 0 seconds

But i dont' get any message on ntfy... :(

Edit 2:

Rear more testing i found this on logs:

Jellyfin-MTM01 | [12:38:21] [ERR] [47] Jellyfin.Plugin.Webhook.WebhookSender: Unable to send notification
Jellyfin-MTM01 | System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]. Path: $.tags | LineNumber: 2 | BytePositionInLine: 13.
Jellyfin-MTM01 |  ---> System.InvalidOperationException: Cannot get the value of a token type 'StartArray' as a string.
Jellyfin-MTM01 |    at System.Text.Json.Utf8JsonReader.GetString()
Jellyfin-MTM01 |    at System.Text.Json.Serialization.JsonDictionaryConverter`3.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TDictionary& value)
Jellyfin-MTM01 |    at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
Jellyfin-MTM01 |    at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
Jellyfin-MTM01 |    --- End of inner exception stack trace ---
Jellyfin-MTM01 |    at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
Jellyfin-MTM01 |    at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
Jellyfin-MTM01 |    at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
Jellyfin-MTM01 |    at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
Jellyfin-MTM01 |    at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
Jellyfin-MTM01 |    at Jellyfin.Plugin.Webhook.Destinations.GenericForm.GenericFormClient.SendAsync(GenericFormOption option, Dictionary`2 data)
Jellyfin-MTM01 |    at Jellyfin.Plugin.Webhook.WebhookSender.SendNotification[T](IWebhookClient`1 webhookClient, T option, Dictionary`2 itemData, Type itemType)

Had to reboot the container to get this.

itzteajay-glitch commented 1 year ago

I just started setting this plugin up and I also got malformed json coming out of it.

[2023-07-11 16:50:20.742 -07:00] [WRN] [34] Jellyfin.Plugin.Webhook.Destinations.Discord.DiscordClient: Notification failed with "POST" request to https://discord.com/api/webhooks/1128**********81985/dU6************************LD3P: "{
[2023-07-11 16:50:20.742 -07:00] [WRN] [34] Jellyfin.Plugin.Webhook.Destinations.Discord.DiscordClient: Notification failed with response status code BadRequest: "{\"message\": \"The request body contains invalid JSON.\", \"code\": 50109}"
hardwareadictos commented 1 year ago

I think that is not maintained anymore, no answers from months. There are no updates on other issues anymore also...

elendil7 commented 2 months ago

+1 same issue here

zivkovic commented 4 weeks ago

I fixed it up a little, but the following is working for me (Currently only using ItemAdded and PendingRestart). The fix I made was to ItemAdded part, which always created two "title" and "message" fields.

{
    "topic": "jellyfin",
    "tags": ["octopus"],
    {{#if_equals NotificationType 'PlaybackStart'}}
        "title": "{{{NotificationUsername}}} Playback Started",
        "priority": 3,
        "attach": "{{{ServerUrl}}}/Items/{{{ItemId}}}/Images/Primary",
            {{#if_equals ItemType 'Episode'}}
                "message": "{{{NotificationUsername}}} started playing:\n\n{{{SeriesName}}} ({{Year}})\nS{{SeasonNumber00}}E{{EpisodeNumber00}} - {{{Name}}}\n{{Overview}}\n\non {{{ServerName}}}"
            {{else}}
                "message": "{{{NotificationUsername}}} started playing:\n\n{{{Name}}} ({{Year}})\n{{Overview}}\n\non {{{ServerName}}}"
            {{/if_equals}}
    {{/if_equals}}

    {{#if_equals NotificationType 'PlaybackStop'}}
        "title": "{{{NotificationUsername}}} Playback Stopped",
        "priority": 3,
        "attach": "{{{ServerUrl}}}/Items/{{{ItemId}}}/Images/Primary",
            {{#if_equals ItemType 'Episode'}}
                "message": "{{{NotificationUsername}}} stopped playing:\n\n{{{SeriesName}}} ({{Year}})\nS{{SeasonNumber00}}E{{EpisodeNumber00}} - {{{Name}}}\n{{Overview}}\n\non {{{ServerName}}}"
            {{else}}
                "message": "{{{NotificationUsername}}} stopped playing:\n\n{{{Name}}} ({{Year}})\n{{Overview}}\n\non {{{ServerName}}}"
            {{/if_equals}}
    {{/if_equals}}

    {{#if_equals NotificationType 'ItemAdded'}}
    "priority": 3,
    "attach": "{{{ServerUrl}}}/Items/{{{ItemId}}}/Images/Primary",
        {{#if_equals ItemType 'Movie'}}
            "title": "Movie Added",
            "message": "{{ItemType}} Now Available\n\n{{{Name}}} ({{Year}})\n{{Overview}}\n\nRuntime\n{{RunTime}}\n\nStatus\nAvailable"
        {{else}}
            {{#if_equals ItemType 'Season'}}
                "title": "Season Added",
                "message": "{{ItemType}} Now Available\n\n{{{SeriesName}}} ({{Year}})\n{{{Name}}}\n{{Overview}}\n\nRuntime\n{{RunTime}}\n\nStatus\nAvailable"
            {{else}}
                "title": "Episode Added",
                "message": "Episode Now Available\n\n{{{SeriesName}}} ({{Year}})\nS{{SeasonNumber00}}E{{EpisodeNumber00}} - {{{Name}}}\n{{Overview}}\n\nRuntime\n{{RunTime}}\n\nStatus\nAvailable"
            {{/if_equals}}
        {{/if_equals}}
    {{/if_equals}}

    {{#if_equals NotificationType 'PendingRestart'}}
        "title": "Jellyfin Restart Required",
        "priority": 4,
        "message": "Jellyfin needs to be restarted, please restart jellyfin as soon as possible!"
    {{/if_equals}}
}