🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

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

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

v1.0.3
Source
Go
Version published
Created
Source

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 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(
		"11001234567@c.us",
		"Hello",
	)

How to create a group:

Link to example: createGroup/main.go

response, _ := GreenAPI.Groups().CreateGroup(
		"Group Title",
		[]string{
			"11001211111@c.us",
			"11001222222@c.us",
			"11001233333@c.us",
		},
	)

How to send file by upload:

Link to example: sendFileByUpload/main.go

response, _ := GreenAPI.Sending().SendFileByUpload(
		"11001234567@c.us",
		"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(
		"11001234567@c.us",
		"urlFile",
		"fileName",
		greenapi.OptionalCaptionSendUrl("Caption"),
	)

How to send a message with a poll:

Link to example: sendPoll/main.go

response, _ := GreenAPI.Sending().SendPoll(
		"11001234567@c.us", 
		"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{"1234567890@c.us", "1234567890@c.us"}),
	)

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: "mail@email.com", // 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.OptionalName("Created by GO SDK"),
		greenapi.OptionalWebhookUrl("https://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),
		greenapi.OptionalEditedMessageWebhook(true),
		greenapi.OptionalDeletedMessageWebhook(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),
		// greenapi.OptionalEditedMessageWebhook(true),
		// greenapi.OptionalDeletedMessageWebhook(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(
		"11001234567@c.us",
		"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().EditMessageThe method edits the message in a chatDeleteMessage
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

FAQs

Package last updated on 08 Feb 2025

Did you know?

Socket

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