Skip to content

Installation & Setup

Filippo Romani edited this page Oct 13, 2024 · 8 revisions

Installing from source - always up to date version

Please note that this version is not stable and may contain bugs. If you want to use a stable version, please install from pip.

git clone https://github.com/filipporomani/whatsapp
cd whatsapp
pip 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
    • 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 using the package.

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)

The update_check parameter is optional and it's used to disable the update checker (default - recommended: True)

The debug arguments enables debug logging. By default, this is set to False.

The version argument specifies a custom version number of the Graph API. By default, the last available version is used.

>>> from whatsapp import WhatsApp, Message
>>> messenger = WhatsApp('TOKEN',  phone_number_id={"key": 'xxxxxxxxx', "key1": 'yyyyyyyyy'}, logger=True, update_check=True, debug=False, version="latest")

Caution

Starting from versions > 3.2.5 you can use different phone numbers to send messages. The phone_number_id parameter is a dictionary with the phone number as the key and the phone number id as the value. When sending a message, you'll be able to choose the phone number to send the message from. Please refer to the Sending messages section for more information. Starting from versions > 3.4.0 you won't be able to init the WhatsApp object using a string as the phone_number_id parameter. You'll need to use a dictionary instead.

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. To disable debug messages, set the debug parameter to False when initializing the WhatsApp object.