Messange order
soerenetler opened this issue · 6 comments
Hi everyone,
I know this is not a problem with heyoo, but maybe it should/cpuld be something to be delt with here. I experience wrong message order on the recieving side of the chatbot when sending several messages directly after one antoher. This happens especially when sending images.
When sending thre images (1.jpg, 2jpg, 3.jpg)
client.send_image("1.jpg", to="xxx")
client.send_image("2.jpg", to="xxx")
client.send_image("3.jpg", to="xxx")
This might result in receiving the images in a different order (1,3,2 or 3,1,2)
Do you have a good way already to change this asynchronous behaviour of the WhatsApp Cloud API? Would this the something of interest for this API?
Best,
Sören
Do you have any ideas that can help enforce the synchronous in a scenario like this?
I did create a prototype of this using an external database (reddis) to store all send messages. With the recieve events in the webhook I delete from the database. Before sending a new message I check in the database if there are open messages for that phone number. For now it is not a pretty solution but it might be helpful also for other people. And can hopefully be improved by the community :)
I suggest you to do an if statement with the response code of the previous sent message, e.g.:
msg1 = client.send_image("1.jpg", to="xxx")
try: msg1["error"]
except: msg2 = . . .
Didn't test this, but it should work.
Hi @filipporomani,
I don't think your code will work. (I think it should use if ... else and not try ... except)
The message order is not a problem on getting errors from the API. The delivery speed for text messages and images is very different. So if you send an image first and a text second. The text will overtake the image. This happens on the WhatsApp side. The only thing you can do is to keep track of send messages and delivered messages using the callbacks you get from the WhatsApp Cloud API and only send a new message once all other messages a delivered.
I know that. The issue is that the send_image() function returns the request json, so the only way to wait is to check the json content.
Ofc u can keep track of the sent messages, but that requires a bit more of code.
When you send a message using the cloud api, your webhook receives 2 requests:
- The first call has a status of "sent"
- The second call has a status of "delivered"
To ensure that messages are sent in order, you first have to check the status of the previous message. If the message was "delivered", then you can proceed to send the second message.