Categorygithub.com/green-api/whatsapp-api-client-golang-v2

# README

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

Support Support Support

Guides & News

Guides News 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

DescriptionLink to example
How to send a messagesendMessage/main.go
How to send a file by uploading from the disksendFileByUpload/main.go
How to send a file by URLsendFileByUrl/main.go
How to upload a file to an external driveuploadFile/main.go
How to send a pollsendPoll/main.go
How to check if there is a WhatsApp account on the phone numbercheckWhatsapp/main.go
How to set instance settingssetSettings/main.go
How to create a groupcreateGroup/main.go
How to send a text statussendTextStatus/main.go
How to receive an incoming notificationreceiveNotification/main.go
How to get all instances of the accountpartnerMethods/getInstances/main.go
How to create an instancepartnerMethods/createInstance/main.go
How to delete an instancepartnerMethods/deleteInstanceAccount/main.go

List of all 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 accountGetSettings
Account().SetSettingsThe method is designed to set the account settingsSetSettings
Account().GetStateInstanceThe method is designed to get the state of the accountGetStateInstance
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
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
ReadMark().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().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 allows you to upload a file from the local file system, which can later 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().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
Service().CheckWhatsappThe method checks if there is a WhatsApp account on the phone numberCheckWhatsapp
Service().GetAvatarThe method returns the avatar of the correspondent or group chatGetAvatar
Service().GetContactsThe method is designed to get a list of contacts of the current accountGetContacts
Service().GetContactInfoThe method is designed to obtain information about the contactGetContactInfo
Service().DeleteMessageThe method deletes the message from chatDeleteMessage
Service().ArchiveChatThe method archives the chatArchiveChat
Service().UnarchiveChatThe method unarchives the chatUnarchiveChat
Service().SetDisappearingChatThe method is designed to change the settings of disappearing messages in chatsSetDisappearingChat
Partner().GetInstancesThe method is for getting all the account instances created by the partner.GetInstances
Partner().CreateInstanceThe method is for creating an instance.CreateInstance
Partner().DeleteInstanceAccountThe method is for deleting an instance.DeleteInstanceAccount
Statuses().SendTextStatusThe method is aimed for sending a text statusSendTextStatus
Statuses().SendVoiceStatusThe method is aimed for sending a voice statusSendVoiceStatus
Statuses().SendMediaStatusThe method is aimed for sending a voice statusSendMediaStatus
Statuses().GetOutgoingStatusesThe method returns the outgoing statuses of the accountGetOutgoingStatuses
Statuses().GetIncomingStatusesThe method returns the incoming status messages of the accountGetIncomingStatuses
Statuses().GetStatusStatisticThe method returns an array of recipients marked for a given status.GetStatusStatistic
Statuses().DeleteStatusThe method is aimed for deleting status.DeleteStatus

# Packages

No description provided by the author

# Functions

No description provided by the author
Location address.
Status background.
Status background.
Media status caption.
File caption.
File caption.
The number of messages to get.
Message sending delay.
Get notifications about the device (phone) and battery level.
Text font.
ID of the incoming message to be marked as read.
Get notifications about adding a chat to the list of blocked contacts.
Get notifications about incoming call statuses.
Get notifications about incoming messages and files.
Sets the 'Online' status for your Whatsapp account.
The parameter includes displaying a preview and a description of the link.
Mark incoming messages as read or not.
Mark incoming messages as read when posting a message to the chat via API.
Time in minutes for which the messages should be displayed (default is 1440 minutes).
Time in minutes for which the status messages should be displayed (1440 minutes by default).
Allow multiple answers.
Location name.
Get notifications about messages sent from API.
Get notifications about messages sent from the phone.
Get notifications about outgoing messages sending/delivering/reading statuses.
An array of strings with contact IDs for whom the status will be available.
An array of strings with contact IDs for whom the status will be available.
An array of strings with contact IDs for whom the status will be available.
Get notifications about the creation of a poll and voting in the poll.
If specified, the message will be sent quoting the specified chat message.
Quoted message ID.
If specified, the message will be sent quoting the specified chat message.
If specified, the message will be sent quoting the specified chat message.
If specified, the message will be sent quoting the specified chat message.
If specified, the message will be sent quoting the specified chat message.
Notification waiting timeout, takes a value from 5 to 60 seconds (5 seconds by default).
Get notifications about the instance authorization state change.
URL for sending notifications.
Token to access your notification server.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author