mirror of
https://github.com/retailcrm/mg-bot-api-client-go.git
synced 2024-11-21 20:36:05 +03:00
Add websocket options
This commit is contained in:
commit
074f787d0f
22
v1/client.go
22
v1/client.go
@ -912,8 +912,20 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe
|
|||||||
return resp, status, err
|
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
|
// WsMeta let you receive url & headers to open web socket connection
|
||||||
func (c *MgClient) WsMeta(events []string) (string, http.Header, error) {
|
func (c *MgClient) WsMeta(events []string, urlParams ...WsParams) (string, http.Header, error) {
|
||||||
var url string
|
var url string
|
||||||
|
|
||||||
if len(events) < 1 {
|
if len(events) < 1 {
|
||||||
@ -923,6 +935,14 @@ func (c *MgClient) WsMeta(events []string) (string, http.Header, error) {
|
|||||||
|
|
||||||
url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ","))
|
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(¶ms)
|
||||||
|
}
|
||||||
|
if len(params.options) > 0 {
|
||||||
|
url = fmt.Sprintf("%s&options=%s", url, strings.Join(params.options, ","))
|
||||||
|
}
|
||||||
|
|
||||||
if url == "" {
|
if url == "" {
|
||||||
err := errors.New("empty WS URL")
|
err := errors.New("empty WS URL")
|
||||||
return url, nil, err
|
return url, nil, err
|
||||||
|
@ -852,6 +852,24 @@ func TestMgClient_CommandEditDelete(t *testing.T) {
|
|||||||
t.Logf("%v", d)
|
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) {
|
func TestMgClient_WsMeta(t *testing.T) {
|
||||||
c := client()
|
c := client()
|
||||||
events := []string{"user_updated", "user_join_chat"}
|
events := []string{"user_updated", "user_join_chat"}
|
||||||
@ -861,10 +879,10 @@ func TestMgClient_WsMeta(t *testing.T) {
|
|||||||
t.Errorf("%v", err)
|
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
|
resToken := c.Token
|
||||||
|
|
||||||
assert.Equal(t, resUrl, url)
|
assert.Equal(t, resURL, url)
|
||||||
assert.Equal(t, resToken, headers["X-Bot-Token"][0])
|
assert.Equal(t, resToken, headers["X-Bot-Token"][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ const (
|
|||||||
WsEventSettingsUpdated string = "settings_updated"
|
WsEventSettingsUpdated string = "settings_updated"
|
||||||
WsEventChatsDeleted string = "chats_deleted"
|
WsEventChatsDeleted string = "chats_deleted"
|
||||||
|
|
||||||
|
WsOptionIncludeMassCommunication WsOption = "include_mass_communication"
|
||||||
|
|
||||||
ChannelFeatureNone string = "none"
|
ChannelFeatureNone string = "none"
|
||||||
ChannelFeatureReceive string = "receive"
|
ChannelFeatureReceive string = "receive"
|
||||||
ChannelFeatureSend string = "send"
|
ChannelFeatureSend string = "send"
|
||||||
@ -435,6 +437,11 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WS options
|
||||||
|
type (
|
||||||
|
WsOption string
|
||||||
|
)
|
||||||
|
|
||||||
// Single entity types
|
// Single entity types
|
||||||
type (
|
type (
|
||||||
Message struct {
|
Message struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user