whatsapp-api-client-golang-v2

This library helps you easily create a Golang application with Whatsapp support

OTHER License

Stars
5
Committers
6

whatsapp-api-client-golang-v2

whatsapp-api-client-golang-v2 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.

You can find the v1 version here - https://github.com/green-api/whatsapp-api-client-golang

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.

Support links

Guides & News

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

Make sure that you have Go installed with a version of 1.20 or newer

go version

Create a module for your project if you didn't:

go mod init ModuleName

Install the library:

go get github.com/green-api/whatsapp-api-client-golang-v2

Import:

import (
	greenapi "github.com/green-api/whatsapp-api-client-golang-v2"
)

Usage and examples

How to initialize an object:

GreenAPI := greenapi.GreenAPI{
		APIURL:           "https://api.green-api.com",
		MediaURL:         "https://media.green-api.com",
		IDInstance:       "1101000001",
		APITokenInstance: "d75b3a66374942c5b3c019c698abc2067e151558acbd412345",
	}

All methods of this library return two objects: *APIResponse and error.

You can see the APIResponse format in the types.go

How to send a message:

Link to example: sendMessage/main.go

response, _ := GreenAPI.Sending().SendMessage(
		"[email protected]",
		"Hello",
	)

How to create a group:

Link to example: createGroup/main.go

response, _ := GreenAPI.Groups().CreateGroup(
		"Group Title",
		[]string{
			"[email protected]",
			"[email protected]",
			"[email protected]",
		},
	)

How to send file by upload:

Link to example: sendFileByUpload/main.go

response, _ := GreenAPI.Sending().SendFileByUpload(
		"[email protected]",
		"C:/Users/user/Desktop/Pictures/image.png",
		"image.png",
	)

How to send a file by URL:

Link to example: sendFileByUrl/main.go

response, _ := GreenAPI.Sending().SendFileByUrl(
		"[email protected]",
		"urlFile",
		"fileName",
		greenapi.OptionalCaptionSendUrl("Caption"),
	)

How to send a message with a poll:

Link to example: sendPoll/main.go

response, _ := GreenAPI.Sending().SendPoll(
		"[email protected]", 
		"Choose a color:", 
		[]string{"Red", "Green", "Blue"}, 
	)

How to send a text status:

Link to example: sendTextStatus/main.go

response, _ := GreenAPI.Statuses().SendTextStatus(
		"Text of the status", 
		greenapi.OptionalFont("SERIF"),
		greenapi.OptionalBackgroundColorText("#87CEEB"),
		//greenapi.OptionalParticipantsTextStatus([]string{"[email protected]", "[email protected]"}),
	)

How to receive an incoming notification:

Link to example: receiveNotification/main.go

response, _ := GreenAPI.Receiving().ReceiveNotification(
		greenapi.OptionalReceiveTimeout(5),
	)

Partner methods

To use partner methods you have to initialize another object:

Partner := greenapi.GreenAPIPartner{
		PartnerToken: "gac.1234567891234567891234567891213456789",
		Email: "[email protected]", // email is optional
	}

Now you can use Partner methods as usual methods, but through the "Partner" object:

How to get instances:

Link to the example: partnerMethods/getInstances/main.go

response, _ := Partner.Partner().GetInstances()

How to create an instance:

Link to the example: partnerMethods/createInstance/main.go

response, _ := Partner.Partner().CreateInstance(
		greenapi.OptionalWebhookUrl("webhook_url"),
		greenapi.OptionalWebhookUrlToken("auth_token"),
		greenapi.OptionalDelaySendMessages(5000),
		greenapi.OptionalMarkIncomingMessagesRead(true),
		greenapi.OptionalMarkIncomingMessagesReadOnReply(true),
		greenapi.OptionalOutgoingWebhook(true),
		greenapi.OptionalOutgoingMessageWebhook(true),
		greenapi.OptionalOutgoingAPIMessageWebhook(true),
		greenapi.OptionalStateWebhook(true),
		greenapi.OptionalIncomingWebhook(true),
		greenapi.OptionalDeviceWebhook(true),
		greenapi.OptionalKeepOnlineStatus(true),
		greenapi.OptionalPollMessageWebhook(true),
		greenapi.OptionalIncomingBlockWebhook(true),
		greenapi.OptionalIncomingCallWebhook(true),
	)

How to delete an instance:

Link to the example: partnerMethods/deleteInstanceAccount/main.go

response, _ := Partner.Partner().DeleteInstanceAccount(1101000000)

Optional parameters

Note that functions might have optional arguments, which you can pass or ignore. Optional parameters are passed as functions into the method's arguments and have similar naming format:

greenapi.Optional + name of parameter

For example, in the SetSettings method all the arguments are optional. Here is an example of how it works:

response, _ := GreenAPI.Account().SetSettings(
        greenapi.OptionalDelaySendMessages(5000),
		greenapi.OptionalOutgoingWebhook(true),
		greenapi.OptionalIncomingWebhook(true),
		// greenapi.OptionalWebhookUrl("webhook_url"),
		// greenapi.OptionalWebhookUrlToken("auth_token"),
		// greenapi.OptionalMarkIncomingMessagesRead(true),
		// greenapi.OptionalMarkIncomingMessagesReadOnReply(true),
		// greenapi.OptionalOutgoingMessageWebhook(true),
		// greenapi.OptionalOutgoingAPIMessageWebhook(true),
		// greenapi.OptionalStateWebhook(true),
		// greenapi.OptionalDeviceWebhook(true),
		// greenapi.OptionalKeepOnlineStatus(true),
		// greenapi.OptionalPollMessageWebhook(true),
		// greenapi.OptionalIncomingBlockWebhook(true),
		// greenapi.OptionalIncomingCallWebhook(true),
	)

In this example, only DelaySendMessages, OutgoingWebhook and IncomingWebhook settings will be changed, other settings are commented so they will not be passed. However, you can uncomment any setting that you prefer. The settings that were not used will not be affected

One more example of using optional parameters, this time let's use sendMessage method:

response, _ := GreenAPI.Sending().SendMessage(
		"[email protected]",
		"Hello",
		greenapi.OptionalLinkPreview(false), // turns off link preview if there is any
		greenapi.OptionalQuotedMessageId("BAE59673E71FC5DB"), // quotes specified message
	)

List of examples

Description Link to example
How to send a message sendMessage/main.go
How to send a file by uploading from the disk sendFileByUpload/main.go
How to send a file by URL sendFileByUrl/main.go
How to upload a file to an external drive uploadFile/main.go
How to send a poll sendPoll/main.go
How to check if there is a WhatsApp account on the phone number checkWhatsapp/main.go
How to set instance settings setSettings/main.go
How to create a group createGroup/main.go
How to send a text status sendTextStatus/main.go
How to receive an incoming notification receiveNotification/main.go
How to get all instances of the account partnerMethods/getInstances/main.go
How to create an instance partnerMethods/createInstance/main.go
How to delete an instance partnerMethods/deleteInstanceAccount/main.go

List of all library methods

API method Description Documentation link
Account().GetSettings The method is designed to get the current settings of the account GetSettings
Account().GetWaSettings The method is designed to get information about the WhatsApp 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().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
Account().GetAuthorizationCode The method is designed to authorize an instance by phone number GetAuthorizationCode
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
ReadMark().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().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().UploadFile The method allows you to upload a file from the local file system, which can later be sent using the SendFileByUrl method UploadFile
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().ForwardMessages The method is designed for forwarding messages to a personal or group chat ForwardMessages
Sending().SendPoll The method is designed for sending messages with a poll to a private or group chat SendPoll
Service().CheckWhatsapp The method checks if there is a WhatsApp account on the phone number CheckWhatsapp
Service().GetAvatar The method returns the avatar of the correspondent or group chat GetAvatar
Service().GetContacts The method is designed to get a list of contacts of the current account GetContacts
Service().GetContactInfo The method is designed to obtain information about the contact GetContactInfo
Service().DeleteMessage The method deletes the message from chat DeleteMessage
Service().ArchiveChat The method archives the chat ArchiveChat
Service().UnarchiveChat The method unarchives the chat UnarchiveChat
Service().SetDisappearingChat The method is designed to change the settings of disappearing messages in chats SetDisappearingChat
Partner().GetInstances The method is for getting all the account instances created by the partner. GetInstances
Partner().CreateInstance The method is for creating an instance. CreateInstance
Partner().DeleteInstanceAccount The method is for deleting an instance. DeleteInstanceAccount
Statuses().SendTextStatus The method is aimed for sending a text status SendTextStatus
Statuses().SendVoiceStatus The method is aimed for sending a voice status SendVoiceStatus
Statuses().SendMediaStatus The method is aimed for sending a voice status SendMediaStatus
Statuses().GetOutgoingStatuses The method returns the outgoing statuses of the account GetOutgoingStatuses
Statuses().GetIncomingStatuses The method returns the incoming status messages of the account GetIncomingStatuses
Statuses().GetStatusStatistic The method returns an array of recipients marked for a given status. GetStatusStatistic
Statuses().DeleteStatus The method is aimed for deleting status. DeleteStatus
Package Rankings
Top 6.51% on Proxy.golang.org
Badges
Extracted from project README
Support Support Support Guides News News
Related Projects