I would like to publish a set (~100) of messages at once to an MQTT broker. To do so I will use publish.multiple().
The messages are of the form topic/one, topic/two, ... with each a payload. The order for these messages does not matter.
I need, however, to send first a message to topic with an empty payload in order to delete the existing messages. I would like to place it first in the list of messages to be sent.
Is there a guarantee that it will be processed first?
Looking at the paho publish code, it invokes Collections.popleft() so if you pass a tuple that has the no-op payload first, I believe it should work.
Related
I am trying to use slack web client API to pull slack messages including threads for a specific date. The conversations.history API only returns the parent message in the case of threaded messages. There is conversations.replies API that returns the message threads, but it requires ts of the parent message to be passed in the request so it will only return conversations related to one thread.
Is there a way to pull all message history including replies for a data range rather than having to combine a call to conversations.history API and then multiple calls to conversations.replies for each message with thread_ts?
This approach of combining both APIs won't work if the reply was posted on the specific date we want to pull, but the root thread message was posted on an older date. The root message won't be returned in conversations.history and hence we won't be able to get that particular message in the thread using conversations.replies.
It's strange that Slack doesn't provide such API to pull all messages including threaded ones.
Unfortunately, there is no way to capture all threads in a workspace with a single API call. The conversations.history is already a very data-heavy method. Most developers calling this method don't need thread information and including that in the reply would be a bit of an overkill. Calling conversations.replies should return all the replies corresponding to that parent message regardless of the date it was posted in unless otherwise specified using the latest or oldest parameters.
Is there an elegant way to pull one message off the broker without having to:
subscribe
create an on_message()
receive the message
unsubscribe
I ask because we are using a json message which has multiple fields. When new data comes in I want to ONLY update that particular field in the json message but not remove the rest of the data. Since we have a TON of these json topics, we don't really want to keep all of them in program memory (also in case the program has to be relaunched). On top of that, this program could be running for months without supervision.
So ideally, I'd like to post the json message to an ID'd topic with the retain flag set to True. Then when new data comes in for that ID, I do a pull of the info on that topic, update that particular field in the json message and repost to the same topic.
I can post example code but I'm hoping there is a simple function that I am unaware of.
Thanks, in advance, for any suggestions.
The Paho Python client comes with a set of help classes that do this single shot type of pattern for you.
Doc here
e.g. the following connects to a broker, subscribes to a topic and returns on receipt of the first message on that topic.
import paho.mqtt.subscribe as subscribe
msg = subscribe.simple("paho/test/simple", hostname="mqtt.eclipse.org")
print("%s %s" % (msg.topic, msg.payload))
And the matching publish call:
import paho.mqtt.publish as publish
publish.single("paho/test/single", "payload", hostname="mqtt.eclipse.org")
I don't think that is possible. You say "When new data comes in..." That's exacty why you need to subscribe and use the callback function. That's basically a "pull when something is actually there".
Just to get an idea of how it should work: you are sending that json message via MQTT, right? And you are re-sending it when it changes?
But you don't have to keep them all in the RAM. You could use a retained message in combination with a fixed topic (not ID'ed) and send the ID in the message.
If you use retained messages with ID'ed topics, that might fill the memory.
What does the ID stand for? A uniqie number? Something like a timestamp? A hash? The sender?
I think you can solve that problem by clearly separating your things, e.g. say in data and message, where data is something you maintain in Python (e.g. a database or something in RAM) and message is something that you acually send / receive via MQTT.
Then you can add / send / update data depending on what is received in MQTT and you don't have to send / update the complete set.
I'm attempting to lay the foundation for a relatively simple messaging system using Twilio API and its Python wrapper. Users should be able to text a keyword and receive back a string of responses.
The problem is, using the suggested HttpResponse() package from Twilio's documentation, when I loop through the messages to add them to a response chain, they hit Twilio in a block, and sends the service sends them in a seemingly random order, which really compromises the readability of the block.
Here's a simplified version of the loop I'm running in Django to create the response:
#csrf_exempt
def inbox(request)
if request.method == "POST":
# Interpret the keyword in the incoming message
data = request.POST
messageBody = data['Body'].lower()
# Check if it matches a valid keyword
targetTrigger = Keyword.objects.get(trigger_word=messageBody)
# Pull the appropriate responses
messageChain = Message.objects.filter(keyword_answers=targetTrigger)
# Create the messaging response
resp = MessagingResponse()
# Populate it with messages
for sms in messageChain:
resp.message(sms)
# Send them to Twilio
return HttpResponse(str(resp))
I've glossed over the try and error catches for the sake of readability. And like I said, this sends the messages in a seemingly random order, with shorter messages appearing to send through to my iPhone first more of the time. Not always, but enough for me to need to rethink this method.
Strangely there's little in the documentation about sending multiple messages in a SMS HttpResponse, though I imagine it's a common use case. The alternative I was considering is sending back a blank HttpResponse to simply acknowledge to Twilio the message was received successfully, and then using its regular one-by-one sending method with my for loop. That seems a little less efficient, but I need accuracy in the order the messages send.
Any suggestions? Twilio developer evangelists, I know you all are out here.
According to Twilio, this cannot be done. as they simply do not have control over the delivery process.
Twilio cannot guarantee that SMS messages sent from your Twilio phone number will arrive in order. While we will send the SMS messages you pass to us in the order that you’ve queued them, the SMS messages are delivered individually with no association to each other. The order of delivery depends on the carrier.
To help your users understand the order of your messages, we recommend
that you append a page reference following each message, for example
1/3, 2/3, 3/3.
In SMS, the shortest message is typically sent the fastest. You could potentially design your messages to be ordered by size. That might reduce the frequency of error.
Another option might be to bundle them all into one large 1600 character message and having the carrier deal with breaking it apart. It would depend on whether concatenated SMS is reliably supported by carriers in your client base. That might deal with the order issue.
If a message is sent to the topic in an mqtt broker, I want to know the topic by Python. In order to use client.subscribe (), I have to manually enter a topic, so I need to know the topic before client.subscribe() dynamically. Is there a way to know what broker topics are?
If you send "EVERY" message to broker with retain message = True, then you can:
Connects to server with subscribe '#'
Check all the retain message and their topic (then you can got all topics)
Unsubscribe '#'
Subscribe the topic you want
This solution subscribe twice, may not fit your original requirement (only subscribe once), but can do what you want
I'm trying to use paho.mqtt for python (project pages) and all works nice. The only problem I have is I would find it very useful to find out who had sent the message. I looked up the source code but could not quite get my head around if the client variable passed within on_message is the client I use to connect to or details of the client who published the message (I'm guessing it's the first option).
So the question is - is it possible to find out who (the user name) had sent the message?
The MQTT protocol was designed to be as light weight as possible, this means that the message header contains the absolute bare minimum to deliver a message to a specific topic. There is no room in the header for anything else.
MQTT is also a Pub/Sub protocol, one of the key features of this type of protocol is to decouple the publisher from the subscriber as much as possible. This means that the publisher shouldn't care how many subscribers there are and subscribers shouldn't care where the information comes from as long as it is to a topic it's interested in.
If you want any more information other than the message topic then you have to add it to the payload yourself.