Skip to content
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

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 this issue Oct 9, 2024 · 6 comments

Comments

@BaeKey
Copy link

BaeKey commented Oct 9, 2024

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
Copy link
Contributor

wunter8 commented Oct 10, 2024

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
Copy link
Author

BaeKey commented Oct 10, 2024

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
Copy link
Contributor

wunter8 commented Oct 10, 2024

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
Copy link
Author

BaeKey commented Oct 10, 2024

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
Copy link
Contributor

wunter8 commented Oct 11, 2024

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
Copy link
Author

BaeKey commented Oct 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants