diff --git a/v1/client.go b/v1/client.go index afb1860..c4a352e 100644 --- a/v1/client.go +++ b/v1/client.go @@ -912,8 +912,20 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe 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) (string, http.Header, error) { +func (c *MgClient) WsMeta(events []string, urlParams ...WsParams) (string, http.Header, error) { var url string 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[:], ",")) + 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 == "" { err := errors.New("empty WS URL") return url, nil, err diff --git a/v1/client_test.go b/v1/client_test.go index fbddc55..72eabc2 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -852,6 +852,24 @@ func TestMgClient_CommandEditDelete(t *testing.T) { 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"} @@ -861,10 +879,10 @@ 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]) } diff --git a/v1/types.go b/v1/types.go index 2d43e5f..5827457 100644 --- a/v1/types.go +++ b/v1/types.go @@ -43,6 +43,8 @@ const ( WsEventSettingsUpdated string = "settings_updated" WsEventChatsDeleted string = "chats_deleted" + WsOptionIncludeMassCommunication WsOption = "include_mass_communication" + ChannelFeatureNone string = "none" ChannelFeatureReceive string = "receive" ChannelFeatureSend string = "send" @@ -435,6 +437,11 @@ type ( } ) +// WS options +type ( + WsOption string +) + // Single entity types type ( Message struct {