Skip to content

Commit

Permalink
Added partner methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Amele9 committed Apr 4, 2024
1 parent 5c8d757 commit b3d1b79
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 3 deletions.
23 changes: 23 additions & 0 deletions examples/createInstance/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

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

func main() {
Partner := api.GreenAPI{
PartnerToken: "gac.1234567891234567891234567891213456789",
}

response, err := Partner.Methods().Partner().CreateInstance(map[string]interface{}{
"stateWebhook": "yes",
"incomingWebhook": "yes",
})
if err != nil {
fmt.Println(err)
}

fmt.Println(response)
}
43 changes: 43 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"strconv"
"strings"

Expand All @@ -11,6 +12,7 @@ type GreenAPI struct {
URL string
IDInstance string
APITokenInstance string
PartnerToken string
}

func (a GreenAPI) Methods() categories.GreenAPICategories {
Expand All @@ -33,6 +35,28 @@ func (a GreenAPI) Request(method, APIMethod string, data map[string]interface{},
return response.(map[string]interface{}), err
}

func (a GreenAPI) PartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error) {
url, err := a.getPartnerURL(APIMethod)
if err != nil {
return nil, err
}

response, err := executeRequest(method, url, data, filePath)

return response.(map[string]interface{}), err
}

func (a GreenAPI) ArrayPartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error) {
url, err := a.getPartnerURL(APIMethod)
if err != nil {
return nil, err
}

response, err := executeRequest(method, url, data, filePath)

return response.([]interface{}), err
}

func (a GreenAPI) RawRequest(method, APIMethod string, data map[string]interface{}, filePath string) (interface{}, error) {
url := a.getURL(method, APIMethod, data)

Expand Down Expand Up @@ -75,3 +99,22 @@ func (a GreenAPI) getURL(method, APIMethod string, data map[string]interface{})

return url.String()
}

func (a GreenAPI) getPartnerURL(APIMethod string) (string, error) {
if a.PartnerToken == "" {
return "", fmt.Errorf("error while generating URL: PartnerToken is empty")
}

var url strings.Builder

url.WriteString("https://api.green-api.com")

url.WriteString("/")
url.WriteString("partner")
url.WriteString("/")
url.WriteString(APIMethod)
url.WriteString("/")
url.WriteString(a.PartnerToken)

return url.String(), nil
}
34 changes: 34 additions & 0 deletions pkg/categories/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,72 @@ type GreenAPICategories struct {
GreenAPI methods.GreenAPIInterface
}

// Account category presents methods for working with the account.
// https://green-api.com/en/docs/api/account/
func (c GreenAPICategories) Account() methods.AccountCategory {
return methods.AccountCategory{GreenAPI: c.GreenAPI}
}

// Device category presents methods for working with the device (phone).
// https://green-api.com/en/docs/api/phone/
func (c GreenAPICategories) Device() methods.DeviceCategory {
return methods.DeviceCategory{GreenAPI: c.GreenAPI}
}

// Groups category presents methods for working with group chats.
// https://green-api.com/en/docs/api/groups/
func (c GreenAPICategories) Groups() methods.GroupsCategory {
return methods.GroupsCategory{GreenAPI: c.GreenAPI}
}

// Journals present methods for working with incoming and outgoing messages.
// https://green-api.com/en/docs/api/journals/
func (c GreenAPICategories) Journals() methods.JournalsCategory {
return methods.JournalsCategory{GreenAPI: c.GreenAPI}
}

// Queues category presents methods for working with a messages queue.
// https://green-api.com/en/docs/api/queues/
func (c GreenAPICategories) Queues() methods.QueuesCategory {
return methods.QueuesCategory{GreenAPI: c.GreenAPI}
}

// ReadMark category presents methods for working with chat read mark.
// https://green-api.com/en/docs/api/marks/
func (c GreenAPICategories) ReadMark() methods.ReadMarkCategory {
return methods.ReadMarkCategory{GreenAPI: c.GreenAPI}
}

// Receiving category presents methods for working with receiving events.
// https://green-api.com/en/docs/api/receiving/
func (c GreenAPICategories) Receiving() methods.ReceivingCategory {
return methods.ReceivingCategory{GreenAPI: c.GreenAPI}
}

// Sending category presents methods for sending different messages.
// https://green-api.com/en/docs/api/sending/
func (c GreenAPICategories) Sending() methods.SendingCategory {
return methods.SendingCategory{GreenAPI: c.GreenAPI}
}

// Service category presents different service methods.
// https://green-api.com/en/docs/api/service/
func (c GreenAPICategories) Service() methods.ServiceCategory {
return methods.ServiceCategory{GreenAPI: c.GreenAPI}
}

// Partner category presents exclusive methods for partners.
// The partnership scheme involves deeper integration with the service
// and working with a larger number of instances on your side:
//
// * Instance management via API
// * Postpaid billing system (starting from the second month of operation)
// * Daily billing (for created and not deleted instances)
// * Dedicated support line
// For questions regarding connection to the partnership scheme
// and additional conditions, please contact us via email
// at support@green-api.com or via chat on the website.
// https://green-api.com/en/docs/partners/
func (c GreenAPICategories) Partner() methods.PartnerCategory {
return methods.PartnerCategory{GreenAPI: c.GreenAPI}
}
10 changes: 10 additions & 0 deletions pkg/categories/methods/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ type AccountCategory struct {
}

// GetSettings is designed to get the current settings of the account.
// https://green-api.com/en/docs/api/account/GetSettings/
func (c AccountCategory) GetSettings() (map[string]interface{}, error) {
return c.GreenAPI.Request("GET", "getSettings", nil, "")
}

// GetWaSettings is designed to get information about the WhatsApp account.
// https://green-api.com/en/docs/api/account/GetWaSettings/
func (c AccountCategory) GetWaSettings() (map[string]interface{}, error) {
return c.GreenAPI.Request("GET", "getWaSettings", nil, "")
}

// SetSettings is for setting the account settings.
// https://green-api.com/en/docs/api/account/SetSettings/
func (c AccountCategory) SetSettings(parameters map[string]interface{}) (map[string]interface{}, error) {
method := "GET"
if parameters != nil {
Expand All @@ -25,37 +28,44 @@ func (c AccountCategory) SetSettings(parameters map[string]interface{}) (map[str
}

// GetStateInstance is designed to get the state of the account.
// https://green-api.com/en/docs/api/account/GetStateInstance/
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.
// https://green-api.com/en/docs/api/account/GetStatusInstance/
func (c AccountCategory) GetStatusInstance() (map[string]interface{}, error) {
return c.GreenAPI.Request("GET", "getStatusInstance", nil, "")
}

// Reboot is designed to restart the account.
// https://green-api.com/en/docs/api/account/Reboot/
func (c AccountCategory) Reboot() (map[string]interface{}, error) {
return c.GreenAPI.Request("GET", "reboot", nil, "")
}

// Logout is designed to unlogin the account.
// https://green-api.com/en/docs/api/account/Logout/
func (c AccountCategory) Logout() (map[string]interface{}, error) {
return c.GreenAPI.Request("GET", "logout", nil, "")
}

// QR is designed to get a QR code.
// https://green-api.com/en/docs/api/account/QR/
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.
// https://green-api.com/en/docs/api/account/SetProfilePicture/
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.
// https://green-api.com/en/docs/api/account/GetAuthorizationCode/
func (c AccountCategory) GetAuthorizationCode(phoneNumber int) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "getAuthorizationCode", map[string]interface{}{
"phoneNumber": phoneNumber,
Expand Down
2 changes: 2 additions & 0 deletions pkg/categories/methods/base_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ type GreenAPIInterface interface {
Request(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error)
RawRequest(method, APIMethod string, data map[string]interface{}, filePath string) (interface{}, error)
ArrayRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error)
PartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error)
ArrayPartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error)
}
1 change: 1 addition & 0 deletions pkg/categories/methods/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type DeviceCategory struct {

// GetDeviceInfo is designed to get information about the device (phone)
// on which the WhatsApp Business application is running.
// https://green-api.com/en/docs/api/phone/GetDeviceInfo/
func (c DeviceCategory) GetDeviceInfo() (map[string]interface{}, error) {
return c.GreenAPI.Request("GET", "getDeviceInfo", nil, "")
}
9 changes: 9 additions & 0 deletions pkg/categories/methods/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type GroupsCategory struct {
}

// CreateGroup is designed to create a group chat.
// https://green-api.com/en/docs/api/groups/CreateGroup/
func (c GroupsCategory) CreateGroup(groupName string, chatIds []string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "createGroup", map[string]interface{}{
"groupName": groupName,
Expand All @@ -13,6 +14,7 @@ func (c GroupsCategory) CreateGroup(groupName string, chatIds []string) (map[str
}

// UpdateGroupName changes the name of the group chat.
// https://green-api.com/en/docs/api/groups/UpdateGroupName/
func (c GroupsCategory) UpdateGroupName(groupId, groupName string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "updateGroupName", map[string]interface{}{
"groupId": groupId,
Expand All @@ -21,13 +23,15 @@ func (c GroupsCategory) UpdateGroupName(groupId, groupName string) (map[string]i
}

// GetGroupData gets group chat data.
// https://green-api.com/en/docs/api/groups/GetGroupData/
func (c GroupsCategory) GetGroupData(groupId string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "getGroupData", map[string]interface{}{
"groupId": groupId,
}, "")
}

// AddGroupParticipant adds a participant to the group chat.
// https://green-api.com/en/docs/api/groups/AddGroupParticipant/
func (c GroupsCategory) AddGroupParticipant(groupId, participantChatId string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "addGroupParticipant", map[string]interface{}{
"groupId": groupId,
Expand All @@ -36,6 +40,7 @@ func (c GroupsCategory) AddGroupParticipant(groupId, participantChatId string) (
}

// RemoveGroupParticipant removes the participant from the group chat.
// https://green-api.com/en/docs/api/groups/RemoveGroupParticipant/
func (c GroupsCategory) RemoveGroupParticipant(groupId, participantChatId string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "removeGroupParticipant", map[string]interface{}{
"groupId": groupId,
Expand All @@ -44,6 +49,7 @@ func (c GroupsCategory) RemoveGroupParticipant(groupId, participantChatId string
}

// SetGroupAdmin designates a member of a group chat as an administrator.
// https://green-api.com/en/docs/api/groups/SetGroupAdmin/
func (c GroupsCategory) SetGroupAdmin(groupId, participantChatId string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "setGroupAdmin", map[string]interface{}{
"groupId": groupId,
Expand All @@ -52,6 +58,7 @@ func (c GroupsCategory) SetGroupAdmin(groupId, participantChatId string) (map[st
}

// RemoveAdmin deprives the participant of group chat administration rights.
// https://green-api.com/en/docs/api/groups/RemoveAdmin/
func (c GroupsCategory) RemoveAdmin(groupId, participantChatId string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "removeAdmin", map[string]interface{}{
"groupId": groupId,
Expand All @@ -60,13 +67,15 @@ func (c GroupsCategory) RemoveAdmin(groupId, participantChatId string) (map[stri
}

// SetGroupPicture sets the avatar of the group.
// https://green-api.com/en/docs/api/groups/SetGroupPicture/
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.
// https://green-api.com/en/docs/api/groups/LeaveGroup/
func (c GroupsCategory) LeaveGroup(groupId string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "leaveGroup", map[string]interface{}{
"groupId": groupId,
Expand Down
4 changes: 4 additions & 0 deletions pkg/categories/methods/journals.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ type JournalsCategory struct {
}

// GetChatHistory returns the chat message history.
// https://green-api.com/en/docs/api/journals/GetChatHistory/
func (c JournalsCategory) GetChatHistory(parameters map[string]interface{}) ([]interface{}, error) {
return c.GreenAPI.ArrayRequest("POST", "getChatHistory", parameters, "")
}

// GetMessage returns a chat message.
// https://green-api.com/en/docs/api/journals/GetMessage/
func (c JournalsCategory) GetMessage(chatId, idMessage string) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "getMessage", map[string]interface{}{
"chatId": chatId,
Expand All @@ -19,11 +21,13 @@ func (c JournalsCategory) GetMessage(chatId, idMessage string) (map[string]inter

// LastIncomingMessages returns the most recent incoming messages
// of the account.
// https://green-api.com/en/docs/api/journals/LastIncomingMessages/
func (c JournalsCategory) LastIncomingMessages(parameters map[string]interface{}) ([]interface{}, error) {
return c.GreenAPI.ArrayRequest("GET", "lastIncomingMessages", parameters, "")
}

// LastOutgoingMessages returns the last sent messages of the account.
// https://green-api.com/en/docs/api/journals/LastOutgoingMessages/
func (c JournalsCategory) LastOutgoingMessages(parameters map[string]interface{}) ([]interface{}, error) {
return c.GreenAPI.ArrayRequest("GET", "lastOutgoingMessages", parameters, "")
}
23 changes: 23 additions & 0 deletions pkg/categories/methods/partner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package methods

type PartnerCategory struct {
GreenAPI GreenAPIInterface
}

// CreateInstance is aimed to create an instace using partner account.
// https://green-api.com/en/docs/partners/createInstance/
func (c PartnerCategory) CreateInstance(parameters map[string]interface{}) (map[string]interface{}, error) {
return c.GreenAPI.PartnerRequest("POST", "createInstance", parameters, "")
}

// DeleteInstanceAccount is aimed to delete an instance using partner account.
// https://green-api.com/en/docs/partners/deleteInstanceAccount/
func (c PartnerCategory) DeleteInstanceAccount(idInstance int) (map[string]interface{}, error) {
return c.GreenAPI.PartnerRequest("POST", "deleteInstanceAccount", map[string]interface{}{"idInstance": idInstance}, "")
}

// GetInstances is aimed to get all instances on a partner account.
// https://green-api.com/en/docs/partners/getInstances/
func (c PartnerCategory) GetInstances() ([]interface{}, error) {
return c.GreenAPI.ArrayPartnerRequest("GET", "getInstances", nil, "")
}
2 changes: 2 additions & 0 deletions pkg/categories/methods/queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ type QueuesCategory struct {

// ShowMessagesQueue is designed to get the list of messages
// that are in the queue to be sent.
// https://green-api.com/en/docs/api/queues/ShowMessagesQueue/
func (c QueuesCategory) ShowMessagesQueue() ([]interface{}, error) {
return c.GreenAPI.ArrayRequest("GET", "showMessagesQueue", nil, "")
}

// ClearMessagesQueue is designed to clear the queue of messages to be sent.
// https://green-api.com/en/docs/api/queues/ClearMessagesQueue/
func (c QueuesCategory) ClearMessagesQueue() (map[string]interface{}, error) {
return c.GreenAPI.Request("GET", "clearMessagesQueue", nil, "")
}
1 change: 1 addition & 0 deletions pkg/categories/methods/read_mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type ReadMarkCategory struct {
}

// ReadChat is designed to mark chat messages as read.
// https://green-api.com/en/docs/api/marks/ReadChat/
func (c ReadMarkCategory) ReadChat(parameters map[string]interface{}) (map[string]interface{}, error) {
return c.GreenAPI.Request("POST", "readChat", parameters, "")
}
Loading

0 comments on commit b3d1b79

Please sign in to comment.