From ae8c5793dc09d116ad0c4316afa8d08b9ec65de9 Mon Sep 17 00:00:00 2001 From: Vragov Roman Date: Tue, 20 Feb 2024 19:00:54 +0300 Subject: [PATCH] Add request `DialogsTagsAdd` and `DialogTagsDelete` --- v1/client.go | 44 +++++++++++++++++++++++++++++++++++++++ v1/client_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ v1/types.go | 32 ++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) diff --git a/v1/client.go b/v1/client.go index 3dd5273..d7cd6c1 100644 --- a/v1/client.go +++ b/v1/client.go @@ -427,6 +427,50 @@ func (c *MgClient) DialogClose(request uint64) (map[string]interface{}, int, err return resp, status, err } +// DialogsTagsAdd allows to assign dialog to Bot or User +// +// Example: +// +// var client = v1.New("https://demo.url", "09jIJ") +// +// data, status, err := client.DialogsTagsAdd(DialogTagsAddRequest{DialogID: uint64(1),Tags: []TagsAdd{{Name: "foo"}}}) +func (c *MgClient) DialogsTagsAdd(request DialogTagsAddRequest) (int, error) { + outgoing, _ := json.Marshal(&request) + + data, status, err := c.PatchRequest(fmt.Sprintf("/dialogs/%d/tags/add", request.DialogID), outgoing) + if err != nil { + return status, err + } + + if status != http.StatusOK { + return status, c.Error(data) + } + + return status, err +} + +// DialogTagsDelete allows to assign dialog to Bot or User +// +// Example: +// +// var client = v1.New("https://demo.url", "09jIJ") +// +// data, status, err := client.DialogsTagsAdd(DialogTagsDelete{DialogID: uint64(1),Tags: []TagsDelete{{Name: "foo"}}}) +func (c *MgClient) DialogTagsDelete(request DialogTagsDeleteRequest) (int, error) { + outgoing, _ := json.Marshal(&request) + + data, status, err := c.PatchRequest(fmt.Sprintf("/dialogs/%d/tags/delete", request.DialogID), outgoing) + if err != nil { + return status, err + } + + if status != http.StatusOK { + return status, c.Error(data) + } + + return status, err +} + // Messages get all available messages // // Example: diff --git a/v1/client_test.go b/v1/client_test.go index 292b34b..2cad797 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -473,6 +473,59 @@ func TestMgClient_DialogClose(t *testing.T) { assert.Equal(t, http.StatusBadRequest, status) } +func TestMgClient_DialogsTagsAdd(t *testing.T) { + c := client() + + color := ColorBlue + req := DialogTagsAddRequest{ + DialogID: uint64(1), + Tags: []TagsAdd{ + {Name: "foo", ColorCode: nil}, + {Name: "bar", ColorCode: &color}, + }, + } + r, _ := json.Marshal(req) + + defer gock.Off() + + gock.New(mgURL). + Patch("/api/bot/v1/dialogs/1/tags/add"). + JSON(r). + Reply(200). + BodyString(`{}`) + + status, err := c.DialogsTagsAdd(req) + + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, status) +} + +func TestMgClient_DialogsTagsDelete(t *testing.T) { + c := client() + + req := DialogTagsDeleteRequest{ + DialogID: uint64(1), + Tags: []TagsDelete{ + {Name: "foo"}, + {Name: "bar"}, + }, + } + r, _ := json.Marshal(req) + + defer gock.Off() + + gock.New(mgURL). + Patch("/api/bot/v1/dialogs/1/tags/delete"). + JSON(r). + Reply(200). + BodyString(`{}`) + + status, err := c.DialogTagsDelete(req) + + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, status) +} + func TestMgClient_Messages(t *testing.T) { c := client() diff --git a/v1/types.go b/v1/types.go index 2e228e6..2c9aff6 100644 --- a/v1/types.go +++ b/v1/types.go @@ -76,6 +76,19 @@ const ( SuggestionTypeText = "text" SuggestionTypeEmail = "email" SuggestionTypePhone = "phone" + + ColorLightRed = "light-red" + ColorLightBlue = "light-blue" + ColorLightGreen = "light-green" + ColorLightOrange = "light-orange" + ColorLightGray = "light-gray" + ColorLightGrayishBlue = "light-grayish-blue" + ColorRed = "red" + ColorBlue = "blue" + ColorGreen = "green" + ColorOrange = "orange" + ColorGray = "gray" + ColorGrayishBlue = "grayish-blue" ) // MgClient type @@ -166,6 +179,25 @@ type ( BotID uint64 `url:"bot_id,omitempty" json:"bot_id"` } + DialogTagsAddRequest struct { + DialogID uint64 `url:"dialog_id,omitempty"` + Tags []TagsAdd `json:"tags"` + } + + TagsAdd struct { + Name string `json:"name"` + ColorCode *string `json:"color_code"` + } + + DialogTagsDeleteRequest struct { + DialogID uint64 `url:"dialog_id,omitempty"` + Tags []TagsDelete `json:"tags"` + } + + TagsDelete struct { + Name string `json:"name"` + } + MessagesRequest struct { ID []int `url:"id,omitempty"` ChatID uint64 `url:"chat_id,omitempty" json:"chat_id"`