This library helps you easily create a Python application with WhatsApp API.
Project description
whatsapp-api-client-python
Python library for intagration with WhatsAPP messanger via API of green-api.com service. To use the library you have to get a registration token and an account id in the personal area. There is a free developer account tariff plan.
API
You can find REST API documentation by url. The library is a wrapper for REST API, so the documentation at the above url applies to the library as well.
Installation
pip install whatsapp-api-client-python
Import
from whatsapp_api_client_python import API
Authorization
To send a message or to exacute some other Green-API method, you have to have the WhatsApp account in the phone application to be authorized. To authorize your account please go to the personal area and scan a QR-code using the WhatsApp application.
Examples
How to initialize an object
greenAPI = API.GreenApi(ID_INSTANCE, API_TOKEN_INSTANCE)
Sending a text message to a WhatsApp number
result = greenAPI.sending.sendMessage('79001234567@g.us', 'Message text')
Example url: sendTextMessage.py
Please note that keys can be obtained from environment variables:
from os import environ
ID_INSTANCE = environ['ID_INSTANCE']
API_TOKEN_INSTANCE = environ['API_TOKEN_INSTANCE']
Sending an image via URL
result = greenAPI.sending.sendFileByUrl('120363025955348359@g.us',
'https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
'googlelogo_color_272x92dp.png', 'Google logo')
Example url: sendPictureByLink.py
Sending an image by uploading from the disk
result = greenAPI.sending.sendFileByUpload('120363025955348359@g.us',
'C:\Games\PicFromDisk.png',
'PicFromDisk.png', 'Picture from disk')
Example url: sendPictureByUpload.py
Group creation and sending a message to the group
chatIds = [
"79001234567@c.us"
]
resultCreate = greenAPI.groups.createGroup('GroupName',
chatIds)
if resultCreate.code == 200:
resultSend = greenAPI.sending.sendMessage(resultCreate.data['chatId'],
'Message text')
IMPORTANT: 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.
Example url: createGroupAndSendMessage.py
Receive incoming messages by HTTP API
The general concept of receiving data in the Green API is described here To start receiving messages by the HTTP API you need to execute the library method:
greenAPI.webhooks.startReceivingNotifications(onEvent)
onEvent - your method which should contain parameters:
Parameter | Description |
---|---|
typewebhook | received message type (string) |
body | message body (json) |
Message body types and formats here
This method will be called when an incoming message is received. Next, process messages 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
- requests - for http requests
License
This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License.
Please see file LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file whatsapp-api-client-python-0.0.36.tar.gz
.
File metadata
- Download URL: whatsapp-api-client-python-0.0.36.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf8a8af58e6ac25c2eb7ed3c9d054e057059bdd242893fa1d57e8b935bb86457 |
|
MD5 | ad53ba18abb3b0e6a0e591c97c5a7727 |
|
BLAKE2b-256 | 17133e4a23b3f767c9979cb6a04f8aff9efefcca8c0b7300c69d3eb18d022941 |
File details
Details for the file whatsapp_api_client_python-0.0.36-py3-none-any.whl
.
File metadata
- Download URL: whatsapp_api_client_python-0.0.36-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ff61aa8b5f7daf32524ec45df2fe0c31bd32ed26a6aed1cdf8f3f41e80fa536 |
|
MD5 | 24ebfac1e94a1e989657213515a9cb0e |
|
BLAKE2b-256 | f2c690e9a3e014eb3ba42ac0b05411f20bd5317110e4839b992cf21f2003bc39 |