mirror of
https://github.com/retailcrm/mg-bot-api-client-go.git
synced 2024-11-24 05:46:04 +03:00
commit
c26accb28f
58
README.md
58
README.md
@ -40,3 +40,61 @@ func main() {
|
||||
fmt.Printf("%v", data.MessageID)
|
||||
}
|
||||
```
|
||||
|
||||
## Websocket Example
|
||||
|
||||
```golang
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/retailcrm/mg-bot-api-client-go/v1"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
|
||||
|
||||
url, headers, err := client.WsMeta([]string{"message_new"})
|
||||
if err != nil {
|
||||
log.Fatal("wsMeta:", err)
|
||||
}
|
||||
|
||||
wsConn, _, err := websocket.DefaultDialer.Dial(url, headers)
|
||||
if err != nil {
|
||||
log.Fatal("dial:", err)
|
||||
}
|
||||
|
||||
for {
|
||||
var wsEvent v1.WsEvent
|
||||
err = wsConn.ReadJSON(&wsEvent)
|
||||
if err != nil {
|
||||
log.Fatal("ReadJSON:", err)
|
||||
}
|
||||
|
||||
var eventData v1.WsEventMessageNewData
|
||||
err = json.Unmarshal(wsEvent.Data, &eventData)
|
||||
if err != nil {
|
||||
log.Fatal("Unmarshal:", err)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(eventData.Message.Content, "Hello") {
|
||||
continue
|
||||
}
|
||||
|
||||
message := v1.MessageSendRequest{
|
||||
Scope: "public",
|
||||
Content: "Bonjour!",
|
||||
ChatID: eventData.Message.ChatID,
|
||||
}
|
||||
|
||||
_, status, err := client.MessageSend(message)
|
||||
if err != nil {
|
||||
log.Fatalf("%d %v", status, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
@ -669,7 +669,7 @@ func (c *MgClient) WsMeta(events []string) (string, http.Header, error) {
|
||||
return url, nil, err
|
||||
}
|
||||
|
||||
url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "ws", 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[:], ","))
|
||||
|
||||
if url == "" {
|
||||
err := errors.New("empty WS URL")
|
||||
|
@ -326,7 +326,7 @@ func TestMgClient_WsMeta(t *testing.T) {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
resUrl := fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "ws", 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)
|
||||
|
85
v1/types.go
85
v1/types.go
@ -1,6 +1,7 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -288,7 +289,7 @@ type (
|
||||
Type string `json:"type"`
|
||||
Meta EventMeta `json:"meta"`
|
||||
AppID uint `json:"app_id"`
|
||||
Data interface{} `json:"data"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
EventMeta struct {
|
||||
@ -370,6 +371,33 @@ type (
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
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"`
|
||||
LastActivity string `json:"last_activity"`
|
||||
}
|
||||
|
||||
Member struct {
|
||||
IsAuthor bool `json:"is_author"`
|
||||
State string `json:"state"`
|
||||
User *UserRef `json:"user"`
|
||||
}
|
||||
|
||||
Dialog struct {
|
||||
ID uint64 `json:"id"`
|
||||
BeginMessageID *uint64 `json:"begin_message_id"`
|
||||
EndingMessageID *uint64 `json:"ending_message_id"`
|
||||
Chat *Chat `json:"chat"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
ClosedAt *string `json:"closed_at"`
|
||||
}
|
||||
)
|
||||
|
||||
// Channel settings
|
||||
@ -392,3 +420,58 @@ type (
|
||||
Text ChannelSettingsText `json:"text"`
|
||||
}
|
||||
)
|
||||
|
||||
// Events
|
||||
type (
|
||||
WsEventMessageNewData struct {
|
||||
Message *Message `json:"message"`
|
||||
}
|
||||
|
||||
WsEventMessageUpdatedData struct {
|
||||
Message *Message `json:"message"`
|
||||
}
|
||||
|
||||
WsEventMessageDeletedData struct {
|
||||
Message *Message `json:"message"`
|
||||
}
|
||||
|
||||
WsEventChatCreatedData struct {
|
||||
Chat *Chat `json:"chat"`
|
||||
}
|
||||
|
||||
WsEventChatUpdatedData struct {
|
||||
Chat *Chat `json:"chat"`
|
||||
}
|
||||
|
||||
WsEventDialogOpenedData struct {
|
||||
Dialog *Dialog `json:"dialog"`
|
||||
}
|
||||
|
||||
WsEventDialogClosedData struct {
|
||||
Dialog *Dialog `json:"dialog"`
|
||||
}
|
||||
|
||||
WsEventUserLeaveData struct {
|
||||
Reason string `json:"reason"`
|
||||
Chat struct {
|
||||
ID uint64 `json:"id"`
|
||||
} `json:"chat"`
|
||||
User struct {
|
||||
ID uint64 `json:"id"`
|
||||
} `json:"user"`
|
||||
}
|
||||
|
||||
WsEventUserUpdatedData struct {
|
||||
*UserRef
|
||||
}
|
||||
|
||||
WsEventDialogAssignData struct {
|
||||
Dialog *Dialog `json:"dialog"`
|
||||
Chat *Chat `json:"chat"`
|
||||
}
|
||||
|
||||
EventUserJoinedChatData struct {
|
||||
Chat *Chat `json:"chat"`
|
||||
User *UserRef `json:"user"`
|
||||
}
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user