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

# README

whatsapp-api-client-golang

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

Do not forget to create a module:

go mod init example

Installation:

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

Import

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

Examples

How to initialize an object

GreenAPI := api.GreenAPI{
    IDInstance:       "1101000001",
    APITokenInstance: "d75b3a66374942c5b3c019c698abc2067e151558acbd412345",
}

Note that keys can be obtained from environment variables:

IDInstance := os.Getenv("ID_INSTANCE")
APITokenInstance := os.Getenv("API_TOKEN_INSTANCE")

How to create a group

Link to example: createGroup/main.go.

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

How to send a file by uploading from the disk

To send a file, you need to give the path to the file.

Link to example: sendFileByUpload/main.go.

response, _ := GreenAPI.Methods().Sending().SendFileByUpload("example.png", map[string]interface{}{
    "chatId": "[email protected]",
})

How to send a file by URL

Link to example: sendFileByURL/main.go.

response, _ := GreenAPI.Methods().Sending().SendFileByUrl(map[string]interface{}{
    "chatId":   "[email protected]",
    "urlFile":  "https://go.dev/blog/go-brand/Go-Logo/SVG/Go-Logo_Blue.svg",
    "fileName": "Go-Logo_Blue.svg",
})

How to send a message

If an API method has optional parameters, you have to pass JSON to the library method (map[string]interface{}).

Link to example: sendMessage/main.go.

response, _ := GreenAPI.Methods().Sending().SendMessage(map[string]interface{}{
    "chatId":  "[email protected]",
    "message": "Any message",
})

How to receive incoming notifications

To receive incoming webhooks, you must send a handler function to Webhook().Start. The handler function should have one parameter (body map[string]interface{}). When you receive a new notification, your handler function will be executed. To stop receiving incoming webhooks, you need to call Webhook().Stop.

Link to example: webhook/main.go.

GreenAPIWebhook := GreenAPI.Webhook()

GreenAPIWebhook.Start(func(body map[string]interface{}) {
    fmt.Println(body)
})

How to send a message with a poll

If an API method has optional parameters, you have to pass JSON to the library method (map[string]interface{}).

Link to example: sendPoll/main.go.

response, err := GreenAPI.Methods().Sending().SendPoll(map[string]interface{}{
	"chatId":  "[email protected]",
	"message": "Please choose a color:",
	"options": []map[string]interface{}{
		{
			"optionName": "Red",
		},
		{
			"optionName": "Green",
		},
		{
			"optionName": "Blue",
		},
	},
})

List of examples

DescriptionLink to example
How to create a groupcreateGroup/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 send a messagesendMessage/main.go
How to receive incoming notificationswebhook/main.go
How to send a message with a pollsendPoll/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().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
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().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 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().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
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
Webhook().StartThe method is designed to start receiving new notifications
Webhook().StopThe method is designed to stop receiving new notifications

Service methods documentation

Service methods documentation.

License

Licensed under Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) terms. Please see file LICENSE.

# Packages

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