Skip to main content

This library helps you easily create a Python application with WhatsApp API.

Project description

whatsapp-api-client-python

whatsapp-api-client-python is a library for integration with WhatsApp messenger using the API service green-api.com. You should get a registration token and an account ID in your personal cabinet to use the library. There is a free developer account tariff.

API

The documentation for the REST API can be found at the link. The library is a wrapper for the REST API, so the documentation at the link above also applies.

Authorization

To send a message or perform other GREEN API methods, the WhatsApp account in the phone app must be authorized. To authorize the account, go to your cabinet and scan the QR code using the WhatsApp app.

Installation

python -m pip install whatsapp-api-client-python

Import

from whatsapp_api_client_python import API

Examples

How to initialize an object

greenAPI = API.GreenApi(
    "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
)

Sending a text message to a WhatsApp number

Link to example: sendTextMessage.py.

response = greenAPI.sending.sendMessage("11001234567@c.us", "Message text")

print(response.data)

Sending an image via URL

Link to example: sendPictureByLink.py.

response = greenAPI.sending.sendFileByUrl(
    "11001234567@c.us",
    "https://green-api.com/green-api-logo_2.png",
    "green-api-logo_2.png",
    "GREEN API logo"
)

print(response.data)

Sending an image by uploading from the disk

Link to example: sendPictureByUpload.py.

response = greenAPI.sending.sendFileByUpload(
    "11001234567@c.us",
    "data/green-api-logo_2.png",
    "green-api-logo_2.png",
    "GREEN API logo"
)

print(response.data)

Group creation and sending a message to the group

Attention. If one tries to create a group with a non-existent number, WhatsApp may block the sender's number. The number in the example is non-existent.

Link to example: createGroupAndSendMessage.py.

create_group_response = greenAPI.groups.createGroup(
    "Group Name", ["11001234567@c.us"]
)
if create_group_response.code == 200:
    send_message_response = greenAPI.sending.sendMessage(
        create_group_response.data["chatId"], "Message text"
    )

Receive incoming messages by HTTP API

Link to example: receiveNotification.py.

The general concept of receiving data in the GREEN API is described here. To start receiving notifications by the HTTP API you need to execute the library method:

greenAPI.webhooks.startReceivingNotifications(onEvent)

onEvent - your function which should contain parameters:

Parameter Description
typeWebhook received notification type (str)
body notification body (dict)

Notification body types and formats can be found here.

This method will be called when an incoming notification is received. Next, process notifications according to the business logic of your system.

Examples list

Description Module
Example of sending text sendTextMessage.py
Example of sending a picture by URL sendPictureByLink.py
Example of sending a picture by uploading from the disk sendPictureByUpload.py
Example of a group creation and sending a message to the group createGroupAndSendMessage.py
Example of incoming webhooks receiving receiveNotification.py

The full list of the library methods

API method Description Documentation link
account.getSettings The method is designed to get the current settings of the account GetSettings
account.setSettings The method is designed to set the account settings SetSettings
account.getStateInstance The method is designed to get the state of the account GetStateInstance
account.getStatusInstance The method is designed to get the socket connection state of the account instance with WhatsApp GetStatusInstance
account.reboot The method is designed to restart the account Reboot
account.logout The method is designed to unlogin the account Logout
account.qr The method is designed to get a QR code QR
account.setProfilePicture The method is designed to set the avatar of the account SetProfilePicture
device.getDeviceInfo The method is designed to get information about the device (phone) on which the WhatsApp Business application is running GetDeviceInfo
groups.createGroup The method is designed to create a group chat CreateGroup
groups.updateGroupName The method changes the name of the group chat UpdateGroupName
groups.getGroupData The method gets group chat data GetGroupData
groups.addGroupParticipant The method adds a participant to the group chat AddGroupParticipant
groups.removeGroupParticipant The method removes the participant from the group chat RemoveGroupParticipant
groups.setGroupAdmin The method designates a member of a group chat as an administrator SetGroupAdmin
groups.removeAdmin The method deprives the participant of group chat administration rights RemoveAdmin
groups.setGroupPicture The method sets the avatar of the group SetGroupPicture
groups.leaveGroup The method logs the user of the current account out of the group chat LeaveGroup
journals.getChatHistory The method returns the chat message history GetChatHistory
journals.getMessage The method returns a chat message GetMessage
journals.lastIncomingMessages The method returns the most recent incoming messages of the account LastIncomingMessages
journals.lastOutgoingMessages The method returns the last sent messages of the account LastOutgoingMessages
queues.showMessagesQueue The method is designed to get the list of messages that are in the queue to be sent ShowMessagesQueue
queues.clearMessagesQueue The method is designed to clear the queue of messages to be sent ClearMessagesQueue
marking.readChat The method is designed to mark chat messages as read ReadChat
receiving.receiveNotification The method is designed to receive a single incoming notification from the notification queue ReceiveNotification
receiving.deleteNotification The method is designed to remove an incoming notification from the notification queue DeleteNotification
receiving.downloadFile The method is for downloading received and sent files DownloadFile
sending.sendMessage The method is designed to send a text message to a personal or group chat SendMessage
sending.sendButtons The method is designed to send a message with buttons to a personal or group chat SendButtons
sending.sendTemplateButtons The method is designed to send a message with interactive buttons from the list of templates in a personal or group chat SendTemplateButtons
sending.sendListMessage The method is designed to send a message with a selection button from a list of values to a personal or group chat SendListMessage
sending.sendFileByUpload The method is designed to send a file loaded through a form (form-data) SendFileByUpload
sending.sendFileByUrl The method is designed to send a file downloaded via a link SendFileByUrl
sending.sendLocation The method is designed to send a geolocation message SendLocation
sending.sendContact The method is for sending a message with a contact SendContact
sending.sendLink The method is designed to send a message with a link that will add an image preview, title and description SendLink
sending.forwardMessages The method is designed for forwarding messages to a personal or group chat ForwardMessages
serviceMethods.checkWhatsapp The method checks if there is a WhatsApp account on the phone number CheckWhatsapp
serviceMethods.getAvatar The method returns the avatar of the correspondent or group chat GetAvatar
serviceMethods.getContacts The method is designed to get a list of contacts of the current account GetContacts
serviceMethods.getContactInfo The method is designed to obtain information about the contact GetContactInfo
serviceMethods.deleteMessage The method deletes the message from chat DeleteMessage
serviceMethods.archiveChat The method archives the chat ArchiveChat
serviceMethods.unarchiveChat The method unarchives the chat UnarchiveChat
serviceMethods.setDisappearingChat The method is designed to change the settings of disappearing messages in chats SetDisappearingChat
webhooks.startReceivingNotifications The method is designed to start receiving new notifications
webhooks.stopReceivingNotifications The method is designed to stop receiving new notifications

Service methods documentation

https://green-api.com/en/docs/api/.

External products

License

Licensed under Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) terms. Please see file LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

whatsapp-api-client-python-0.0.38.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file whatsapp-api-client-python-0.0.38.tar.gz.

File metadata

File hashes

Hashes for whatsapp-api-client-python-0.0.38.tar.gz
Algorithm Hash digest
SHA256 ebd01fc1b3d2211d94f0dbb77fc217e9d78513f8d09359b9d7e6ad5460e5177c
MD5 8b29e9234d921e98ae9efaf5c99f9c6e
BLAKE2b-256 e9e847c1d3547596db99ec74ec091a2030a1ca55c0a2a6d077ce8638739f0f05

See more details on using hashes here.

File details

Details for the file whatsapp_api_client_python-0.0.38-py3-none-any.whl.

File metadata

File hashes

Hashes for whatsapp_api_client_python-0.0.38-py3-none-any.whl
Algorithm Hash digest
SHA256 2b5fe98caa0aab8b80714a06e6853a6ead32233f1bcfdb31fe87d4cb3e1dc7dd
MD5 1537d4a5960223ab6c2eb5d1fcae4988
BLAKE2b-256 b48eb023098de8d7530d67a7ef3d432eeb47c41b5b456d0afbd2eeb1f2399d85

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page