Compare commits

..

No commits in common. "master" and "v1.5.2" have entirely different histories.

5 changed files with 160 additions and 732 deletions

View file

@ -19,33 +19,33 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4
- name: Set up Go 1.24
uses: actions/setup-go@v5
uses: actions/checkout@v2
- name: Set up latest Go 1.x version
uses: actions/setup-go@v2
with:
go-version: '1.24'
go-version: '1.17'
- name: Get dependencies
run: |
go mod tidy
cp .env.dist .env
- name: Lint code with golangci-lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v3
with:
version: v1.62.2
version: v1.36
only-new-issues: true
tests:
name: Tests
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.13', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.20', '1.21', '1.22', '1.23', '1.24']
go-version: ['1.13', '1.14', '1.15', '1.16']
steps:
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Check out code into the Go module directory
uses: actions/checkout@v4
uses: actions/checkout@v2
- name: Get dependencies
run: |
go mod tidy

View file

@ -1,28 +1,30 @@
run:
skip-dirs-use-default: true
allow-parallel-runners: true
modules-download-mode: readonly
output:
formats:
- format: colored-line-number
format: colored-line-number
sort-results: true
linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- unused
- unparam
- varcheck
- bodyclose
- dogsled
- dupl
- errorlint
- exhaustive
- copyloopvar
- exportloopref
- funlen
- gocognit
- goconst
@ -30,23 +32,26 @@ linters:
- gocyclo
- godot
- goimports
- revive
- mnd
- golint
- gomnd
- gosec
- ifshort
- interfacer
- lll
- makezero
- maligned
- misspell
- nestif
- prealloc
- predeclared
- scopelint
- sqlclosecheck
- unconvert
- whitespace
- unused
- testifylint
linters-settings:
govet:
check-shadowing: false
disable-all: true
enable:
- assign
@ -65,6 +70,7 @@ linters-settings:
- unmarshal
- unreachable
- unsafeptr
- unused
settings:
printf:
funcs:
@ -113,6 +119,8 @@ linters-settings:
- log.Printf
- log.Println
- runtime/trace.Logf
unused:
check-exported: false
unparam:
check-exported: false
dogsled:
@ -121,13 +129,11 @@ linters-settings:
threshold: 200
errorlint:
errorf: true
asserts: true
comparison: true
exhaustive:
check-generated: false
default-signifies-exhaustive: false
funlen:
lines: 90
lines: 60
statements: 40
gocognit:
min-complexity: 25
@ -137,6 +143,8 @@ linters-settings:
local-prefixes: github.com/retailcrm/mg-bot-api-client-go
lll:
line-length: 120
maligned:
suggest-new: true
misspell:
locale: US
nestif:
@ -149,8 +157,7 @@ issues:
exclude-rules:
- path: _test\.go
linters:
- dupl
- mnd
- gomnd
- lll
- bodyclose
- errcheck
@ -176,4 +183,7 @@ issues:
severity:
default-severity: error
case-sensitive: false
case-sensitive: false
service:
golangci-lint-version: 1.36.x

View file

@ -14,46 +14,16 @@ import (
"github.com/google/go-querystring/query"
)
type Option func(*MgClient)
// OptionHTTPClient set custom http.Client for MgClient.
func OptionHTTPClient(client *http.Client) func(*MgClient) {
return func(c *MgClient) {
c.httpClient = client
}
}
// OptionLogger sets the provided logger instance into the MgClient.
func OptionLogger(logger BasicLogger) func(*MgClient) {
return func(c *MgClient) {
c.logger = logger
}
}
// OptionDebug enables debug mode for MgClient.
func OptionDebug() func(*MgClient) {
return func(c *MgClient) {
c.Debug = true
}
}
// New initialize client
func New(url string, token string, opts ...Option) *MgClient {
c := &MgClient{
func New(url string, token string) *MgClient {
return &MgClient{
URL: url,
Token: token,
httpClient: &http.Client{Timeout: time.Minute},
}
for _, opt := range opts {
opt(c)
}
return c
}
// WithLogger sets the provided logger instance into the Client.
// Deprecated: Use functional option OptionLogger instead.
func (c *MgClient) WithLogger(logger BasicLogger) *MgClient {
c.logger = logger
return c
@ -457,50 +427,6 @@ 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:
@ -912,57 +838,8 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe
return resp, status, err
}
// UpdateFileMetadata update file metadata
//
// Example:
//
// response, status, err := c.UpdateFileMetadata(UploadFileByUrlRequest{
// ID: "e038aa39-2338-4285-be86-e2a0bb424daa"
// Transcription: "demo transcription",
// })
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n%s", response.ID, status)
func (c *MgClient) UpdateFileMetadata(request UpdateFileMetadataRequest) (UploadFileResponse, int, error) {
var resp UploadFileResponse
outgoing, err := json.Marshal(&request)
if err != nil {
return resp, 0, err
}
data, status, err := c.PutRequest(fmt.Sprintf("/files/%s/meta", request.ID), outgoing)
if err != nil {
return resp, status, err
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
return resp, status, err
}
type wsParams struct {
options []string
}
type WsParams interface {
apply(*wsParams)
}
func (c WsOption) apply(opts *wsParams) {
opts.options = append(opts.options, string(c))
}
// WsMeta let you receive url & headers to open web socket connection
func (c *MgClient) WsMeta(events []string, urlParams ...WsParams) (string, http.Header, error) {
func (c *MgClient) WsMeta(events []string) (string, http.Header, error) {
var url string
if len(events) < 1 {
@ -972,14 +849,6 @@ func (c *MgClient) WsMeta(events []string, urlParams ...WsParams) (string, http.
url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ","))
var params wsParams
for _, param := range urlParams {
param.apply(&params)
}
if len(params.options) > 0 {
url = fmt.Sprintf("%s&options=%s", url, strings.Join(params.options, ","))
}
if url == "" {
err := errors.New("empty WS URL")
return url, nil, err

View file

@ -2,12 +2,8 @@ package v1
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"io"
"log"
"math/rand"
"net/http"
@ -48,12 +44,14 @@ var (
debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
)
func client(opts ...Option) *MgClient {
func client() *MgClient {
c := New(mgURL, mgToken)
if debug != false {
opts = append(opts, OptionDebug())
c.Debug = true
}
return New(mgURL, mgToken, opts...)
return c
}
func TestMgClient_Bots(t *testing.T) {
@ -73,7 +71,7 @@ func TestMgClient_Bots(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data)
for _, bot := range data {
@ -152,7 +150,7 @@ func TestMgClient_Channels(t *testing.T) {
]`)
channels, status, err := c.Channels(ChannelsRequest{Active: 1})
require.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, 200, status)
assert.Len(t, channels, 1)
@ -216,7 +214,7 @@ func TestMgClient_Users(t *testing.T) {
gock.New(mgURL).
Get("/api/bot/v1/users").
Reply(200).
BodyString(`[{"id": 1, "external_id":"1", "username": "Test", "first_name":"Test", "last_name":"Test", "created_at": "2018-01-01T00:00:00.000000Z", "is_active": true, "is_online": true, "is_technical_account": true}]`)
BodyString(`[{"id": 1, "external_id":"1", "username": "Test", "first_name":"Test", "last_name":"Test", "created_at": "2018-01-01T00:00:00.000000Z", "is_active": true, "is_online": true}]`)
req := UsersRequest{Active: 1}
@ -225,19 +223,11 @@ func TestMgClient_Users(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data)
for _, user := range data {
assert.Equal(t, uint64(1), user.ID)
assert.Equal(t, "1", user.ExternalID)
assert.Equal(t, "Test", user.Username)
assert.Equal(t, "Test", user.FirstName)
assert.Equal(t, "Test", user.LastName)
assert.Equal(t, "2018-01-01T00:00:00.000000Z", user.CreatedAt)
assert.True(t, user.IsActive)
assert.True(t, user.IsOnline)
assert.True(t, user.IsTechnicalAccount)
assert.NotEmpty(t, user.FirstName)
}
}
@ -246,44 +236,10 @@ func TestMgClient_Customers(t *testing.T) {
defer gock.Off()
response := `
[
{
"id": 1,
"channel_id": 1,
"created_at":
"2018-01-01T00:00:00.000000Z",
"utm": {
"source": "test"
}
},
{
"id": 2,
"channel_id": 1,
"created_at":
"2018-01-01T00:00:00.000000Z",
"utm": {
"source": null
}
},
{
"id": 3,
"channel_id": 1,
"created_at":
"2018-01-01T00:00:00.000000Z",
"utm": null
},
{
"id": 4,
"channel_id": 1,
"created_at": "2018-01-01T00:00:00.000000Z"
}
]`
gock.New(mgURL).
Get("/api/bot/v1/customers").
Reply(200).
BodyString(response)
BodyString(`[{"id": 1,"channel_id": 1, "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := CustomersRequest{}
@ -292,17 +248,12 @@ func TestMgClient_Customers(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data)
for _, customer := range data {
assert.NotEmpty(t, customer.ChannelId)
}
assert.Equal(t, "test", data[0].Utm.Source)
assert.Equal(t, "", data[1].Utm.Source)
assert.Nil(t, data[2].Utm)
assert.Nil(t, data[3].Utm)
}
func TestMgClient_Chats(t *testing.T) {
@ -313,22 +264,16 @@ func TestMgClient_Chats(t *testing.T) {
gock.New(mgURL).
Get("/api/bot/v1/chats").
Reply(200).
BodyString(`[
{"id": 2,"customer": {"id": 2, "name": "Foo"}, "created_at": "2018-01-01T00:00:00.000000Z"},
{"id": 3,"customer": {"id": 3, "name": "Bar"}, "created_at": "2018-01-02T00:00:00.000000Z"}
]`)
BodyString(`[{"id": 1,"customer": {"id": 1, "name": "Test"}, "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := ChatsRequest{
ChannelType: ChannelTypeTelegram,
SinceID: 1,
}
req := ChatsRequest{ChannelType: ChannelTypeTelegram}
data, status, err := c.Chats(req)
if err != nil {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data)
for _, chat := range data {
@ -353,7 +298,7 @@ func TestMgClient_Members(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
for _, member := range data {
assert.NotEmpty(t, member.ChatID)
@ -370,14 +315,14 @@ func TestMgClient_Dialogs(t *testing.T) {
Reply(200).
BodyString(`[{"id": 1, "chat_id": 1, "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := DialogsRequest{Active: 0, SinceID: 1}
req := DialogsRequest{Active: 0}
data, status, err := c.Dialogs(req)
if err != nil {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data)
for _, dialog := range data {
@ -419,7 +364,7 @@ func TestMgClient_DialogUnassign(t *testing.T) {
resp, status, err := c.DialogUnassign(777)
require.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, int64(111), resp.PreviousResponsible.ID)
@ -481,59 +426,6 @@ 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)
require.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)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
}
func TestMgClient_Messages(t *testing.T) {
c := client()
@ -551,7 +443,7 @@ func TestMgClient_Messages(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data)
for _, message := range data {
@ -559,83 +451,6 @@ func TestMgClient_Messages(t *testing.T) {
}
}
func TestMgClient_MessagesDialog(t *testing.T) {
t.Parallel()
c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/messages").
Reply(200).
BodyString(`[
{
"id": 1,
"time": "2024-11-14T20:48:21Z",
"type": "system",
"scope": "private",
"chat_id": 1,
"is_read": false,
"is_edit": false,
"status": "received",
"dialog": {
"id": 2054
},
"action": "dialog_closed",
"channel_id": 71,
"created_at": "2024-11-14T20:48:21.907566Z",
"updated_at": "2024-11-14T20:48:21.907566Z"
},
{
"id": 2,
"time": "2024-11-14T19:58:42Z",
"type": "text",
"scope": "public",
"chat_id": 1,
"is_read": false,
"is_edit": false,
"status": "failed",
"from": {
"id": 1,
"external_id": "1",
"type": "user",
"avatar": "http://avatars-test.jpeg",
"name": "John Smith",
"first_name": "John",
"last_name": "Smith",
"available": true
},
"dialog": {
"id": 2054
},
"error": {
"code": "malformed_response"
},
"content": "Message from user John Smith",
"quote": null,
"channel_id": 71,
"created_at": "2024-11-14T19:58:42.933025Z",
"updated_at": "2024-11-14T19:58:45.01619Z"
}
]`)
req := MessagesRequest{}
data, status, err := c.Messages(req)
if err != nil {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.Len(t, data, 2)
for _, m := range data {
assert.NotNil(t, m.Message.Dialog)
assert.Equal(t, uint64(2054), m.Message.Dialog.ID)
}
}
func TestMgClient_MessageSendText(t *testing.T) {
c := client()
@ -660,7 +475,7 @@ func TestMgClient_MessageSendText(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data.MessageID)
}
@ -698,7 +513,7 @@ func TestMgClient_MessageSendTextWithSuggestions(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data.MessageID)
}
@ -739,7 +554,7 @@ func TestMgClient_MessageSendProduct(t *testing.T) {
t.Errorf("%v", err)
}
require.NoError(t, err)
assert.NoError(t, err)
t.Logf("%v", msg)
}
@ -799,7 +614,7 @@ func TestMgClient_MessageSendOrder(t *testing.T) {
t.Errorf("%v", err)
}
require.NoError(t, err)
assert.NoError(t, err)
t.Logf("%v", msg)
}
@ -868,7 +683,7 @@ func TestMgClient_Info(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
}
func TestMgClient_Commands(t *testing.T) {
@ -888,7 +703,7 @@ func TestMgClient_Commands(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, data)
for _, command := range data {
@ -916,7 +731,7 @@ func TestMgClient_CommandEditDelete(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
assert.NotEmpty(t, n.ID)
gock.New(mgURL).
@ -929,28 +744,10 @@ func TestMgClient_CommandEditDelete(t *testing.T) {
t.Errorf("%d %v", status, err)
}
require.NoError(t, err)
assert.NoError(t, err)
t.Logf("%v", d)
}
func TestMgClient_WsMeta_With_Options(t *testing.T) {
c := client()
events := []string{"user_updated", "user_join_chat"}
params := []WsParams{WsOptionIncludeMassCommunication}
url, headers, err := c.WsMeta(events, params...)
if err != nil {
t.Errorf("%v", err)
}
resURL := "wss://api.example.com/api/bot/v1/ws?events=user_updated,user_join_chat&options=include_mass_communication"
resToken := c.Token
assert.Equal(t, resURL, url)
assert.Equal(t, resToken, headers["X-Bot-Token"][0])
}
func TestMgClient_WsMeta(t *testing.T) {
c := client()
events := []string{"user_updated", "user_join_chat"}
@ -960,56 +757,23 @@ func TestMgClient_WsMeta(t *testing.T) {
t.Errorf("%v", err)
}
resURL := fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events, ","))
resUrl := fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ","))
resToken := c.Token
assert.Equal(t, resURL, url)
assert.Equal(t, resUrl, url)
assert.Equal(t, resToken, headers["X-Bot-Token"][0])
}
func TestMgClient_UploadFile(t *testing.T) {
c := client()
defer gock.Off()
gock.New("https://via.placeholder.com").
Get("/300").
Reply(http.StatusOK).
SetHeader("Content-Type", "image/jpeg").
Body(func() io.Reader {
res, err := base64.StdEncoding.DecodeString(`
H4sIAAAAAAACA/t/4/8DBgEvN083BkZGBgZGIGT4f5vBmYGVmYWFhZkVSLCysrKx83CwAwE/Nzcn
jyC/kJAgv6CgsJiMuLCIlKigoISihJSsrLy8vLC4koqSnIqMnLwcyBBGoFZ2NnY+Dg4+ORFBETmS
wf8DDIIcDAoMCsyMSgxMgozMgoz/jzDIA93JyggGDFDAyMTMAnQlBycXN1DBVgEGJkZmZiag+4Ee
AMrWAuUZWARZhRQNHdmEAxPZlQpFjBonLuRQdtp4UDTo4gcV46SiJk4uMXEJSSlVNXUNTS0TUzNz
C0srZxdXN3cPT6/gkNCw8IjIqOSU1LT0jMys4pLSsvKKyqrmlta29o7OrkmTp0ydNn3GzFmLFi9Z
umz5ipWrNm3esnXb9h07dx06fOToseMnTp66dPnK1WvXb9y89fDR4ydPnz1/8fLVx0+fv3z99v3H
z18gfzEyMDPCAFZ/CQL9xQSMFhZ2kL8YmcpBCgRZWBUN2YQcA9kTC4WVjBo5RJwmLtx4kFPZOOiD
aFLRRS4xFZOHqh9BXgP7jDiPNZHlM7jHEP66xcDDzAiMPGZBBnuGH/fuKWs3sItefBlWa7FqV+h8
P+01l9b8KnQQ27Labk545NLIL49WZwLl1m322vzyKEPFu6npl7temwAlQ3O1zi8XvQaSXMBtBdcZ
itDYYP//JgDowAia0AIAAA==`)
if err != nil {
t.Errorf("%v", err)
t.FailNow()
return nil
}
unpacker, err := gzip.NewReader(bytes.NewReader(res))
if err != nil {
t.Errorf("%v", err)
t.FailNow()
return nil
}
return unpacker
}())
resp, err := http.Get("https://via.placeholder.com/300")
if err != nil {
t.Errorf("%v", err)
t.FailNow()
}
defer resp.Body.Close()
defer gock.Off()
gock.New(mgURL).
Post("/api/bot/v1/files/upload").
@ -1047,7 +811,7 @@ func TestMgClient_UploadFileByUrl(t *testing.T) {
t.Logf("File %+v is upload", uploadFileResponse.ID)
require.NoError(t, err)
assert.NoError(t, err)
}
func RandStringBytesMaskImprSrc(n int) string {
@ -1087,110 +851,11 @@ func TestMgClient_DebugWithLogger(t *testing.T) {
var buf bytes.Buffer
logger := log.New(&buf, "Custom log prefix ", 0)
c := client(OptionDebug(), OptionLogger(logger))
c := client()
c.Debug = true
c.WithLogger(logger)
c.writeLog("Test log string")
assert.Contains(t, buf.String(), "Custom log prefix Test log string")
}
func TestMgClient_SuccessChatsByCustomerId(t *testing.T) {
defer gock.Off()
customerID := uint64(191140)
gock.New(mgURL).
Path("/api/bot/v1/chats").
MatchParam("customer_id", fmt.Sprintf("%d", customerID)).
MatchHeader("X-Bot-Token", mgToken).
Reply(http.StatusOK).
JSON(getJSONResponseChats())
apiClient := client()
chatsRequest := ChatsRequest{
CustomerID: customerID,
}
resp, statusCode, err := apiClient.Chats(chatsRequest)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, statusCode)
assert.Len(t, resp, 1)
assert.Equal(t, uint64(9000), resp[0].ID)
assert.Equal(t, uint64(8000), resp[0].Channel.ID)
assert.Equal(t, customerID, resp[0].Customer.ID)
assert.Equal(t, "Имя Фамилия", resp[0].Customer.Name)
assert.Equal(t, "Имя", resp[0].Customer.FirstName)
assert.Equal(t, "Фамилия", resp[0].Customer.LastName)
}
func getJSONResponseChats() string {
return `[
{
"id": 9000,
"channel": {
"id": 8000,
"avatar": "",
"transport_id": 555,
"type": "transport",
"settings": {
"status": {
"delivered": "send"
},
"text": {
"creating": "both",
"editing": "both",
"quoting": "both",
"deleting": "receive",
"max_chars_count": 4096
},
"product": {
"creating": "receive",
"editing": "receive"
},
"order": {
"creating": "receive",
"editing": "receive"
},
"image": {
"creating": "both",
"editing": "both",
"quoting": "both",
"deleting": "receive",
"max_items_count": 10
},
"file": {
"creating": "both",
"editing": "both",
"quoting": "both",
"deleting": "receive",
"max_items_count": 1
},
"audio": {
"creating": "both",
"quoting": "both",
"deleting": "receive",
"max_items_count": 1
},
"suggestions": {
"text": "both",
"email": "both",
"phone": "both"
}
},
"name": "@test_bot123",
"is_active": false
},
"customer": {
"id": 191140,
"external_id": "",
"type": "customer",
"avatar": "",
"name": "Имя Фамилия",
"username": "Имя",
"first_name": "Имя",
"last_name": "Фамилия"
},
"last_activity": "2022-10-28T13:17:38+03:00",
"created_at": "2022-10-07T14:00:24.795382Z",
"updated_at": "2022-10-28T12:19:04.834592Z"
}
]`
}

View file

@ -43,8 +43,6 @@ const (
WsEventSettingsUpdated string = "settings_updated"
WsEventChatsDeleted string = "chats_deleted"
WsOptionIncludeMassCommunication WsOption = "include_mass_communication"
ChannelFeatureNone string = "none"
ChannelFeatureReceive string = "receive"
ChannelFeatureSend string = "send"
@ -52,7 +50,6 @@ const (
BotRoleDistributor string = "distributor"
BotRoleResponsible string = "responsible"
BotRoleHidden string = "hidden"
MsgTypeText string = "text"
MsgTypeSystem string = "system"
@ -79,23 +76,6 @@ 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"
WaitingLevelNone = "none"
WaitingLevelWarning = "warning"
WaitingLevelDanger = "danger"
)
// MgClient type
@ -116,7 +96,7 @@ type (
Role string `url:"role,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
Limit int `url:"int,omitempty"`
}
ChannelsRequest struct {
@ -125,7 +105,7 @@ type (
Active uint8 `url:"active,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
Limit int `url:"int,omitempty"`
}
UsersRequest struct {
@ -135,30 +115,24 @@ type (
Active uint8 `url:"active,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
Limit int `url:"int,omitempty"`
}
CustomersRequest struct {
ID uint64 `url:"id,omitempty"`
ChannelID uint64 `url:"channel_id,omitempty" json:"channel_id"`
ChannelType string `url:"channel_type,omitempty" json:"channel_type"`
ExternalID string `url:"external_id,omitempty" json:"external_id"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
ID uint64 `url:"id,omitempty"`
ExternalID string `url:"external_id,omitempty" json:"external_id"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"int,omitempty"`
}
ChatsRequest struct {
ID uint64 `url:"id,omitempty"`
ChannelID uint64 `url:"channel_id,omitempty" json:"channel_id"`
ChannelType string `url:"channel_type,omitempty" json:"channel_type"`
CustomerID uint64 `url:"customer_id,omitempty" json:"customer_id"`
CustomerExternalID string `url:"customer_external_id,omitempty" json:"customer_external_id"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
SinceID int `url:"since_id,omitempty"`
IncludeMassCommunication uint8 `url:"include_mass_communication,omitempty"`
ID uint64 `url:"id,omitempty"`
ChannelID uint64 `url:"channel_id,omitempty" json:"channel_id"`
ChannelType string `url:"channel_type,omitempty" json:"channel_type"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"int,omitempty"`
}
MembersRequest struct {
@ -167,21 +141,19 @@ type (
State string `url:"state,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
Limit int `url:"int,omitempty"`
}
DialogsRequest struct {
ID uint64 `url:"id,omitempty"`
ChatID string `url:"chat_id,omitempty" json:"chat_id"`
UserID string `url:"user_id,omitempty" json:"user_id"`
BotID string `url:"bot_id,omitempty" json:"bot_id"`
Assign uint8 `url:"assign,omitempty"`
Active uint8 `url:"active,omitempty"`
Since string `url:"since,omitempty"`
SinceID int `url:"since_id,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
IncludeMassCommunication uint8 `url:"include_mass_communication,omitempty"`
ID uint64 `url:"id,omitempty"`
ChatID string `url:"chat_id,omitempty" json:"chat_id"`
UserID string `url:"user_id,omitempty" json:"user_id"`
BotID string `url:"bot_id,omitempty" json:"bot_id"`
Assign uint8 `url:"assign,omitempty"`
Active uint8 `url:"active,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"int,omitempty"`
}
DialogAssignRequest struct {
@ -190,40 +162,20 @@ 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"`
DialogID uint64 `url:"dialog_id,omitempty" json:"dialog_id"`
UserID uint64 `url:"user_id,omitempty" json:"user_id"`
CustomerID uint64 `url:"customer_id,omitempty" json:"customer_id"`
BotID uint64 `url:"bot_id,omitempty" json:"bot_id"`
ChannelID uint64 `url:"channel_id,omitempty" json:"channel_id"`
ChannelType string `url:"channel_type,omitempty" json:"channel_type"`
Scope string `url:"scope,omitempty"`
Type string `url:"type,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
IncludeMassCommunication uint8 `url:"include_mass_communication,omitempty"`
ID []int `url:"id,omitempty"`
ChatID uint64 `url:"chat_id,omitempty" json:"chat_id"`
DialogID uint64 `url:"dialog_id,omitempty" json:"dialog_id"`
UserID uint64 `url:"user_id,omitempty" json:"user_id"`
CustomerID uint64 `url:"customer_id,omitempty" json:"customer_id"`
BotID uint64 `url:"bot_id,omitempty" json:"bot_id"`
ChannelID uint64 `url:"channel_id,omitempty" json:"channel_id"`
ChannelType string `url:"channel_type,omitempty" json:"channel_type"`
Scope string `url:"scope,omitempty"`
Type string `url:"type,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"int,omitempty"`
}
MessageSendRequest struct {
@ -263,7 +215,7 @@ type (
Name string `url:"name,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Limit int `url:"limit,omitempty"`
Limit int `url:"int,omitempty"`
}
CommandEditRequest struct {
@ -274,13 +226,6 @@ type (
UploadFileByUrlRequest struct {
Url string `json:"url"`
}
UpdateFileMetadataRequest struct {
ID string `json:"-"`
Transcription string `json:"transcription,omitempty"`
// Current status of transcription process. Available values: "in_progress", "ready", "error"
TranscriptionStatus string `json:"transcription_status,omitempty"`
}
)
// Response types
@ -311,20 +256,17 @@ type (
}
UsersResponseItem struct {
ID uint64 `json:"id"`
ExternalID string `json:"external_id,omitempty"`
Username string `json:"username,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at,omitempty"`
RevokedAt string `json:"revoked_at,omitempty"`
Available bool `json:"available"`
IsOnline bool `json:"is_online"`
Connected bool `json:"connected"`
IsActive bool `json:"is_active"`
IsTechnicalAccount bool `json:"is_technical_account"`
Avatar string `json:"avatar_url,omitempty"`
ID uint64 `json:"id"`
ExternalID string `json:"external_id,omitempty"`
Username string `json:"username,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at,omitempty"`
RevokedAt string `json:"revoked_at,omitempty"`
IsOnline bool `json:"is_online"`
IsActive bool `json:"is_active"`
Avatar string `json:"avatar_url,omitempty"`
}
CustomersResponseItem struct {
@ -343,22 +285,19 @@ type (
Language string `json:"language,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Utm *Utm `json:"utm,omitempty"`
}
ChatResponseItem struct {
ID uint64 `json:"id"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Channel Channel `json:"channel,omitempty"`
Customer UserRef `json:"customer"`
AuthorID uint64 `json:"author_id"`
LastMessage Message `json:"last_message"`
LastUserMessage MessageID `json:"last_user_message"`
LastActivity string `json:"last_activity"`
LastDialog Dialog `json:"last_dialog"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID uint64 `json:"id"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Channel Channel `json:"channel,omitempty"`
Customer UserRef `json:"customer"`
AuthorID uint64 `json:"author_id"`
LastMessage Message `json:"last_message"`
LastActivity string `json:"last_activity"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
MemberResponseItem struct {
@ -383,7 +322,6 @@ type (
IsAssigned bool `json:"is_assigned"`
Responsible Responsible `json:"responsible,omitempty"`
IsActive bool `json:"is_active"`
Utm *Utm `json:"utm,omitempty"`
}
DialogAssignResponse struct {
@ -451,11 +389,6 @@ type (
}
)
// WS options.
type (
WsOption string
)
// Single entity types
type (
Message struct {
@ -471,16 +404,11 @@ type (
From *UserRef `json:"from"`
Product *MessageProduct `json:"product,omitempty"`
Order *MessageOrder `json:"order,omitempty"`
Dialog *MessageDialog `json:"dialog,omitempty"`
*TextMessage
*SystemMessage
*AttachmentList
}
MessageID struct {
ID uint64 `json:"id"`
}
TextMessage struct {
Content string `json:"content"`
Quote *QuoteMessage `json:"quote"`
@ -488,22 +416,16 @@ type (
}
SystemMessage struct {
Action string `json:"action"`
// Deprecated: Use Message.Dialog.ID instead.
Action string `json:"action"`
Dialog *SystemMessageDialog `json:"dialog,omitempty"`
User *UserRef `json:"user,omitempty"`
Responsible *UserRef `json:"responsible,omitempty"`
}
// Deprecated: Use MessageDialog instead.
SystemMessageDialog struct {
ID uint64 `json:"id"`
}
MessageDialog struct {
ID uint64 `json:"id"`
}
QuoteMessage struct {
ID uint64 `json:"id"`
Content string `json:"content"`
@ -517,21 +439,13 @@ type (
}
Attachment struct {
File
Caption string `json:"caption"`
}
File struct {
PreviewURL *string `json:"preview_url,omitempty"`
Height *uint64 `json:"height,omitempty"`
Width *uint64 `json:"width,omitempty"`
Transcription string `json:"transcription,omitempty"`
ID string `json:"id"`
Mime string `json:"type"`
Type string `json:"kind"`
Duration int `json:"duration,omitempty"`
Size uint64 `json:"size"`
ID string `json:"id"`
Mime string `json:"type"`
Caption string `json:"caption"`
Size uint64 `json:"size"`
PreviewURL *string `json:"preview_url,omitempty"`
Height *uint64 `json:"height,omitempty"`
Width *uint64 `json:"width,omitempty"`
}
MessageProduct struct {
@ -597,19 +511,17 @@ type (
}
UserRef struct {
ID uint64 `json:"id"`
ExternalID string `json:"external_id"`
Avatar string `json:"avatar"`
Type string `json:"type"`
Name string `json:"name"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
IsAdmin bool `json:"is_admin"`
Available bool `json:"available"`
IsTechnicalAccount bool `json:"is_technical_account"`
IsSystem bool `json:"is_system"`
ID uint64 `json:"id"`
ExternalID uint64 `json:"external_id"`
Avatar string `json:"avatar"`
Type string `json:"type"`
Name string `json:"name"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
IsAdmin bool `json:"is_admin"`
Available bool `json:"available"`
}
Channel struct {
@ -641,22 +553,15 @@ type (
}
Chat struct {
ID uint64 `json:"id"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Channel *Channel `json:"channel,omitempty"`
Members []Member `json:"members"`
Customer *UserRef `json:"customer"`
AuthorID uint64 `json:"author_id"`
LastMessage *Message `json:"last_message"`
LastUserMessage *MessageID `json:"last_user_message"`
LastActivity string `json:"last_activity"`
}
WaitingChat struct {
Chat
WaitingLevel string `json:"waiting_level"`
ID uint64 `json:"id"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Channel *Channel `json:"channel,omitempty"`
Members []Member `json:"members"`
Customer *UserRef `json:"customer"`
AuthorID uint64 `json:"author_id"`
LastMessage *Message `json:"last_message"`
LastActivity string `json:"last_activity"`
}
Member struct {
@ -673,7 +578,6 @@ type (
Responsible *Responsible `json:"responsible"`
CreatedAt string `json:"created_at"`
ClosedAt *string `json:"closed_at"`
Utm *Utm `json:"utm,omitempty"`
}
FileMeta struct {
@ -685,14 +589,6 @@ type (
ID string `json:"id"`
Caption string `json:"caption"`
}
Utm struct {
Source string `json:"source"`
Medium string `json:"medium"`
Campaign string `json:"campaign"`
Term string `json:"term"`
Content string `json:"content"`
}
)
// Channel settings
@ -778,12 +674,10 @@ type (
Message *Message `json:"message"`
}
// Deprecated: Use WsEventWaitingChatCreatedData instead.
WsEventChatCreatedData struct {
Chat *Chat `json:"chat"`
}
// Deprecated: Use WsEventWaitingChatUpdatedData instead.
WsEventChatUpdatedData struct {
Chat *Chat `json:"chat"`
}
@ -808,7 +702,6 @@ type (
WsEventUserUpdatedData struct {
*UserRef
IsActive bool `json:"is_active"`
}
WsEventCustomerUpdatedData struct {
@ -830,9 +723,8 @@ type (
}
WsEventUserOnlineUpdatedData struct {
User *UserRef `json:"user"`
Online bool `json:"online"`
Connected bool `json:"connected"`
User *UserRef `json:"user"`
Online bool `json:"online"`
}
WsEventChatsDeletedData struct {
@ -842,12 +734,4 @@ type (
WsEventChannelUpdatedData struct {
Channel *ChannelResponseItem `json:"channel"`
}
WsEventWaitingChatCreatedData struct {
Chat *WaitingChat `json:"chat"`
}
WsEventWaitingChatUpdatedData struct {
Chat *WaitingChat `json:"chat"`
}
)