-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.go
211 lines (167 loc) · 6.11 KB
/
bot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package sendbird
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// BotService is an interface for interfacing with the Bot
// endpoints of the Sendbird API
type BotService interface {
Create(params *BotRequest) (*Bot, *Response, error)
SendMessage(botUserId string, params *BotMessageRequest) (*BotMessage, *Response, error)
List() ([]Bot, *Response, error)
Get(botUserId string) (*Bot, *Response, error)
Update(botUserId string, params *BotUpdateRequest) (*Bot, *Response, error)
Delete(botUserId string) (*BotUserId, *Response, error)
Handler(rw http.ResponseWriter, req *http.Request)
}
type MessageReceivedHandler func(*BotCallback)
// BotServiceOp handles communication with the Bot related methods of
// the Sendbird API.
type BotServiceOp struct {
client *SendbirdClient
MessageReceived MessageReceivedHandler
}
var _ BotService = &BotServiceOp{}
type BotRequest struct {
RequestDefaultsAPIV2
BotUserId string `json:"bot_userid"`
BotNickname string `json:"bot_nickname"`
BotCallbackUrl string `json:"bot_callback_url"`
IsPrivacyMode bool `json:"is_privacy_mode"`
}
type BotUpdateRequest struct {
RequestDefaultsAPIV2
BotNickname string `json:"bot_nickname"`
BotCallbackUrl string `json:"bot_callback_url"`
IsPrivacyMode bool `json:"is_privacy_mode"`
}
type BotMessageRequest struct {
RequestDefaultsAPIV2
Message string `json:"message"`
Data string `json:"data"`
ChannelUrl string `json:"channel_url"`
}
type BotUserId struct {
BotUserId string `json:"bot_userid"`
}
type Bot struct {
BotToken string `json:"bot_token"`
BotUserId string `json:"bot_userid"`
BotNickname string `json:"bot_nickname"`
BotCallbackUrl string `json:"bot_callback_url"`
IsPrivacyMode bool `json:"is_privacy_mode"`
}
type BotMessage struct {
BotUserId string `json:"bot_userid"`
Message string `json:"message"`
Data string `json:"data"`
ChannelUrl string `json:"channel_url"`
}
type BotCallback struct {
BotUserId string `json:"bot_userid"` // Recipient bot's username
Category string `json:"bot_message_notification"` // Type of bot notification. For now, there's only one type available. "bot_message_notification"
Timestamp int64 `json:"ts"` // UNIX timestamp of this message. You could use this field to sort messages sent to your bot.
BotToken string `json:"bot_token"` // This is a secret token return to you when you create your bot. You can use the bot_token to verify that a callback is sent from SendBird.
BotNickname string `json:"bot_nickname"` // Recipient bot's nickname
SenderUsername string `json:"sender_username"` // Sender's username. This is equivalent to id field in Server API.
SenderNickname string `json:"sender_nickname"` // Sender's nickname
Message string `json:"message"` // Message sent from a sender
Data string `json:"data"` // Additional data field sent from a sender
Mentioned []string `json:"mentioned"` // A list of usernames mentioned in this message
ChannelType string `json:"channel_type"` // Type of channel. Two types are available. 'messaging' for 1on1 channel, 'group_messaging' for group messaging channel.
ChannelURl string `json:"channel_url"` // Unique url of each channel
}
// Create a bot
func (s *BotServiceOp) Create(params *BotRequest) (*Bot, *Response, error) {
path := "/v2/bots"
params.PopulateApiV2Token(s.client)
req, err := s.client.NewRequest("POST", path, *params)
bot := new(Bot)
resp, err := s.client.Do(req, bot)
if err != nil {
return nil, resp, err
}
return bot, resp, nil
}
// Send Message
func (s *BotServiceOp) SendMessage(botUserId string, params *BotMessageRequest) (*BotMessage, *Response, error) {
path := fmt.Sprintf("v2/bots/%s/send", botUserId)
params.PopulateApiV2Token(s.client)
req, err := s.client.NewRequest("POST", path, *params)
message := new(BotMessage)
resp, err := s.client.Do(req, message)
if err != nil {
return nil, resp, err
}
return message, resp, nil
}
// Get a list of bots in your application
func (s *BotServiceOp) List() ([]Bot, *Response, error) {
path := fmt.Sprintf("v2/bots?api_token=%s", s.client.ApiToken)
req, err := s.client.NewRequest("GET", path, nil)
bots := []Bot{}
resp, err := s.client.Do(req, &bots)
if err != nil {
return nil, resp, err
}
return bots, resp, nil
}
// Retrieve a bot
func (s *BotServiceOp) Get(botUserId string) (*Bot, *Response, error) {
path := fmt.Sprintf("v2/bots/%s?api_token=%s", botUserId, s.client.ApiToken)
req, err := s.client.NewRequest("GET", path, nil)
bot := new(Bot)
resp, err := s.client.Do(req, bot)
if err != nil {
return nil, resp, err
}
return bot, resp, nil
}
// Update a bot
func (s *BotServiceOp) Update(botUserId string, params *BotUpdateRequest) (*Bot, *Response, error) {
path := fmt.Sprintf("v2/bots/%s", botUserId)
params.PopulateApiV2Token(s.client)
req, err := s.client.NewRequest("POST", path, params)
bot := new(Bot)
resp, err := s.client.Do(req, bot)
if err != nil {
return nil, resp, err
}
return bot, resp, nil
}
// Delete a bot
func (s *BotServiceOp) Delete(botUserId string) (*BotUserId, *Response, error) {
params := RequestDefaultsAPIV2{}
params.PopulateApiV2Token(s.client)
path := fmt.Sprintf("v2/bots/%s", botUserId)
req, err := s.client.NewRequest("DELETE", path, params)
bot := new(BotUserId)
resp, err := s.client.Do(req, bot)
if err != nil {
return nil, resp, err
}
return bot, resp, nil
}
func (s *BotServiceOp) Handler(rw http.ResponseWriter, req *http.Request) {
read, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatal(err)
rw.WriteHeader(http.StatusBadRequest)
return
}
//TODO: Message integrity check here
callbackData := new(BotCallback)
err = json.Unmarshal(read, callbackData)
if err != nil {
log.Fatal("Couldn't parse sendbird json:" + err.Error())
rw.WriteHeader(http.StatusBadRequest)
return
}
if s.MessageReceived != nil {
go s.MessageReceived(callbackData)
}
rw.WriteHeader(http.StatusOK)
}