Skip to content

green-api/whatsapp-api-client-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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{
    "11001234567@c.us",
    "11002345678@c.us",
})

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

How to send a file by URL

Link to example: sendFileByURL/main.go.

response, _ := GreenAPI.Methods().Sending().SendFileByUrl(map[string]interface{}{
    "chatId":   "11001234567@c.us",
    "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":  "11001234567@c.us",
    "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":  "11001234567@c.us",
	"message": "Please choose a color:",
	"options": []map[string]interface{}{
		{
			"optionName": "Red",
		},
		{
			"optionName": "Green",
		},
		{
			"optionName": "Blue",
		},
	},
})

List of examples

Description Link to example
How to create a group createGroup/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 send a message sendMessage/main.go
How to receive incoming notifications webhook/main.go
How to send a message with a poll sendPoll/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().GetStatusInstance The method is designed to get the socket connection state of the account instance with WhatsApp GetStatusInstance
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
Device().GetDeviceInfo The method is designed to get information about the device (phone) on which the WhatsApp Business application is running GetDeviceInfo
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().SendButtons The method is designed to send a message with buttons to a personal or group chat SendButtons
Sending().SendTemplateButtons The method is designed to send a message with interactive buttons from the list of templates in a personal or group chat SendTemplateButtons
Sending().SendListMessage The method is designed to send a message with a selection button from a list of values to a personal or group chat SendListMessage
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().SendLink The method is designed to send a message with a link that will add an image preview, title and description SendLink
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
Webhook().Start The method is designed to start receiving new notifications
Webhook().Stop The 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.