Whatsapp API send message Python

How to Send a Message by WhatsApp API using Python

Whapi.Cloud provides a gateway to interact with customers through WhatsApp. This tutorial will help you learn how to send messages through the WhatsApp API using the Python language.

Before you get started, make sure that Python and the requests library are installed on your machine or server.

Getting Started

After registering on our platform, go to the channel page, which will be immediately available to you.

You will now need to link any number to the channel. To do this, use the QR code that is displayed inside the channel.

Open your WhatsApp mobile app, navigate to Settings -> Linked devices -> Link a device -> Scan QR code

Our service enables you to interact with the API without the need for an uninterrupted phone connection. Operate WhatsApp on up to four paired devices and one mobile device concurrently.

Now save the generated API-Key token on channel page:

How to Send WhatsApp API Message using Python, Example

Before we delve into writing the code, let's install the requests library, which we'll use to send HTTP requests. You can do this by running pip install requests in your terminal.

To send a text message, you need to make a request to:

POST https://gate.whapi.cloud/messages/text

Here's how you can dispatch a WhatsApp message using Python:

import requests

url = "https://gate.whapi.cloud/messages/text"

payload = {
    "typing_time": 0,
    "to": "14409416972",
    "body": "Hello"
}
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": "Bearer iNdBBvVwcPwcX1S4sbBmpzBDChVo2bcs"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)

To run this script, save it in a .py file (e.g., whapi_script.py), open your terminal, navigate to the directory containing your script, and run the command python whapi_script.py. The message in the 'body' will be dispatched to the WhatsApp number specified in 'to'. Remember to substitute the 'to' and 'token' values with your actual information.

The parameters are as follows:

  • to: The recipientโ€™s WhatsApp number in international format (e.g., 14409416972) or chatID for contact or group (e.g 14409416972@s.whatsapp.net or 12560234567890@g.us).

  • body: The content of the message.

This basic script prints the response from the server, which includes any errors that occurred during the request. When you're ready to deploy in a production environment, you should implement robust error and exception handling mechanisms.

If you have any inquiries or would like to share your experiences with us, please feel free to reach out.

Get Started Free

Step-by-step: build a WhatsApp ChatBot using PYTHON: https://whapi.cloud/whatsapp-bot-python


Quickstart

Use the source code of ready-made WhatsApp bots as a platform for an easy start. Download, insert your token and launch!

Send Media Message

Additional endpoint for easy send media-file as message. Use request body as file and inpath parameters for send parameters. Media message can be one of the following types:

Send Image

Send Document

Send Audio

Send Voice

You can Send a ppt audio recording to the phone number or group, But WhatsApp is sensitive to this extension, you need to be in OGG format and the codecs should be the opus, For more information, you can check out this article.

Send Video

Send GIF

Send Contact

Send Contact List

Send Location

Send Sticker

Last updated