Skip to content
Filippo Romani edited this page May 22, 2023 · 2 revisions

Getting started

To get started with whatsapp-python, you have to firstly install the libary either directly or using pip.

Installing from source - always up to date

$ git clone https://github.com/filipporomani/whatsapp
$ cd whatsapp
$ python setup.py install

Installing from pip - stable version

# For Windows 

pip install --upgrade whatsapp-python

#For Linux | MAC 

pip3 install --upgrade whatsapp-python

Setting up

To get started using this package, you will need TOKEN and TEST WHATSAPP NUMBER (the library works either with a production phone number, if you have one) which you can get from the Facebook Developer Portal

Here are steps to follow for you to get started:

  1. Go to your apps
  2. create an app
  3. Select Business >> Business
  4. It will prompt you to enter basic app informations
  5. It will ask you to add products to your app a. Add WhatsApp Messenger
  6. Right there you will see a your TOKEN and TEST WHATSAPP NUMBER and its phone_number_id
  7. Lastly verify the number you will be using for testing on the To field.

Once you've followed the above procedures you're ready to start hacking with the Wrapper.

Authentication

To authenticate your application, you need to specify the TOKEN and the phone_number_id of your application. The logger parameter is optional and it's used to disable logging (default: True)

>>> from whatsapp import WhatsApp
>>> messenger = WhatsApp('TOKEN',  phone_number_id='xxxxxxxxx', logger=True)

Once you have authenticated your app you can start using the above mentioned feature as shown above;

It is only possible to send messages other than templates only after the target phone responds to an initial template message or sends a message first. This resets every 24 hours; after that, you need to send a template again or the message won't be delivered. Reference: https://developers.facebook.com/community/threads/425605939396247/

Logging

You can configure your own log level. This is an example to set the log level to info. By default only Error messages are logged.

import logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

To disable logging, set the logger parameter to False when initializing the WhatsApp object.

Sending Messanges

Use this method to send text message to a WhatsApp number.

>>> messenger.send_message('Your message ', 'Mobile eg: 255757xxxxx')

Marking messages as read

Use this method to mark a previously sent text message as read.

>>> messenger.mark_as_read('Message ID')

Sending Images

When sending media(image, video, audio, gif and document ), you can either specify a link containing the media or specify object id, you can do this using the same method.

By default all media methods assume you're sending link containing media but you can change this by specifying the link=False.

Here an example;

>>> messenger.send_image(
        image="https://i.imgur.com/Fh7XVYY.jpeg",
        recipient_id="255757xxxxxx",
    )

Note: You can also send media from your local machine but you have to upload it first to Whatsapp Cloud API, you can do this using the upload_media method. and then use the returned object id to send the media.

Here an example;

>>> media_id = messenger.upload_media(
        media='path/to/media',
    )['id']
>>> messenger.send_image(
        image=media_id,
        recipient_id="255757xxxxxx",
        link=False
    )

Note: Don't forget to set the link to False, and also you can use the same technique for sending video, audio, gif and document from your local machine.

Sending Video

Here an example;

>>> messenger.send_video(
        video="https://www.youtube.com/watch?v=K4TOrB7at0Y",
        recipient_id="255757xxxxxx",
    )

Sending Audio

Here an example;

>>> messenger.send_audio(
        audio="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
        recipient_id="255757xxxxxx",
    )

Sending Document

Here an example;

>>> messenger.send_document(
        document="http://www.africau.edu/images/default/sample.pdf",
        recipient_id="255757xxxxxx",
    )

Sending Location

Here an example;

>>> messenger.send_location(
        lat=1.29,
        long=103.85,
        name="Singapore",
        address="Singapore",
        recipient_id="255757xxxxxx",
    )

Sending Interactive buttons

Here an example;

Note: row button title may not exceed 20 characters otherwise your message will not be sent to the target phone.

>>> messenger.send_button(
        recipient_id="255757xxxxxx",
        button={
            "header": "Header Testing",
            "body": "Body Testing",
            "footer": "Footer Testing",
            "action": {
                "button": "Button Testing",
                "sections": [
                    {
                        "title": "iBank",
                        "rows": [
                            {"id": "row 1", "title": "Send Money", "description": ""},
                            {
                                "id": "row 2",
                                "title": "Withdraw money",
                                "description": "",
                            },
                        ],
                    }
                ],
            },
        },
    )

Sending Interactive reply buttons

Here an example;

Send reply button only displays three reply buttons, if it exceeds three reply buttons, it will raise an error and your message will not be sent.

>>> messenger.send_reply_button(
        recipient_id="255757xxxxxx",
        button={
            "type": "button",
            "body": {
                "text": "This is a test button"
            },
            "action": {
                "buttons": [
                    {
                        "type": "reply",
                        "reply": {
                            "id": "b1",
                            "title": "This is button 1"
                        }
                    },
                    {
                        "type": "reply",
                        "reply": {
                            "id": "b2",
                            "title": "this is button 2"
                        }
                    }
                ]
            }
      },
    )

Sending a Template Messages

Here how to send a pre-approved template message, Template messages can either be;

  1. Text template
  2. Media based template
  3. Interactive template

You can customize the template message by passing a dictionary of components.

IMPORTANT: components are also known as variable parameters (like {{0}} or {{1}}) which are used to include variables into a message. You can find the available components in the documentation. https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates

>>> messenger.send_template("hello_world", "255757xxxxxx", components=[], lang="en_US")

lang is optional but required when sending templates in other languages.

Webhook

Webhook are useful incase you're wondering how to respond to incoming message sent by users. An example of webhook is shown in the hook.py file.

Issues

If you are facing any issues or have any questions regarding this wiki page, please open an issue on the GitHub repository

References

  1. WhatsApp Cloud API official documentation