Socket
Socket
Sign inDemoInstall

whatsapp-api-client-python

Package Overview
Dependencies
1
Maintainers
2
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    whatsapp-api-client-python

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


Maintainers
2

Readme

whatsapp-api-client-python

Support Support Support

Guides & News

Guides News News

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://download.samplelib.com/png/sample-clouds2-400x300.png",
    "sample-clouds2-400x300.png",
    "Sample PNG"
)

print(response.data)

Sending an image by uploading from the disk

Link to example: sendPictureByUpload.py.

response = greenAPI.sending.sendFileByUpload(
    "11001234567@c.us",
    "data/rates.png",
    "rates.png",
    "Available rates"
)

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:

ParameterDescription
typeWebhookreceived notification type (str)
bodynotification 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.

Sending a polling message

Link to example: sendPoll.py.

response = greenAPI.sending.sendPoll(
    "11001234567@c.us",
    "Please choose a color:",
    [
        {"optionName": "Red"},
        {"optionName": "Green"},
        {"optionName": "Blue"}
    ]
)

print(response.data)

Examples list

DescriptionModule
Example of sending textsendTextMessage.py
Example of sending a picture by URLsendPictureByLink.py
Example of sending a picture by uploading from the disksendPictureByUpload.py
Example of a group creation and sending a message to the groupcreateGroupAndSendMessage.py
Example of incoming webhooks receivingreceiveNotification.py
Example of sending a polling messagesendPoll.py

The full list of the library methods

API methodDescriptionDocumentation link
account.getSettingsThe method is designed to get the current settings of the accountGetSettings
account.getWaSettingsThe method is designed to get information about the WhatsApp accountGetWaSettings
account.setSettingsThe method is designed to set the account settingsSetSettings
account.getStateInstanceThe method is designed to get the state of the accountGetStateInstance
account.getStatusInstanceThe method is designed to get the socket connection state of the account instance with WhatsAppGetStatusInstance
account.rebootThe method is designed to restart the accountReboot
account.logoutThe method is designed to unlogin the accountLogout
account.qrThe method is designed to get a QR codeQR
account.setProfilePictureThe method is designed to set the avatar of the accountSetProfilePicture
account.getAuthorizationCodeThe method is designed to authorize an instance by phone numberGetAuthorizationCode
device.getDeviceInfoThe method is designed to get information about the device (phone) on which the WhatsApp Business application is runningGetDeviceInfo
groups.createGroupThe method is designed to create a group chatCreateGroup
groups.updateGroupNameThe method changes the name of the group chatUpdateGroupName
groups.getGroupDataThe method gets group chat dataGetGroupData
groups.addGroupParticipantThe method adds a participant to the group chatAddGroupParticipant
groups.removeGroupParticipantThe method removes the participant from the group chatRemoveGroupParticipant
groups.setGroupAdminThe method designates a member of a group chat as an administratorSetGroupAdmin
groups.removeAdminThe method deprives the participant of group chat administration rightsRemoveAdmin
groups.setGroupPictureThe method sets the avatar of the groupSetGroupPicture
groups.leaveGroupThe method logs the user of the current account out of the group chatLeaveGroup
journals.getChatHistoryThe method returns the chat message historyGetChatHistory
journals.getMessageThe method returns a chat messageGetMessage
journals.lastIncomingMessagesThe method returns the most recent incoming messages of the accountLastIncomingMessages
journals.lastOutgoingMessagesThe method returns the last sent messages of the accountLastOutgoingMessages
queues.showMessagesQueueThe method is designed to get the list of messages that are in the queue to be sentShowMessagesQueue
queues.clearMessagesQueueThe method is designed to clear the queue of messages to be sentClearMessagesQueue
marking.readChatThe method is designed to mark chat messages as readReadChat
receiving.receiveNotificationThe method is designed to receive a single incoming notification from the notification queueReceiveNotification
receiving.deleteNotificationThe method is designed to remove an incoming notification from the notification queueDeleteNotification
receiving.downloadFileThe method is for downloading received and sent filesDownloadFile
sending.sendMessageThe method is designed to send a text message to a personal or group chatSendMessage
sending.sendButtonsThe method is designed to send a message with buttons to a personal or group chatSendButtons
sending.sendTemplateButtonsThe method is designed to send a message with interactive buttons from the list of templates in a personal or group chatSendTemplateButtons
sending.sendListMessageThe method is designed to send a message with a selection button from a list of values to a personal or group chatSendListMessage
sending.sendFileByUploadThe method is designed to send a file loaded through a form (form-data)SendFileByUpload
sending.sendFileByUrlThe method is designed to send a file downloaded via a linkSendFileByUrl
sending.uploadFileThe method is designed to upload a file to the cloud storage, which can be sent using the sendFileByUrl methodUploadFile
sending.sendLocationThe method is designed to send a geolocation messageSendLocation
sending.sendContactThe method is for sending a message with a contactSendContact
sending.sendLinkThe method is designed to send a message with a link that will add an image preview, title and descriptionSendLink
sending.forwardMessagesThe method is designed for forwarding messages to a personal or group chatForwardMessages
sending.sendPollThe method is designed for sending messages with a poll to a private or group chatSendPoll
serviceMethods.checkWhatsappThe method checks if there is a WhatsApp account on the phone numberCheckWhatsapp
serviceMethods.getAvatarThe method returns the avatar of the correspondent or group chatGetAvatar
serviceMethods.getContactsThe method is designed to get a list of contacts of the current accountGetContacts
serviceMethods.getContactInfoThe method is designed to obtain information about the contactGetContactInfo
serviceMethods.deleteMessageThe method deletes the message from chatDeleteMessage
serviceMethods.archiveChatThe method archives the chatArchiveChat
serviceMethods.unarchiveChatThe method unarchives the chatUnarchiveChat
serviceMethods.setDisappearingChatThe method is designed to change the settings of disappearing messages in chatsSetDisappearingChat
webhooks.startReceivingNotificationsThe method is designed to start receiving new notifications
webhooks.stopReceivingNotificationsThe 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.

FAQs


Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc