Skip to content
Omigirl edited this page Dec 5, 2024 · 4 revisions

If you want to use the async version of the library, you can use the AsyncWhatsApp class. This class is a different implementation of the WhatsApp class, and it's based on the asyncio library. This means that you can use the await keyword to call the methods of the class. You must also use the AsyncMessage class instead of the Message class.

The key benefit of using the async version is that you can send multiple messages at the same time without blocking the main thread - but this can also lead to some issues if you're not careful.

Here's an example of how to use the async version of the library:

from whatsapp import AsyncWhatsApp, AsyncMessage
import asyncio

messenger = AsyncWhatsApp('TOKEN',  phone_number_id={"key": 'xxxxxxxxx', "key1": 'yyyyyyyyy'}, logger=True, update_check=True, debug=False, version="latest")

msg = app.create_message(to="DESTINATION_PHONE_NUMBER", content="Hello world") # the create message function is not awaited as it doesn't call any API




async def main():
    # we send the message and the call is saved for later as .send() doesn't wait for the request to be finished.
    msg = await msg.send()

    # Arbitrary 5 second sleep time so we are sure the request is closed - a while cycle can also be used.
    await asyncio.sleep(5)

    # check if the message request is closed and print the result in the terminal
    if msg.done():
        print(f"Result: {msg.result()}")

asyncio.run(main())

In this example, we define an async function called send_message that sends a message using the AsyncWhatsApp class. We then define a main function that calls the send_message function and waits for the message to be sent. Finally, we use the asyncio.run function to run the main function.

The main issue with using the async version of the library is that you need to be careful when sending multiple messages at the same time. If you send too many messages too quickly, you may run into rate limiting issues or messages might be delivered in the wrong order. To avoid these issues, you should use the asyncio library to manage the sending of messages and make sure that you're not sending too many messages at once.

You should send messages in a controlled way, and make sure that you're not sending too many messages at once. You should also make sure that you're handling any errors that occur when sending messages, and that you're not blocking the main thread while sending messages.

Depending on the code you are building, you might also need to add a waiting time before exiting the software to allow all the tasks to complete.

The rest of the documentation is the same as the sync version of the library, so you can refer to all the other pages! However, when trying to send messages or to query the API, you must await every method call.

If you have any questions or issues, feel free to open an issue as this is a new feature and there might be some bugs that I haven't caught yet. I'll be happy to help you out!

Clone this wiki locally