-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineEventHandler.go
82 lines (68 loc) · 2.3 KB
/
lineEventHandler.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
package main
import (
// logger "log"
"regexp"
// "strings"
"time"
"github.com/line/line-bot-sdk-go/linebot"
)
// LineBotEventHandler ...
type LineBotEventHandler struct {
botClient *linebot.Client
}
// SetLineBotClient to assign linebot client handler
func (s *LineBotEventHandler) SetLineBotClient(bc *linebot.Client) {
s.botClient = bc
}
func (s *LineBotEventHandler) matchString(pattern, text string) (result bool) {
result, _ = regexp.MatchString(pattern, text)
return
}
// OnTextMessage ...
func (s *LineBotEventHandler) OnTextMessage(from, text string) {
log.Printf("Received text \"%s\" from %s", text, from)
subj := "aitc.text.service"
msg, err := nc.Request(subj, []byte(text), 1*time.Second)
if err != nil {
log.Fatalf("Error in Request: %v\n", err)
}
strResult := string(msg.Data)
if len(strResult) != 0 {
if _, err = s.botClient.ReplyMessage(from, linebot.NewTextMessage(strResult)).Do(); err != nil {
log.Printf("Send message \"%s\" to \"%s\" fail, error:%d", strResult, from, err)
}
} else {
}
}
/*
// OnImageMessage ...
func (s *LineBotEventHandler) OnImageMessage(from string, rc *linebot.ReceivedContent) {
s.botClient.SendText([]string{from}, "收到一張照片")
log.Print("=== Received Image ===")
}
// OnVideoMessage ...
func (s *LineBotEventHandler) OnVideoMessage(from string, rc *linebot.ReceivedContent) {
s.botClient.SendText([]string{from}, "收到一段錄影")
log.Print("=== Received Video ===")
}
// OnAudioMessage ...
func (s *LineBotEventHandler) OnAudioMessage(from string, duration int) {
s.botClient.SendText([]string{from}, "收到一段錄音")
log.Print("=== Received Audio ===")
}
// OnLocationMessage ...
func (s *LineBotEventHandler) OnLocationMessage(from, title, address string, latitude, longitude float64) {
s.botClient.SendText([]string{from}, "收到地點資訊")
log.Print("=== Received Location ===")
}
// OnStickerMessage ...
func (s *LineBotEventHandler) OnStickerMessage(from string, stickerPackageID, stickerID, stickerVersion int) {
s.botClient.SendText([]string{from}, "收到一張貼紙")
log.Print("=== Received Sticker ===")
}
// OnContactMessage ...
func (s *LineBotEventHandler) OnContactMessage(from, MID, displayName string) {
s.botClient.SendText([]string{from}, "收到聯絡人資料")
log.Print("=== Received Contact ===")
}
*/