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:
parent
3d84ddaee6
commit
73b06d5de4
22
v1/client.go
22
v1/client.go
@ -912,8 +912,20 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
type wsOptions struct {
|
||||
params []string
|
||||
}
|
||||
|
||||
type WsOption interface {
|
||||
apply(*wsOptions)
|
||||
}
|
||||
|
||||
func (c WsOptionParam) apply(opts *wsOptions) {
|
||||
opts.params = append(opts.params, 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, opts ...WsOption) (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 wsOpts wsOptions
|
||||
for _, opt := range opts {
|
||||
opt.apply(&wsOpts)
|
||||
}
|
||||
if len(wsOpts.params) > 0 {
|
||||
url = fmt.Sprintf("%s&options=%s", url, strings.Join(wsOpts.params, ","))
|
||||
}
|
||||
|
||||
if url == "" {
|
||||
err := errors.New("empty WS URL")
|
||||
return url, nil, err
|
||||
|
@ -846,6 +846,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"}
|
||||
options := []WsOption{WsOptionIncludeMassCommunication}
|
||||
|
||||
url, headers, err := c.WsMeta(events, options...)
|
||||
|
||||
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"}
|
||||
|
@ -43,6 +43,8 @@ const (
|
||||
WsEventSettingsUpdated string = "settings_updated"
|
||||
WsEventChatsDeleted string = "chats_deleted"
|
||||
|
||||
WsOptionIncludeMassCommunication WsOptionParam = "include_mass_communication"
|
||||
|
||||
ChannelFeatureNone string = "none"
|
||||
ChannelFeatureReceive string = "receive"
|
||||
ChannelFeatureSend string = "send"
|
||||
@ -434,6 +436,11 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// WS options
|
||||
type (
|
||||
WsOptionParam string
|
||||
)
|
||||
|
||||
// Single entity types
|
||||
type (
|
||||
Message struct {
|
||||
|
Loading…
Reference in New Issue
Block a user