binwiederhier / ntfy

Send push notifications to your phone or desktop using PUT/POST
https://ntfy.sh
Apache License 2.0
18.54k stars 729 forks source link

When subscribing to messages via an API using the raw stream, how should the messages be split in order to retrieve the latest ones? #1196

Open BaeKey opened 1 month ago

BaeKey commented 1 month ago

After reviewing the documentation, I couldn't find any query parameters that allow fetching the latest messages. I want to use iOS Shortcuts to retrieve the latest messages for a specified topic. However, using either /json or /sse, iOS Shortcuts currently can't parse the file content correctly. If I use the raw format, I'm unable to properly split the content. I have also found a similar issue to mine.

How to get the latest message of a topic in JSON format from the API?

Could you consider adding a filter parameter to retrieve the latest message, or advise me on how to correctly parse the messages from the raw format?

wunter8 commented 1 month ago

Here are the actions that worked for me. This will show a pop-up/alert/notification with the message of the most recent message sent to the topic

  1. Get contents of URL: https://ntfy.sh/mytopic/raw?poll=1
  2. Split text: Split "Contents of URL" by "New Lines" (make sure "Contents of URL" treats the URL response as "Text")
  3. Get item from list: Get "Last Item" from "Split Text"
  4. Show alert: "Item from list"
BaeKey commented 1 month ago

Here are the actions that worked for me. This will show a pop-up/alert/notification with the message of the most recent message sent to the topic

  1. Get contents of URL: https://ntfy.sh/mytopic/raw?poll=1
  2. Split text: Split "Contents of URL" by "New Lines" (make sure "Contents of URL" treats the URL response as "Text")
  3. Get item from list: Get "Last Item" from "Split Text"
  4. Show alert: "Item from list"

Thank you for your explanation. However, when using the method of splitting 'Contents of URL' by 'New Lines,' sometimes when dealing with multi-line text, I encounter an issue where only the last line of the text is extracted, and I haven't been able to solve it.

wunter8 commented 1 month ago

When I send multi-line text and read it back in the "raw" stream, it's collapsed onto one line, so it splits fine in the iOS Shortcut. Are you saying it doesn't collapse onto one line for you?

BaeKey commented 1 month ago

When I send multi-line text and read it back in the "raw" stream, it's collapsed onto one line, so it splits fine in the iOS Shortcut. Are you saying it doesn't collapse onto one line for you?

This issue might be related to my code. When I use a Python script to automatically send clipboard content, I use the following code:

def send_clipboard(msg):
    requests.post("https://%s" % (URL),
                  data=msg.encode(encoding='utf-8'),
                  headers={
                      "Authorization": TOKEN if TOKEN != "-" else "",
                      "Title": CLIENTNAME
                  })

During testing, when sending multi-line text, the iOS Shortcut only receives the last line of text.

Thank you for your help. Perhaps I can fix this by modifying the request I’m sending.

wunter8 commented 1 month ago

Hmm. I tested your script (using python 3.10). The message printed on two lines in the console and in the web app, but in the "raw" stream, it was collapsed onto one line for me. I was able to grab the whole "test message" in the iOS Shortcut.

import requests
URL = 'ntfy.sh/mytopic'
TOKEN = '-'
CLIENTNAME = 'test'

def send_clipboard(msg):
    requests.post("https://%s" % (URL),
                  data=msg.encode(encoding='utf-8'),
                  headers={
                      "Authorization": TOKEN if TOKEN != "-" else "",
                      "Title": CLIENTNAME
                      })

if __name__ == '__main__':
    send_clipboard('test\nmessage')
    print('test\nmessage')

This printed on two lines in the console and in the web app

BaeKey commented 1 month ago

Hmm. I tested your script (using python 3.10). The message printed on two lines in the console and in the web app, but in the "raw" stream, it was collapsed onto one line for me. I was able to grab the whole "test message" in the iOS Shortcut.

import requests
URL = 'ntfy.sh/mytopic'
TOKEN = '-'
CLIENTNAME = 'test'

def send_clipboard(msg):
    requests.post("https://%s" % (URL),
                  data=msg.encode(encoding='utf-8'),
                  headers={
                      "Authorization": TOKEN if TOKEN != "-" else "",
                      "Title": CLIENTNAME
                      })

if __name__ == '__main__':
    send_clipboard('test\nmessage')
    print('test\nmessage')

This printed on two lines in the console and in the web app

I copied this paragraph at random:

  Failed to create a virtual machine in vCenter: PBM error occurred during PreCreateCheckCallback: Connection refused. 
  dinghui.org has been viewed 402 times on the VMware tag.
  Failed to create a virtual machine via vCenter: PBM error occurred during PreCreateCheckCallback: Connection refused.

  Additionally, I found that migrating the virtual machine also cannot be completed.

  However, creating a virtual machine can be done on the ESXi host, so the idea is to check the services of vCenter.

When I send this, iOS Shortcut can only receive the last sentence.