Package: whatsapp-cloud-sdk
WhatsApp Python SDKThe first ever and the best python unofficial library written for theWhatsapp Cloud APIProject InformationTable of ContentsIntroductionAcknowledgementsPrerequisitesWhatsapp Cloud API SupportInstallationGetting StartedBasic UsageContributingLicenseIntroductionThe WhatsApp Python SDK is a Python library that provides a comprehensive solution for managing WhatsApp Cloud interactions using the WhatsApp Cloud API. It allows you to send various types of messages, mark messages as read, and handle incoming webhooks. The library is designed to be flexible and can be easily integrated into your Python projects.
This module is meant to be purely used with python async and await.
This is the v1 of this open source project so not all whatsapp cloud features are currently supported. If you need any feature to be added please open a feature request!Key Features:Send text messages with or without buttonsSend image, audio, video, document, location, contact, and sticker messagesReceive and process incoming messages via webhooksSet up a callback function to handle incoming messagesEasy-to-use and customizable and lot more!AcknowledgementsGod first of all.Whatsapp Cloud APIAuthors@Akpojiyovwi Tega(Me)PrerequisitesBefore you begin, ensure you have met the following requirements:Python 3.7+FastAPIuvicornWhatsApp API Cloud Access Token (obtained from WhatsApp)Basic understanding of how the Whatsapp API worksWhatsapp Cloud API SupportThe following types of the Whatsapp Cloud API v17.0 are supported.Normal messagessend textsend contact messagesend reaction messagesLink messagessend audio via linksend image via linksend video via linksend document via linksend sticker via linkReply to a user message using linksend reply to message using an audio linksend reply to message using an image linksend reply to message using a video linksend reply to message using a document linksend reply to message using a reaction linkReply to a user messagessend reply with a textInstallationYou can install or upgradewhatsapp-cloud-sdkvia:$pipinstallwhatsapp-cloud-sdk--upgradeYou can also install directly from the repo, though this is usually not necessary.$gitclonehttps://github.com/t-ega/whatsapp-cloud-sdk
$cdwhatsapp-cloud-sdk
$pipinstallpoetry
$poetryinstallGetting StartedSet Up Environment VariablesTo run this project, you will need to add the following environment variables to your .env file
Otherwise you would need to pass it as an argument to thewhatsappclass.CLOUD_API_ACCESS_TOKENWA_PHONE_NUMBER_IDWA_VERSION='v17.0'Import the necessary classes and modules:fromwhatsapp_cloud_sdkimportWAManagerfromwhatsapp_cloud_sdkimportBot# Create a WhatsApp manager instancewhatsapp=WAManager(cloud_api_access_token="your_access_token",wa_phone_number_id="your_phone_number_id",version="v17.0")# Set up a callback function to handle incoming messages:# **NOTE: The callback function must be an asynchronous function!**asyncdefhandle_message(request:Request,message:Message):print(message.type)Start the FastAPI server to handle incoming webhooks:whatsapp.run_server(callback=handle_message,webhook_url="/webhook",port=8000,verify_token="your_verify_token")Basic UsageReceive and handle incoming messages in the callback function:asyncdefhandle_message(request:Request,message:Message):print("Received a message of type:",message.type)# Your custom logic to handle the incoming message# reply to the message recievedawaitmessage.reply_text(text="This is a reply")Send text messages:whatsapp.bot.send_text("Hello, world!","recipient_number")Send text messages with a list of dict buttons:buttons=[{"title":"Option 1","id":"option1"},{"title":"Option 2","id":"option2"},]awaitwhatsapp.bot.send_text_with_buttons("Choose an option:",buttons,"recipient_number")Send other types of messages (image, audio, video, etc.):Send an image by URLawaitwhatsapp.bot.send_image_by_url("https://example.com/image.jpg","Image caption","recipient_number")Send a reply to a message recievedasyncdefhandle_message(request:Request,message:Message):awaitmessage.reply_text(text="Heyy")Get the type of message that was receivedasyncdefhandle_message(request:Request,message:Message):print("Received a message of type:",message.type)Mark a message as read (although this is done automatically by the bot)asyncdefhandle_message(request:Request,message:Message):awaitmessage.mark_as_read()For more details on available methods and usage, refer to the documentation(in progress).HelpCurrently this project is only maintained by me, I am looking forward to accepting pull requests. If you need help just open an issue
I would try my best to attend to all issues.InspirationThis project was inspired by theWhastpp Nodejs SDK. Furthermore it was also inspired by thePython Telegram Bot wrapperContributingContributions are welcome! Please see the Contribution Guidelines for more information.You can also help by reporting bugs or feature requests.LicenseYou may copy, distribute and modify the software provided that modifications are described and licensed for free under LGPL-3. Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under LGPL-3, but applications that use the library don't have to be.DisclaimerThis module is not officially affiliated with WhatsApp or Facebook. It's an independent project developed by the community.Note:
For more detailed information about the methods and classes provided by the whatsapp-cloud-api module, refer to the module's source code or docstrings.