diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..e9ba1cc --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "daily" + allow: + - dependency-type: "all" diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..b967eaf --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,30 @@ +name: Go + +on: + push: + branches: + - "master" + pull_request: + branches: + - "master" + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [ "1.19", "1.20", "1.21" ] + + steps: + - uses: actions/checkout@v4 + - name: Setup Go ${{ matrix.go-version }} + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/README.md b/README.md index d658d4d..21e832a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # whatsapp-api-client-golang -- [Документация на русском языке](docs/README_RU.md) +- [Документация на русском языке](docs/README_RU.md). whatsapp-api-client-golang is a library for integration with WhatsApp messenger using the API service [green-api.com](https://green-api.com/en/). You should get a registration token and an account ID in @@ -120,6 +120,30 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { }) ``` +### 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](examples/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 | @@ -129,12 +153,14 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { | How to send a file by URL | [sendFileByURL/main.go](examples/sendFileByURL/main.go) | | How to send a message | [sendMessage/main.go](examples/sendMessage/main.go) | | How to receive incoming notifications | [webhook/main.go](examples/webhook/main.go) | +| How to send a message with a poll | [sendPoll/main.go](examples/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](https://green-api.com/en/docs/api/account/GetSettings/) | +| `Account().GetWaSettings` | The method is designed to get information about the WhatsApp account | [GetSettings](https://green-api.com/en/docs/api/account/GetWaSettings/) | | `Account().SetSettings` | The method is designed to set the account settings | [SetSettings](https://green-api.com/docs/api/account/SetSettings/) | | `Account().GetStateInstance` | The method is designed to get the state of the account | [GetStateInstance](https://green-api.com/en/docs/api/account/GetStateInstance/) | | `Account().GetStatusInstance` | The method is designed to get the socket connection state of the account instance with WhatsApp | [GetStatusInstance](https://green-api.com/en/docs/api/account/GetStatusInstance/) | @@ -142,6 +168,7 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { | `Account().Logout` | The method is designed to unlogin the account | [Logout](https://green-api.com/en/docs/api/account/Logout/) | | `Account().QR` | The method is designed to get a QR code | [QR](https://green-api.com/en/docs/api/account/QR/) | | `Account().SetProfilePicture` | The method is designed to set the avatar of the account | [SetProfilePicture](https://green-api.com/en/docs/api/account/SetProfilePicture/) | +| `Account().GetAuthorizationCode` | The method is designed to authorize an instance by phone number | [GetAuthorizationCode](https://green-api.com/en/docs/api/account/GetAuthorizationCode/) | | `Device().GetDeviceInfo` | The method is designed to get information about the device (phone) on which the WhatsApp Business application is running | [GetDeviceInfo](https://green-api.com/en/docs/api/phone/GetDeviceInfo/) | | `Groups().CreateGroup` | The method is designed to create a group chat | [CreateGroup](https://green-api.com/en/docs/api/groups/CreateGroup/) | | `Groups().UpdateGroupName` | The method changes the name of the group chat | [UpdateGroupName](https://green-api.com/en/docs/api/groups/UpdateGroupName/) | @@ -173,6 +200,7 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { | `Sending().SendContact` | The method is for sending a message with a contact | [SendContact](https://green-api.com/en/docs/api/sending/SendContact/) | | `Sending().SendLink` | The method is designed to send a message with a link that will add an image preview, title and description | [SendLink](https://green-api.com/en/docs/api/sending/SendLink/) | | `Sending().ForwardMessages` | The method is designed for forwarding messages to a personal or group chat | [ForwardMessages](https://green-api.com/en/docs/api/sending/ForwardMessages/) | +| `Sending().SendPoll` | The method is designed for sending messages with a poll to a private or group chat | [SendPoll](https://green-api.com/en/docs/api/sending/SendPoll/) | | `Service().CheckWhatsapp` | The method checks if there is a WhatsApp account on the phone number | [CheckWhatsapp](https://green-api.com/en/docs/api/service/CheckWhatsapp/) | | `Service().GetAvatar` | The method returns the avatar of the correspondent or group chat | [GetAvatar](https://green-api.com/en/docs/api/service/GetAvatar/) | | `Service().GetContacts` | The method is designed to get a list of contacts of the current account | [GetContacts](https://green-api.com/en/docs/api/service/GetContacts/) | @@ -186,7 +214,7 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { ## Service methods documentation -[Service methods documentation](https://green-api.com/en/docs/api/) +[Service methods documentation](https://green-api.com/en/docs/api/). ## License diff --git a/docs/README_RU.md b/docs/README_RU.md index 8cc6d7f..337f55e 100644 --- a/docs/README_RU.md +++ b/docs/README_RU.md @@ -119,6 +119,30 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { }) ``` +### Как отправить сообщение с опросом + +Если у метода API есть необязательные параметры, то в метод библиотеки нужно передавать JSON (`map[string]interface{}`). + +Ссылка на пример: [sendPoll/main.go](../examples/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", + }, + }, +}) +``` + ## Список примеров | Описание | Ссылка на пример | @@ -128,12 +152,14 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { | Как отправить файл по ссылке | [sendFileByURL/main.go](../examples/sendFileByURL/main.go) | | Как отправить сообщение | [sendMessage/main.go](../examples/sendMessage/main.go) | | Как получать входящие уведомления | [webhook/main.go](../examples/webhook/main.go) | +| Как отправить сообщение с опросом | [sendPoll/main.go](../examples/sendPoll/main.go) | ## Список всех методов библиотеки | Метод API | Описание | Documentation link | |-----------------------------------|---------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------| | `Account().GetSettings` | Метод предназначен для получения текущих настроек аккаунта | [GetSettings](https://green-api.com/docs/api/account/GetSettings/) | +| `Account().GetWaSettings` | Метод предназначен для получения информации о аккаунте WhatsApp | [GetWaSettings](https://green-api.com/docs/api/account/GetWaSettings/) | | `Account().SetSettings` | Метод предназначен для установки настроек аккаунта | [SetSettings](https://green-api.com/docs/api/account/SetSettings/) | | `Account().GetStateInstance` | Метод предназначен для получения состояния аккаунта | [GetStateInstance](https://green-api.com/docs/api/account/GetStateInstance/) | | `Account().GetStatusInstance` | Метод предназначен для получения состояния сокета соединения инстанса аккаунта с WhatsApp | [GetStatusInstance](https://green-api.com/docs/api/account/GetStatusInstance/) | @@ -141,6 +167,7 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { | `Account().Logout` | Метод предназначен для разлогинивания аккаунта | [Logout](https://green-api.com/docs/api/account/Logout/) | | `Account().QR` | Метод предназначен для получения QR-кода | [QR](https://green-api.com/docs/api/account/QR/) | | `Account().SetProfilePicture` | Метод предназначен для установки аватара аккаунта | [SetProfilePicture](https://green-api.com/docs/api/account/SetProfilePicture/) | +| `Account().GetAuthorizationCode` | Метод предназначен для авторизации инстанса по номеру телефона | [GetAuthorizationCode](https://green-api.com/docs/api/account/GetAuthorizationCode/) | | `Device().GetDeviceInfo` | Метод предназначен для получения информации об устройстве (телефоне), на котором запущено приложение WhatsApp Business | [GetDeviceInfo](https://green-api.com/docs/api/phone/GetDeviceInfo/) | | `Groups().CreateGroup` | Метод предназначен для создания группового чата | [CreateGroup](https://green-api.com/docs/api/groups/CreateGroup/) | | `Groups().UpdateGroupName` | Метод изменяет наименование группового чата | [UpdateGroupName](https://green-api.com/docs/api/groups/UpdateGroupName/) | @@ -173,6 +200,7 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { | `Sending().SendLink` | Метод предназначен для отправки сообщения со ссылкой, по которой будут добавлены превью изображения, заголовок и описание | [SendLink](https://green-api.com/docs/api/sending/SendLink/) | | `Sending().ForwardMessages` | Метод предназначен для пересылки сообщений в личный или групповой чат | [ForwardMessages](https://green-api.com/docs/api/sending/ForwardMessages/) | | `Sending().UploadFile` | Метод позволяет выгружать файл из локальной файловой системы, который позднее можно отправить методом SendFileByUrl | [UploadFile](https://green-api.com/docs/api/sending/UploadFile/) | +| `Sending().SendPoll` | Метод предназначен для отправки сообщения с опросом в личный или групповой чат | [SendPoll](https://green-api.com/docs/api/sending/SendPoll/) | | `Service().CheckWhatsapp` | Метод проверяет наличие аккаунта WhatsApp на номере телефона | [CheckWhatsapp](https://green-api.com/docs/api/service/CheckWhatsapp/) | | `Service().GetAvatar` | Метод возвращает аватар корреспондента или группового чата | [GetAvatar](https://green-api.com/docs/api/service/GetAvatar/) | | `Service().GetContacts` | Метод предназначен для получения списка контактов текущего аккаунта | [GetContacts](https://green-api.com/docs/api/service/GetContacts/) | @@ -186,7 +214,7 @@ GreenAPIWebhook.Start(func(body map[string]interface{}) { ## Документация по методам сервиса -[Документация по методам сервиса](https://green-api.com/docs/api/) +[Документация по методам сервиса](https://green-api.com/docs/api/). ## Лицензия diff --git a/examples/sendPoll/main.go b/examples/sendPoll/main.go new file mode 100644 index 0000000..ce44795 --- /dev/null +++ b/examples/sendPoll/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "log" + + "github.com/green-api/whatsapp-api-client-golang/pkg/api" +) + +func main() { + GreenAPI := api.GreenAPI{ + IDInstance: "1101000001", + APITokenInstance: "d75b3a66374942c5b3c019c698abc2067e151558acbd412345", + } + + 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", + }, + }, + }) + if err != nil { + log.Fatal(err) + } + + fmt.Println(response) +} diff --git a/go.mod b/go.mod index f901845..43a1f1f 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module github.com/green-api/whatsapp-api-client-golang go 1.19 + +require github.com/gabriel-vasile/mimetype v1.4.3 + +require golang.org/x/net v0.19.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a691820 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= diff --git a/pkg/api/api.go b/pkg/api/api.go index 24e9e48..cda8bc7 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -18,7 +18,11 @@ func (a GreenAPI) Methods() categories.GreenAPICategories { } func (a GreenAPI) Webhook() GreenAPIWebhook { - return GreenAPIWebhook{GreenAPI: a} + return GreenAPIWebhook{ + GreenAPI: a, + + ErrorChannel: make(chan error), + } } func (a GreenAPI) Request(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error) { diff --git a/pkg/api/http.go b/pkg/api/http.go index 969bc61..491f09c 100644 --- a/pkg/api/http.go +++ b/pkg/api/http.go @@ -6,53 +6,58 @@ import ( "errors" "fmt" "io" - "log" "mime/multipart" "net/http" "os" "strings" + + "github.com/gabriel-vasile/mimetype" ) func executeRequest(method, url string, data map[string]interface{}, filePath string) (map[string]interface{}, error) { client := &http.Client{} - req := getRequest(method, url, data, filePath) - if strings.Contains(url, "UploadFile") { - req = getUploadFileRequest(method, url, filePath) + req, err := getRequest(method, url, data, filePath) + if strings.Contains(url, "uploadFile") { + req, err = getUploadFileRequest(method, url, filePath) + } + + if err != nil { + return nil, err } resp, err := client.Do(req) if err != nil { - log.Fatal(err) + return nil, err } return getResponse(resp) } -func getRequest(method, url string, data map[string]interface{}, filePath string) *http.Request { +func getRequest(method, url string, data map[string]interface{}, filePath string) (*http.Request, error) { if method == http.MethodGet || method == http.MethodDelete { req, err := http.NewRequest(method, url, nil) if err != nil { - log.Fatal(err) + return nil, err } - return req + return req, nil } if filePath == "" { buf, err := json.Marshal(data) if err != nil { - log.Fatal(err) + return nil, err } req, err := http.NewRequest(method, url, bytes.NewBuffer(buf)) if err != nil { - log.Fatal(err) + return nil, err } - req.Header.Add("Content-Type", "application/json") + req.Header.Set("Content-Type", "application/json") - return req + return req, nil } buffer := &bytes.Buffer{} @@ -63,71 +68,73 @@ func getRequest(method, url string, data map[string]interface{}, filePath string for key, value := range data { err := writer.WriteField(key, value.(string)) if err != nil { - log.Fatal(err) + return nil, err } } } file, err := os.Open(filePath) if err != nil { - log.Fatal(err) + return nil, err } part, err := writer.CreateFormFile("file", filePath) if err != nil { - log.Fatal(err) + return nil, err } _, err = io.Copy(part, file) if err != nil { - log.Fatal(err) + return nil, err } err = file.Close() if err != nil { - log.Fatal(err) + return nil, err } err = writer.Close() if err != nil { - log.Fatal(err) + return nil, err } req, err := http.NewRequest(method, url, buffer) if err != nil { - log.Fatal(err) + return nil, err } - req.Header.Add("Content-Type", writer.FormDataContentType()) + req.Header.Set("Content-Type", writer.FormDataContentType()) - return req + return req, nil } -func getUploadFileRequest(method, url string, filePath string) *http.Request { +func getUploadFileRequest(method, url string, filePath string) (*http.Request, error) { buf, err := os.ReadFile(filePath) if err != nil { - log.Fatal(err) + return nil, err } req, err := http.NewRequest(method, url, bytes.NewBuffer(buf)) if err != nil { - log.Fatal(err) + return nil, err } - req.Header.Add("Content-Type", http.DetectContentType(buf)) + MIMEType := mimetype.Detect(buf).String() + + req.Header.Set("Content-Type", MIMEType) - return req + return req, nil } func getResponse(resp *http.Response) (map[string]interface{}, error) { body, err := io.ReadAll(resp.Body) if err != nil { - log.Fatal(err) + return nil, err } err = resp.Body.Close() if err != nil { - log.Fatal(err) + return nil, err } if resp.StatusCode != http.StatusOK { @@ -138,7 +145,7 @@ func getResponse(resp *http.Response) (map[string]interface{}, error) { err = json.Unmarshal(body, &data) if err != nil { - log.Fatal(err) + return nil, err } return data, nil diff --git a/pkg/api/webhook.go b/pkg/api/webhook.go index fe97c96..954b5fc 100644 --- a/pkg/api/webhook.go +++ b/pkg/api/webhook.go @@ -1,9 +1,11 @@ package api -import "log" +import "time" type GreenAPIWebhook struct { GreenAPI GreenAPI + + ErrorChannel chan error } var running = true @@ -12,7 +14,11 @@ func (w GreenAPIWebhook) Start(handler func(map[string]interface{})) { for running { response, err := w.GreenAPI.Methods().Receiving().ReceiveNotification() if err != nil { - log.Fatal(err) + w.ErrorChannel <- err + + time.Sleep(time.Second * 5) + + continue } if response == nil { @@ -24,7 +30,11 @@ func (w GreenAPIWebhook) Start(handler func(map[string]interface{})) { receiptId := int(response["receiptId"].(float64)) response, err = w.GreenAPI.Methods().Receiving().DeleteNotification(receiptId) if err != nil { - log.Fatal(err) + w.ErrorChannel <- err + + time.Sleep(time.Second * 5) + + continue } } } diff --git a/pkg/categories/methods/account.go b/pkg/categories/methods/account.go index e81d21a..b5b054d 100644 --- a/pkg/categories/methods/account.go +++ b/pkg/categories/methods/account.go @@ -4,47 +4,60 @@ type AccountCategory struct { GreenAPI GreenAPIInterface } -// GetSettings is designed to get the current settings of the account +// GetSettings is designed to get the current settings of the account. func (c AccountCategory) GetSettings() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "GetSettings", nil, "") + return c.GreenAPI.Request("GET", "getSettings", nil, "") } -// SetSettings is for setting the account settings +// GetWaSettings is designed to get information about the WhatsApp account. +func (c AccountCategory) GetWaSettings() (map[string]interface{}, error) { + return c.GreenAPI.Request("GET", "getWaSettings", nil, "") +} + +// SetSettings is for setting the account settings. func (c AccountCategory) SetSettings(parameters map[string]interface{}) (map[string]interface{}, error) { method := "GET" if parameters != nil { method = "POST" } - return c.GreenAPI.Request(method, "SetSettings", parameters, "") + return c.GreenAPI.Request(method, "setSettings", parameters, "") } -// GetStateInstance is designed to get the state of the account +// GetStateInstance is designed to get the state of the account. func (c AccountCategory) GetStateInstance() (map[string]interface{}, error) { return c.GreenAPI.Request("GET", "getStateInstance", nil, "") } -// GetStatusInstance is designed to get the socket connection state of the account instance with WhatsApp +// GetStatusInstance is designed to get the socket connection state +// of the account instance with WhatsApp. func (c AccountCategory) GetStatusInstance() (map[string]interface{}, error) { return c.GreenAPI.Request("GET", "getStatusInstance", nil, "") } -// Reboot is designed to restart the account +// Reboot is designed to restart the account. func (c AccountCategory) Reboot() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "Reboot", nil, "") + return c.GreenAPI.Request("GET", "reboot", nil, "") } -// Logout is designed to unlogin the account +// Logout is designed to unlogin the account. func (c AccountCategory) Logout() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "Logout", nil, "") + return c.GreenAPI.Request("GET", "logout", nil, "") } -// QR is designed to get a QR code +// QR is designed to get a QR code. func (c AccountCategory) QR() (map[string]interface{}, error) { return c.GreenAPI.Request("GET", "qr", nil, "") } -// SetProfilePicture is designed to set the avatar of the account +// SetProfilePicture is designed to set the avatar of the account. func (c AccountCategory) SetProfilePicture(filePath string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "setProfilePicture", nil, filePath) } + +// GetAuthorizationCode is designed to authorize an instance by phone number. +func (c AccountCategory) GetAuthorizationCode(phoneNumber int) (map[string]interface{}, error) { + return c.GreenAPI.Request("POST", "getAuthorizationCode", map[string]interface{}{ + "phoneNumber": phoneNumber, + }, "") +} diff --git a/pkg/categories/methods/device.go b/pkg/categories/methods/device.go index 7ae9176..8a00511 100644 --- a/pkg/categories/methods/device.go +++ b/pkg/categories/methods/device.go @@ -4,7 +4,8 @@ type DeviceCategory struct { GreenAPI GreenAPIInterface } -// GetDeviceInfo is designed to get information about the device (phone) on which the WhatsApp Business application is running +// GetDeviceInfo is designed to get information about the device (phone) +// on which the WhatsApp Business application is running. func (c DeviceCategory) GetDeviceInfo() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "GetDeviceInfo", nil, "") + return c.GreenAPI.Request("GET", "getDeviceInfo", nil, "") } diff --git a/pkg/categories/methods/groups.go b/pkg/categories/methods/groups.go index 3dbc8f0..1046724 100644 --- a/pkg/categories/methods/groups.go +++ b/pkg/categories/methods/groups.go @@ -4,71 +4,71 @@ type GroupsCategory struct { GreenAPI GreenAPIInterface } -// CreateGroup is designed to create a group chat +// CreateGroup is designed to create a group chat. func (c GroupsCategory) CreateGroup(groupName string, chatIds []string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "CreateGroup", map[string]interface{}{ + return c.GreenAPI.Request("POST", "createGroup", map[string]interface{}{ "groupName": groupName, "chatIds": chatIds, }, "") } -// UpdateGroupName changes the name of the group chat +// UpdateGroupName changes the name of the group chat. func (c GroupsCategory) UpdateGroupName(groupId, groupName string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "UpdateGroupName", map[string]interface{}{ + return c.GreenAPI.Request("POST", "updateGroupName", map[string]interface{}{ "groupId": groupId, "groupName": groupName, }, "") } -// GetGroupData gets group chat data +// GetGroupData gets group chat data. func (c GroupsCategory) GetGroupData(groupId string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "GetGroupData", map[string]interface{}{ + return c.GreenAPI.Request("POST", "getGroupData", map[string]interface{}{ "groupId": groupId, }, "") } -// AddGroupParticipant adds a participant to the group chat +// AddGroupParticipant adds a participant to the group chat. func (c GroupsCategory) AddGroupParticipant(groupId, participantChatId string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "AddGroupParticipant", map[string]interface{}{ + return c.GreenAPI.Request("POST", "addGroupParticipant", map[string]interface{}{ "groupId": groupId, "participantChatId": participantChatId, }, "") } -// RemoveGroupParticipant removes the participant from the group chat +// RemoveGroupParticipant removes the participant from the group chat. func (c GroupsCategory) RemoveGroupParticipant(groupId, participantChatId string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "RemoveGroupParticipant", map[string]interface{}{ + return c.GreenAPI.Request("POST", "removeGroupParticipant", map[string]interface{}{ "groupId": groupId, "participantChatId": participantChatId, }, "") } -// SetGroupAdmin designates a member of a group chat as an administrator +// SetGroupAdmin designates a member of a group chat as an administrator. func (c GroupsCategory) SetGroupAdmin(groupId, participantChatId string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "SetGroupAdmin", map[string]interface{}{ + return c.GreenAPI.Request("POST", "setGroupAdmin", map[string]interface{}{ "groupId": groupId, "participantChatId": participantChatId, }, "") } -// RemoveAdmin deprives the participant of group chat administration rights +// RemoveAdmin deprives the participant of group chat administration rights. func (c GroupsCategory) RemoveAdmin(groupId, participantChatId string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "RemoveAdmin", map[string]interface{}{ + return c.GreenAPI.Request("POST", "removeAdmin", map[string]interface{}{ "groupId": groupId, "participantChatId": participantChatId, }, "") } -// SetGroupPicture sets the avatar of the group +// SetGroupPicture sets the avatar of the group. func (c GroupsCategory) SetGroupPicture(filePath, groupId string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "setGroupPicture", map[string]interface{}{ "groupId": groupId, }, filePath) } -// LeaveGroup logs the user of the current account out of the group chat +// LeaveGroup logs the user of the current account out of the group chat. func (c GroupsCategory) LeaveGroup(groupId string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "LeaveGroup", map[string]interface{}{ + return c.GreenAPI.Request("POST", "leaveGroup", map[string]interface{}{ "groupId": groupId, }, "") } diff --git a/pkg/categories/methods/journals.go b/pkg/categories/methods/journals.go index 1acc820..ba23750 100644 --- a/pkg/categories/methods/journals.go +++ b/pkg/categories/methods/journals.go @@ -4,12 +4,12 @@ type JournalsCategory struct { GreenAPI GreenAPIInterface } -// GetChatHistory returns the chat message history +// GetChatHistory returns the chat message history. func (c JournalsCategory) GetChatHistory(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "GetChatHistory", parameters, "") + return c.GreenAPI.Request("POST", "getChatHistory", parameters, "") } -// GetMessage returns a chat message +// GetMessage returns a chat message. func (c JournalsCategory) GetMessage(chatId, idMessage string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "getMessage", map[string]interface{}{ "chatId": chatId, @@ -17,12 +17,13 @@ func (c JournalsCategory) GetMessage(chatId, idMessage string) (map[string]inter }, "") } -// LastIncomingMessages returns the most recent incoming messages of the account +// LastIncomingMessages returns the most recent incoming messages +// of the account. func (c JournalsCategory) LastIncomingMessages(parameters map[string]interface{}) (map[string]interface{}, error) { return c.GreenAPI.Request("GET", "lastIncomingMessages", parameters, "") } -// LastOutgoingMessages returns the last sent messages of the account +// LastOutgoingMessages returns the last sent messages of the account. func (c JournalsCategory) LastOutgoingMessages(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "LastOutgoingMessages", parameters, "") + return c.GreenAPI.Request("GET", "lastOutgoingMessages", parameters, "") } diff --git a/pkg/categories/methods/queues.go b/pkg/categories/methods/queues.go index 039048b..5da5bf2 100644 --- a/pkg/categories/methods/queues.go +++ b/pkg/categories/methods/queues.go @@ -4,12 +4,13 @@ type QueuesCategory struct { GreenAPI GreenAPIInterface } -// ShowMessagesQueue is designed to get the list of messages that are in the queue to be sent +// ShowMessagesQueue is designed to get the list of messages +// that are in the queue to be sent. func (c QueuesCategory) ShowMessagesQueue() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "ShowMessagesQueue", nil, "") + return c.GreenAPI.Request("GET", "showMessagesQueue", nil, "") } -// ClearMessagesQueue is designed to clear the queue of messages to be sent +// ClearMessagesQueue is designed to clear the queue of messages to be sent. func (c QueuesCategory) ClearMessagesQueue() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "ClearMessagesQueue", nil, "") + return c.GreenAPI.Request("GET", "clearMessagesQueue", nil, "") } diff --git a/pkg/categories/methods/read_mark.go b/pkg/categories/methods/read_mark.go index 742d96e..a8763dc 100644 --- a/pkg/categories/methods/read_mark.go +++ b/pkg/categories/methods/read_mark.go @@ -4,7 +4,7 @@ type ReadMarkCategory struct { GreenAPI GreenAPIInterface } -// ReadChat is designed to mark chat messages as read +// ReadChat is designed to mark chat messages as read. func (c ReadMarkCategory) ReadChat(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "ReadChat", parameters, "") + return c.GreenAPI.Request("POST", "readChat", parameters, "") } diff --git a/pkg/categories/methods/receiving.go b/pkg/categories/methods/receiving.go index 3b5d6b3..d07aca7 100644 --- a/pkg/categories/methods/receiving.go +++ b/pkg/categories/methods/receiving.go @@ -4,19 +4,21 @@ type ReceivingCategory struct { GreenAPI GreenAPIInterface } -// ReceiveNotification is designed to receive a single incoming notification from the notification queue +// ReceiveNotification is designed to receive a single incoming notification +// from the notification queue. func (c ReceivingCategory) ReceiveNotification() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "ReceiveNotification", nil, "") + return c.GreenAPI.Request("GET", "receiveNotification", nil, "") } -// DeleteNotification is designed to remove an incoming notification from the notification queue +// DeleteNotification is designed to remove an incoming notification +// from the notification queue. func (c ReceivingCategory) DeleteNotification(receiptId int) (map[string]interface{}, error) { - return c.GreenAPI.Request("DELETE", "DeleteNotification", map[string]interface{}{ + return c.GreenAPI.Request("DELETE", "deleteNotification", map[string]interface{}{ "receiptId": receiptId, }, "") } -// DownloadFile is for downloading received and sent files +// DownloadFile is for downloading received and sent files. func (c ReceivingCategory) DownloadFile(chatId, idMessage string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "downloadFile", map[string]interface{}{ "chatId": chatId, diff --git a/pkg/categories/methods/sending.go b/pkg/categories/methods/sending.go index 9881dc1..03059b8 100644 --- a/pkg/categories/methods/sending.go +++ b/pkg/categories/methods/sending.go @@ -4,52 +4,57 @@ type SendingCategory struct { GreenAPI GreenAPIInterface } -// SendMessage is designed to send a text message to a personal or group chat +// SendMessage is designed to send a text message to a personal or group chat. func (c SendingCategory) SendMessage(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "SendMessage", parameters, "") + return c.GreenAPI.Request("POST", "sendMessage", parameters, "") } -// SendButtons is designed to send a message with buttons to a personal or group chat +// SendButtons is designed to send a message with buttons +// to a personal or group chat. func (c SendingCategory) SendButtons(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "SendButtons", parameters, "") + return c.GreenAPI.Request("POST", "sendButtons", parameters, "") } -// SendTemplateButtons is designed to send a message with interactive buttons from the list of templates in a personal or group chat +// SendTemplateButtons is designed to send a message with interactive buttons +// from the list of templates in a personal or group chat. func (c SendingCategory) SendTemplateButtons(parameters map[string]interface{}) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "sendTemplateButtons", parameters, "") } -// SendListMessage is designed to send a message with a selection button from a list of values to a personal or group chat +// SendListMessage is designed to send a message with a selection button +// from a list of values to a personal or group chat. func (c SendingCategory) SendListMessage(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "SendListMessage", parameters, "") + return c.GreenAPI.Request("POST", "sendListMessage", parameters, "") } -// SendFileByUpload is designed to send a file loaded through a form (form-data) +// SendFileByUpload is designed to send a file loaded through a form (form-data). func (c SendingCategory) SendFileByUpload(filePath string, parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "SendFileByUpload", parameters, filePath) + return c.GreenAPI.Request("POST", "sendFileByUpload", parameters, filePath) } -// SendFileByUrl is designed to send a file downloaded via a link +// SendFileByUrl is designed to send a file downloaded via a link. func (c SendingCategory) SendFileByUrl(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "SendFileByUrl", parameters, "") + return c.GreenAPI.Request("POST", "sendFileByUrl", parameters, "") } -// SendLocation is designed to send a geolocation message +// SendLocation is designed to send a geolocation message. func (c SendingCategory) SendLocation(parameters map[string]interface{}) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "sendLocation", parameters, "") } -// SendContact is for sending a message with a contact +// SendContact is for sending a message with a contact. func (c SendingCategory) SendContact(parameters map[string]interface{}) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "sendContact", parameters, "") } -// SendLink is designed to send a message with a link that will add an image preview, title and description +// SendLink is designed to send a message with a link +// that will add an image preview, title and description. func (c SendingCategory) SendLink(parameters map[string]interface{}) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "sendLink", parameters, "") } -// ForwardMessages is designed for forwarding messages to a personal or group chat +// ForwardMessages is designed for forwarding messages +// to a personal or group chat. func (c SendingCategory) ForwardMessages(chatId, chatIdFrom string, messages []string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "forwardMessages", map[string]interface{}{ "chatId": chatId, @@ -58,7 +63,14 @@ func (c SendingCategory) ForwardMessages(chatId, chatIdFrom string, messages []s }, "") } -// UploadFile allows you to upload a file from the local file system, which can later be sent using the SendFileByUrl method +// UploadFile allows you to upload a file from the local file system, +// which can later be sent using the SendFileByUrl method. func (c SendingCategory) UploadFile(filePath string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "UploadFile", nil, filePath) + return c.GreenAPI.Request("POST", "uploadFile", nil, filePath) +} + +// SendPoll is designed for sending messages with a poll +// to a private or group chat. +func (c SendingCategory) SendPoll(parameters map[string]interface{}) (map[string]interface{}, error) { + return c.GreenAPI.Request("POST", "sendPoll", parameters, "") } diff --git a/pkg/categories/methods/service.go b/pkg/categories/methods/service.go index 76c7af7..6b70fa7 100644 --- a/pkg/categories/methods/service.go +++ b/pkg/categories/methods/service.go @@ -4,33 +4,33 @@ type ServiceCategory struct { GreenAPI GreenAPIInterface } -// CheckWhatsapp checks if there is a WhatsApp account on the phone number +// CheckWhatsapp checks if there is a WhatsApp account on the phone number. func (c ServiceCategory) CheckWhatsapp(phoneNumber int) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "CheckWhatsapp", map[string]interface{}{ + return c.GreenAPI.Request("POST", "checkWhatsapp", map[string]interface{}{ "phoneNumber": phoneNumber, }, "") } -// GetAvatar returns the avatar of the correspondent or group chat +// GetAvatar returns the avatar of the correspondent or group chat. func (c ServiceCategory) GetAvatar(chatId string) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "GetAvatar", map[string]interface{}{ + return c.GreenAPI.Request("POST", "getAvatar", map[string]interface{}{ "chatId": chatId, }, "") } -// GetContacts is designed to get a list of contacts of the current account +// GetContacts is designed to get a list of contacts of the current account. func (c ServiceCategory) GetContacts() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "GetContacts", nil, "") + return c.GreenAPI.Request("GET", "getContacts", nil, "") } -// GetContactInfo is designed to obtain information about the contact +// GetContactInfo is designed to obtain information about the contact. func (c ServiceCategory) GetContactInfo(chatId string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "getContactInfo", map[string]interface{}{ "chatId": chatId, }, "") } -// DeleteMessage deletes the message from chat +// DeleteMessage deletes the message from chat. func (c ServiceCategory) DeleteMessage(chatId, idMessage string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "deleteMessage", map[string]interface{}{ "chatId": chatId, @@ -38,21 +38,22 @@ func (c ServiceCategory) DeleteMessage(chatId, idMessage string) (map[string]int }, "") } -// ArchiveChat archives the chat +// ArchiveChat archives the chat. func (c ServiceCategory) ArchiveChat(chatId string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "archiveChat", map[string]interface{}{ "chatId": chatId, }, "") } -// UnarchiveChat unarchives the chat +// UnarchiveChat unarchives the chat. func (c ServiceCategory) UnarchiveChat(chatId string) (map[string]interface{}, error) { return c.GreenAPI.Request("POST", "unarchiveChat", map[string]interface{}{ "chatId": chatId, }, "") } -// SetDisappearingChat is designed to change the settings of disappearing messages in chats +// SetDisappearingChat is designed to change the settings +// of disappearing messages in chats. func (c ServiceCategory) SetDisappearingChat(parameters map[string]interface{}) (map[string]interface{}, error) { method := "GET" if parameters != nil {