Skip to content

green-api/whatsapp-api-client-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 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{
    "[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]any{
    "chatId": "[email protected]",
})

How to send a file by URL

Link to example: sendFileByURL/main.go.

response, _ := GreenAPI.Methods().Sending().SendFileByUrl(map[string]any{
    "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]any).

Link to example: sendMessage/main.go.

response, _ := GreenAPI.Methods().Sending().SendMessage(map[string]any{
    "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]any). 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]any) {
    log.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]any).

Link to example: sendPoll/main.go.

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

How to send a message with interactive buttons

Link to example: sendInteractiveButtons/main.go.

	buttons := []map[string]any{
		{
			"type":       "copy",
			"buttonId":   "1",
			"buttonText": "Copy me",
			"copyCode":   "3333",
		},
		{
			"type":        "call",
			"buttonId":    "2",
			"buttonText":  "Call me",
			"phoneNumber": "79123456789",
		},
		{
			"type":       "url",
			"buttonId":   "3",
			"buttonText": "Green-api",
			"url":        "https://green-api.com",
		},
	}

	parameters := map[string]any{
		"chatId":  "[email protected]",
		"body":    "Main message text",
		"header":  "Message header",
		"footer":  "Message footer",
		"buttons": buttons,
	}
	response, err := GreenAPI.Methods().Sending().SendInteractiveButtons(parameters)
	if err != nil {
		log.Fatal(err)
	}

How to send a text status

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

Link to example: sendStatus/main.go.

response, _ := GreenAPI.Methods().Status().SendTextStatus(map[string]any{
		"message":         "I used Green API GO SDK to send this status!",
		"backgroundColor": "#87CEEB",
		"font":            "SERIF",
	})

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
How to send a message with interactive buttons sendInteractiveButtons/main.go
How to send a message with interactive reply buttons sendInteractiveButtonsReply/main.go
How to send a text status sendStatus/main.go
How to create an instance (partner method) createInstance/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
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
Status().SendTextStatus The method is aimed for sending a text status SendTextStatus
Status().SendVoiceStatus The method is aimed for sending a voice status SendVoiceStatus
Status().SendMediaStatus The method is used to sending a pictures or video status SendMediaStatus
Status().GetOutgoingStatuses The method returns the outgoing statuses of the account GetOutgoingStatuses
Status().GetIncomingStatuses The method returns the incoming status messages of the account GetIncomingStatuses
Status().GetStatusStatistic The method returns an array of recipients marked for a given status. GetStatusStatistic
Status().DeleteStatus The method is aimed for deleting status. DeleteStatus
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
Sending().SendInteractiveButtons The method is for sending a message with interactive buttons SendInteractiveButtons
Sending().SendInteractiveButtonsReply The method is for sending a message with interactive reply buttons SendInteractiveButtonsReply
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
Partner().CreateInstance The method is aimed to create an instace using partner account. CreateInstance
Partner().DeleteInstanceAccount The method is aimed to delete an instance using partner account. DeleteInstanceAccount
Partner().GetInstances The method is aimed to get all instances on a partner account. GetInstances

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.

About

Create a golang application with WhatsApp API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 9

Languages