Whatsapp Cloud API: How to send WhatsApp messages from Python?

Rafi
4 min readMay 22, 2022

--

WhatsApp has launched its cloud-based API services for developers all around the world to build custom dashboards and ease of interaction from small to large scale business and their use cases. This will open up a new horizon for building applications with WhatsApp services as a backend API. However, all the data will be sent through the WhatsApp server so it will be fast with the comprise of data that will be sent to their servers.

To start sending messages from WhatsApp API you will need to have the access token and phone number id which you will get by following the steps given below from the Facebook developer account. Once we get the credentials, they will be used in the python script to send messages to WhatsApp.

Setup command

# For Windows Machine

pip install --upgrade heyoo

#For Linux | MAC Machine

pip3 install --upgrade heyoo

Scripts to trigger messages in WhatsApp:

For getting the access_token and phone_number_id you have to follow steps 1–7 given below and run the script below to see the magic.

from heyoo import WhatsApp
messenger = WhatsApp('EABJj1xxxxxx',phone_number_id='1130xxxxxxxx')
# For sending a Text messages
messenger.send_message('Hello I am WhatsApp Cloud API', '91989155xxxx')
# For sending an Image
messenger.send_image(
image="https://i.imgur.com/YSJayCb.jpeg",
recipient_id="91989155xxxx",
)

Setup for credentials

  1. Goto the Facebook developer page: developers. facebook.com and click on the My Apps in the top right corner of the page to create an app.

2. Create an app using the create app button. you can see the list of apps you have created as a grid in the section below create an app.

3. Select the type of app you want to create to use the WhatsApp chat API. For now, you can select the Business type and click next.

4. In this step, you have to provide additional information about the application for instance- name, email id, and business account is optional leave it for now and hit the create app button.

5. Now you have to re-enter your password for final confirmation to create an app. Once you have entered it and pressed the submit button to continue further.

6. Scroll down to WhatsApp in this section and click on the setup button as shown below.

7. After clicking the setup page you will land on the below page where you will get the access token, and phone number id which will be used in our python script. Finally, you have to verify the phone number to which you want to send the message. You will receive an OTP for verification and you are done with the setup. Now you can test the app by clicking on the send message from the dashboard or the python script described above.

The WhatsApp API services can be leveraged into building multiple applications including online product customer services and building a custom dashboard for a support system.

It can also be used as an application of Artificial intelligence to send reports, images, etc for anomalies in computer vision projects to the admin WhatsApp account.

You can follow me on Linkedin: — https://www.linkedin.com/in/rafiuddin-khan-53b58191

References:

  1. https://developers.facebook.com/
  2. https://github.com/Neurotech-HQ/heyoo

--

--

Rafi

Deep Learning, Computer-Vision, Object Detection, CNN architecture, Jetson Nano, Android-AI