-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
444 lines (372 loc) · 12.3 KB
/
handler.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package line
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/urns"
"github.com/pkg/errors"
)
var (
replySendURL = "https://api.line.me/v2/bot/message/reply"
pushSendURL = "https://api.line.me/v2/bot/message/push"
mediaDataURL = "https://api-data.line.me/v2/bot/message"
maxMsgLength = 2000
maxMsgSend = 5
signatureHeader = "X-Line-Signature"
)
// see https://developers.line.biz/en/reference/messaging-api/#message-objects
var mediaSupport = map[handlers.MediaType]handlers.MediaTypeSupport{
handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxBytes: 10 * 1024 * 1024},
handlers.MediaTypeAudio: {Types: []string{"audio/mp4"}, MaxBytes: 200 * 1024 * 1024},
handlers.MediaTypeVideo: {Types: []string{"video/mp4"}, MaxBytes: 200 * 1024 * 1024},
handlers.MediaTypeApplication: {},
}
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("LN"), "Line")}
}
// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
s.AddHandlerRoute(h, http.MethodPost, "receive", courier.ChannelLogTypeMsgReceive, handlers.JSONPayload(h, h.receiveMessage))
return nil
}
// {
// "events": [
// {
// "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
// "type": "message",
// "timestamp": 1462629479859,
// "source": {
// "type": "user",
// "userId": "U4af4980629..."
// },
// "message": {
// "id": "325708",
// "type": "text",
// "text": "Hello, world"
// }
// },
// {
// "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
// "type": "follow",
// "timestamp": 1462629479859,
// "source": {
// "type": "user",
// "userId": "U4af4980629..."
// }
// }
// ]
// }
type moPayload struct {
Events []struct {
ReplyToken string `json:"replyToken"`
Type string `json:"type"`
Timestamp int64 `json:"timestamp"`
Source struct {
Type string `json:"type"`
UserID string `json:"userId"`
} `json:"source"`
Message struct {
ID string `json:"id"`
Type string `json:"type"`
Text string `json:"text"`
Title string `json:"title"`
Address string `json:"address"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
ContentProvider struct {
Type string `json:"type"`
OriginalContentURL string `json:"originalContentUrl"`
} `json:"contentProvider"`
} `json:"message"`
} `json:"events"`
}
// receiveMessage is our HTTP handler function for incoming messages
func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, payload *moPayload, clog *courier.ChannelLog) ([]courier.Event, error) {
err := h.validateSignature(channel, r)
if err != nil {
return nil, err
}
msgs := []courier.MsgIn{}
for _, lineEvent := range payload.Events {
if lineEvent.ReplyToken == "" || (lineEvent.Source.Type == "" && lineEvent.Source.UserID == "") || (lineEvent.Message.Type == "" && lineEvent.Message.ID == "") {
continue
}
text := ""
mediaURL := ""
lineEventMsgType := lineEvent.Message.Type
if lineEventMsgType == "text" {
text = lineEvent.Message.Text
} else if lineEventMsgType == "audio" || lineEventMsgType == "video" || lineEventMsgType == "image" || lineEventMsgType == "file" {
if lineEvent.Message.ContentProvider.Type == "line" || lineEventMsgType == "file" {
mediaURL = buildMediaURL(lineEvent.Message.ID)
} else if lineEvent.Message.ContentProvider.Type == "external" {
mediaURL = lineEvent.Message.ContentProvider.OriginalContentURL
}
} else if lineEventMsgType == "location" {
mediaURL = fmt.Sprintf("geo:%f,%f", lineEvent.Message.Latitude, lineEvent.Message.Longitude)
text = lineEvent.Message.Title
} else {
continue
}
// create our date from the timestamp (they give us millis, arg is nanos)
date := time.Unix(0, lineEvent.Timestamp*1000000).UTC()
urn, err := urns.NewURNFromParts(urns.LineScheme, lineEvent.Source.UserID, "", "")
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
msg := h.Backend().NewIncomingMsg(channel, urn, text, lineEvent.ReplyToken, clog).WithReceivedOn(date)
if mediaURL != "" {
msg.WithAttachment(mediaURL)
}
msgs = append(msgs, msg)
}
if len(msgs) == 0 {
return nil, handlers.WriteAndLogRequestIgnored(ctx, h, channel, w, r, "ignoring request, no message")
}
return handlers.WriteMsgsAndResponse(ctx, h, msgs, w, r, clog)
}
func buildMediaURL(mediaID string) string {
mediaURL, _ := url.Parse(fmt.Sprintf("%s/%s/content", mediaDataURL, mediaID))
return mediaURL.String()
}
// BuildAttachmentRequest to download media for message attachment with Bearer token set
func (h *handler) BuildAttachmentRequest(ctx context.Context, b courier.Backend, channel courier.Channel, attachmentURL string, clog *courier.ChannelLog) (*http.Request, error) {
token := channel.StringConfigForKey(courier.ConfigAuthToken, "")
if token == "" {
return nil, fmt.Errorf("missing token for LN channel")
}
// set the access token as the authorization header
req, _ := http.NewRequest(http.MethodGet, attachmentURL, nil)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return req, nil
}
var _ courier.AttachmentRequestBuilder = (*handler)(nil)
func (h *handler) validateSignature(channel courier.Channel, r *http.Request) error {
actual := r.Header.Get(signatureHeader)
if actual == "" {
return fmt.Errorf("missing request signature")
}
confSecret := channel.ConfigForKey(courier.ConfigSecret, "")
secret, isStr := confSecret.(string)
if !isStr || secret == "" {
return fmt.Errorf("invalid or missing auth token in config")
}
expected, err := calculateSignature(secret, r)
if err != nil {
return err
}
// compare signatures in way that isn't sensitive to a timing attack
if !hmac.Equal(expected, []byte(actual)) {
return fmt.Errorf("invalid request signature")
}
return nil
}
// see https://developers.line.me/en/docs/messaging-api/reference/#signature-validation
func calculateSignature(secret string, r *http.Request) ([]byte, error) {
defer r.Body.Close()
body, err := io.ReadAll(r.Body)
r.Body = io.NopCloser(bytes.NewBuffer(body))
if err != nil {
return nil, err
}
// hash with SHA256
mac := hmac.New(sha256.New, []byte(secret))
mac.Write(body)
hash := mac.Sum(nil)
// encode with Base64
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(hash)))
base64.StdEncoding.Encode(encoded, hash)
return encoded, nil
}
type mtTextMsg struct {
Type string `json:"type"`
Text string `json:"text"`
QuickReply *mtQuickReply `json:"quickReply,omitempty"`
}
type mtQuickReply struct {
Items []QuickReplyItem `json:"items"`
}
type QuickReplyItem struct {
Type string `json:"type"`
Action struct {
Type string `json:"type"`
Label string `json:"label"`
Text string `json:"text"`
} `json:"action"`
}
type mtImageMsg struct {
Type string `json:"type"`
URL string `json:"originalContentUrl"`
PreviewURL string `json:"previewImageUrl"`
}
type mtVideoMsg struct {
Type string `json:"type"`
URL string `json:"originalContentUrl"`
PreviewURL string `json:"previewImageUrl"`
}
type mtAudioMsg struct {
Type string `json:"type"`
URL string `json:"originalContentUrl"`
Duration int `json:"duration"`
}
type mtPayload struct {
To string `json:"to,omitempty"`
ReplyToken string `json:"replyToken,omitempty"`
Messages json.RawMessage `json:"messages"`
}
type mtResponse struct {
Message string `json:"message"`
}
func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
authToken := msg.Channel().StringConfigForKey(courier.ConfigAuthToken, "")
if authToken == "" {
return courier.ErrChannelConfig
}
// all msg parts in JSON
var jsonMsgs []string
parts := handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)
qrs := msg.QuickReplies()
attachments, err := handlers.ResolveAttachments(ctx, h.Backend(), msg.Attachments(), mediaSupport, false)
if err != nil {
return errors.Wrap(err, "error resolving attachments")
}
// fill all msg parts with attachment parts
for _, attachment := range attachments {
var jsonMsg []byte
var err error
switch attachment.Type {
case handlers.MediaTypeImage:
jsonMsg, err = json.Marshal(mtImageMsg{Type: "image", URL: attachment.Media.URL(), PreviewURL: attachment.Media.URL()})
case handlers.MediaTypeVideo:
jsonMsg, err = json.Marshal(mtVideoMsg{Type: "video", URL: attachment.Media.URL(), PreviewURL: attachment.Thumbnail.URL()})
case handlers.MediaTypeAudio:
jsonMsg, err = json.Marshal(mtAudioMsg{Type: "audio", URL: attachment.Media.URL(), Duration: attachment.Media.Duration()})
default:
jsonMsg, err = json.Marshal(mtTextMsg{Type: "text", Text: attachment.URL})
}
if err == nil {
jsonMsgs = append(jsonMsgs, string(jsonMsg))
} else {
return err
}
}
// fill all msg parts with text parts
for i, part := range parts {
if i < (len(parts) - 1) {
if jsonMsg, err := json.Marshal(mtTextMsg{Type: "text", Text: part}); err == nil {
jsonMsgs = append(jsonMsgs, string(jsonMsg))
}
} else {
mtTextMsg := mtTextMsg{Type: "text", Text: part}
items := make([]QuickReplyItem, len(qrs))
for j, qr := range qrs {
items[j] = QuickReplyItem{Type: "action"}
items[j].Action.Type = "message"
items[j].Action.Label = qr
items[j].Action.Text = qr
}
if len(items) > 0 {
mtTextMsg.QuickReply = &mtQuickReply{Items: items}
}
if jsonMsg, err := json.Marshal(mtTextMsg); err == nil {
jsonMsgs = append(jsonMsgs, string(jsonMsg))
}
}
}
// send msg parts in batches
var batch []string
batchCount := 0
for i, jsonMsg := range jsonMsgs {
batch = append(batch, jsonMsg)
batchCount++
if batchCount == maxMsgSend || (i == len(jsonMsgs)-1) {
req, err := buildSendMsgRequest(authToken, msg.URN().Path(), msg.ResponseToExternalID(), batch)
if err != nil {
return err
}
resp, respBody, err := h.RequestHTTP(req, clog)
if err == nil && resp.StatusCode/100 == 2 {
batch = []string{}
batchCount = 0
continue
}
respPayload := &mtResponse{}
err = json.Unmarshal(respBody, respPayload)
if err != nil {
return courier.ErrResponseUnparseable
}
errMsg := respPayload.Message
if errMsg == "Invalid reply token" {
req, err = buildSendMsgRequest(authToken, msg.URN().Path(), "", batch)
if err != nil {
return err
}
resp, respBody, _ := h.RequestHTTP(req, clog)
respPayload := &mtResponse{}
err = json.Unmarshal(respBody, respPayload)
if err != nil {
return courier.ErrResponseUnparseable
}
if resp.StatusCode/100 != 2 {
return courier.ErrFailedWithReason(strconv.Itoa(resp.StatusCode), respPayload.Message)
}
} else {
return courier.ErrFailedWithReason(strconv.Itoa(resp.StatusCode), respPayload.Message)
}
}
}
return nil
}
func buildSendMsgRequest(authToken, to string, replyToken string, jsonMsgs []string) (*http.Request, error) {
// convert from string slice to bytes JSON
rawJsonMsgs := bytes.Buffer{}
rawJsonMsgs.WriteString("[")
for i, msgJson := range jsonMsgs {
rawJsonMsgs.WriteString(msgJson)
if i < len(jsonMsgs)-1 {
rawJsonMsgs.WriteString(",")
}
}
rawJsonMsgs.WriteString("]")
payload := mtPayload{Messages: rawJsonMsgs.Bytes()}
sendURL := ""
// check if this sending is a response to user
if replyToken != "" {
sendURL = replySendURL
payload.ReplyToken = replyToken
} else {
sendURL = pushSendURL
payload.To = to
}
body := &bytes.Buffer{}
if err := json.NewEncoder(body).Encode(payload); err != nil {
return nil, err
}
// build our request
req, err := http.NewRequest(http.MethodPost, sendURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken))
return req, nil
}