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

Messange order #67

Open
soerenetler opened this issue Feb 13, 2023 · 6 comments
Open

Messange order #67

soerenetler opened this issue Feb 13, 2023 · 6 comments

Comments

@soerenetler
Copy link
Collaborator

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

@Kalebu
Copy link
Contributor

Kalebu commented Feb 22, 2023

Do you have any ideas that can help enforce the synchronous in a scenario like this?

@soerenetler
Copy link
Collaborator Author

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 :)

@filipporomani
Copy link
Contributor

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.

@soerenetler
Copy link
Collaborator Author

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.

@filipporomani
Copy link
Contributor

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.

@tanaka-mambinge
Copy link

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.

Link to section explaining this in the documentation

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

4 participants