-
-
Notifications
You must be signed in to change notification settings - Fork 18
Async
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 = messenger = AsyncWhatsApp('TOKEN', phone_number_id={"key": 'xxxxxxxxx', "key1": 'yyyyyyyyy'}, logger=True, update_check=True, debug=False, version="latest")
async def send_message():
message = AsyncMessage("Hello, world!")
await messenger.send_message(message)
async def main():
msg = await send_message()
await asyncio.sleep(5)
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!
If you find any issue in the docs, please open an issue/pr and report it