Skip to content

Commit b3d1b79

Browse files
committed
Added partner methods
1 parent 5c8d757 commit b3d1b79

File tree

14 files changed

+176
-3
lines changed

14 files changed

+176
-3
lines changed

examples/createInstance/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/green-api/whatsapp-api-client-golang/pkg/api"
7+
)
8+
9+
func main() {
10+
Partner := api.GreenAPI{
11+
PartnerToken: "gac.1234567891234567891234567891213456789",
12+
}
13+
14+
response, err := Partner.Methods().Partner().CreateInstance(map[string]interface{}{
15+
"stateWebhook": "yes",
16+
"incomingWebhook": "yes",
17+
})
18+
if err != nil {
19+
fmt.Println(err)
20+
}
21+
22+
fmt.Println(response)
23+
}

pkg/api/api.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67

@@ -11,6 +12,7 @@ type GreenAPI struct {
1112
URL string
1213
IDInstance string
1314
APITokenInstance string
15+
PartnerToken string
1416
}
1517

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

38+
func (a GreenAPI) PartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error) {
39+
url, err := a.getPartnerURL(APIMethod)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
response, err := executeRequest(method, url, data, filePath)
45+
46+
return response.(map[string]interface{}), err
47+
}
48+
49+
func (a GreenAPI) ArrayPartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error) {
50+
url, err := a.getPartnerURL(APIMethod)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
response, err := executeRequest(method, url, data, filePath)
56+
57+
return response.([]interface{}), err
58+
}
59+
3660
func (a GreenAPI) RawRequest(method, APIMethod string, data map[string]interface{}, filePath string) (interface{}, error) {
3761
url := a.getURL(method, APIMethod, data)
3862

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

76100
return url.String()
77101
}
102+
103+
func (a GreenAPI) getPartnerURL(APIMethod string) (string, error) {
104+
if a.PartnerToken == "" {
105+
return "", fmt.Errorf("error while generating URL: PartnerToken is empty")
106+
}
107+
108+
var url strings.Builder
109+
110+
url.WriteString("https://api.green-api.com")
111+
112+
url.WriteString("/")
113+
url.WriteString("partner")
114+
url.WriteString("/")
115+
url.WriteString(APIMethod)
116+
url.WriteString("/")
117+
url.WriteString(a.PartnerToken)
118+
119+
return url.String(), nil
120+
}

pkg/categories/categories.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,72 @@ type GreenAPICategories struct {
66
GreenAPI methods.GreenAPIInterface
77
}
88

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

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

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

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

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

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

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

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

57+
// Service category presents different service methods.
58+
// https://green-api.com/en/docs/api/service/
4159
func (c GreenAPICategories) Service() methods.ServiceCategory {
4260
return methods.ServiceCategory{GreenAPI: c.GreenAPI}
4361
}
62+
63+
// Partner category presents exclusive methods for partners.
64+
// The partnership scheme involves deeper integration with the service
65+
// and working with a larger number of instances on your side:
66+
//
67+
// * Instance management via API
68+
// * Postpaid billing system (starting from the second month of operation)
69+
// * Daily billing (for created and not deleted instances)
70+
// * Dedicated support line
71+
// For questions regarding connection to the partnership scheme
72+
// and additional conditions, please contact us via email
73+
// at support@green-api.com or via chat on the website.
74+
// https://green-api.com/en/docs/partners/
75+
func (c GreenAPICategories) Partner() methods.PartnerCategory {
76+
return methods.PartnerCategory{GreenAPI: c.GreenAPI}
77+
}

pkg/categories/methods/account.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ type AccountCategory struct {
55
}
66

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

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

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

2730
// GetStateInstance is designed to get the state of the account.
31+
// https://green-api.com/en/docs/api/account/GetStateInstance/
2832
func (c AccountCategory) GetStateInstance() (map[string]interface{}, error) {
2933
return c.GreenAPI.Request("GET", "getStateInstance", nil, "")
3034
}
3135

3236
// GetStatusInstance is designed to get the socket connection state
3337
// of the account instance with WhatsApp.
38+
// https://green-api.com/en/docs/api/account/GetStatusInstance/
3439
func (c AccountCategory) GetStatusInstance() (map[string]interface{}, error) {
3540
return c.GreenAPI.Request("GET", "getStatusInstance", nil, "")
3641
}
3742

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

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

4855
// QR is designed to get a QR code.
56+
// https://green-api.com/en/docs/api/account/QR/
4957
func (c AccountCategory) QR() (map[string]interface{}, error) {
5058
return c.GreenAPI.Request("GET", "qr", nil, "")
5159
}
5260

5361
// SetProfilePicture is designed to set the avatar of the account.
62+
// https://green-api.com/en/docs/api/account/SetProfilePicture/
5463
func (c AccountCategory) SetProfilePicture(filePath string) (map[string]interface{}, error) {
5564
return c.GreenAPI.Request("POST", "setProfilePicture", nil, filePath)
5665
}
5766

5867
// GetAuthorizationCode is designed to authorize an instance by phone number.
68+
// https://green-api.com/en/docs/api/account/GetAuthorizationCode/
5969
func (c AccountCategory) GetAuthorizationCode(phoneNumber int) (map[string]interface{}, error) {
6070
return c.GreenAPI.Request("POST", "getAuthorizationCode", map[string]interface{}{
6171
"phoneNumber": phoneNumber,

pkg/categories/methods/base_category.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ type GreenAPIInterface interface {
44
Request(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error)
55
RawRequest(method, APIMethod string, data map[string]interface{}, filePath string) (interface{}, error)
66
ArrayRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error)
7+
PartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error)
8+
ArrayPartnerRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error)
79
}

pkg/categories/methods/device.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type DeviceCategory struct {
66

77
// GetDeviceInfo is designed to get information about the device (phone)
88
// on which the WhatsApp Business application is running.
9+
// https://green-api.com/en/docs/api/phone/GetDeviceInfo/
910
func (c DeviceCategory) GetDeviceInfo() (map[string]interface{}, error) {
1011
return c.GreenAPI.Request("GET", "getDeviceInfo", nil, "")
1112
}

pkg/categories/methods/groups.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type GroupsCategory struct {
55
}
66

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

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

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

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

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

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

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

6269
// SetGroupPicture sets the avatar of the group.
70+
// https://green-api.com/en/docs/api/groups/SetGroupPicture/
6371
func (c GroupsCategory) SetGroupPicture(filePath, groupId string) (map[string]interface{}, error) {
6472
return c.GreenAPI.Request("POST", "setGroupPicture", map[string]interface{}{
6573
"groupId": groupId,
6674
}, filePath)
6775
}
6876

6977
// LeaveGroup logs the user of the current account out of the group chat.
78+
// https://green-api.com/en/docs/api/groups/LeaveGroup/
7079
func (c GroupsCategory) LeaveGroup(groupId string) (map[string]interface{}, error) {
7180
return c.GreenAPI.Request("POST", "leaveGroup", map[string]interface{}{
7281
"groupId": groupId,

pkg/categories/methods/journals.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ type JournalsCategory struct {
55
}
66

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

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

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

2629
// LastOutgoingMessages returns the last sent messages of the account.
30+
// https://green-api.com/en/docs/api/journals/LastOutgoingMessages/
2731
func (c JournalsCategory) LastOutgoingMessages(parameters map[string]interface{}) ([]interface{}, error) {
2832
return c.GreenAPI.ArrayRequest("GET", "lastOutgoingMessages", parameters, "")
2933
}

pkg/categories/methods/partner.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package methods
2+
3+
type PartnerCategory struct {
4+
GreenAPI GreenAPIInterface
5+
}
6+
7+
// CreateInstance is aimed to create an instace using partner account.
8+
// https://green-api.com/en/docs/partners/createInstance/
9+
func (c PartnerCategory) CreateInstance(parameters map[string]interface{}) (map[string]interface{}, error) {
10+
return c.GreenAPI.PartnerRequest("POST", "createInstance", parameters, "")
11+
}
12+
13+
// DeleteInstanceAccount is aimed to delete an instance using partner account.
14+
// https://green-api.com/en/docs/partners/deleteInstanceAccount/
15+
func (c PartnerCategory) DeleteInstanceAccount(idInstance int) (map[string]interface{}, error) {
16+
return c.GreenAPI.PartnerRequest("POST", "deleteInstanceAccount", map[string]interface{}{"idInstance": idInstance}, "")
17+
}
18+
19+
// GetInstances is aimed to get all instances on a partner account.
20+
// https://green-api.com/en/docs/partners/getInstances/
21+
func (c PartnerCategory) GetInstances() ([]interface{}, error) {
22+
return c.GreenAPI.ArrayPartnerRequest("GET", "getInstances", nil, "")
23+
}

pkg/categories/methods/queues.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ type QueuesCategory struct {
66

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

1314
// ClearMessagesQueue is designed to clear the queue of messages to be sent.
15+
// https://green-api.com/en/docs/api/queues/ClearMessagesQueue/
1416
func (c QueuesCategory) ClearMessagesQueue() (map[string]interface{}, error) {
1517
return c.GreenAPI.Request("GET", "clearMessagesQueue", nil, "")
1618
}

0 commit comments

Comments
 (0)