-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
125 lines (104 loc) · 2.75 KB
/
app.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
package main
import (
"encoding/base64"
"encoding/json"
"net/http"
"net/url"
"os"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/taskqueue"
"google.golang.org/appengine/urlfetch"
"github.com/joho/godotenv"
"github.com/line/line-bot-sdk-go/linebot"
"github.com/line/line-bot-sdk-go/linebot/httphandler"
)
var botHandler *httphandler.WebhookHandler
func init() {
err := godotenv.Load("line.env")
if err != nil {
panic(err)
}
botHandler, err = httphandler.New(
os.Getenv("LINE_BOT_CHANNEL_SECRET"),
os.Getenv("LINE_BOT_CHANNEL_TOKEN"),
)
botHandler.HandleEvents(handleCallback)
http.Handle("/callback", botHandler)
http.HandleFunc("/task", handleTask)
}
// Recived Webhook
func handleCallback(evs []*linebot.Event, r *http.Request) {
c := newContext(r)
ts := make([]*taskqueue.Task, len(evs))
for i, e := range evs {
j, err := json.Marshal(e)
if err != nil {
errorf(c, "json.Marshal: %v", err)
return
}
data := base64.StdEncoding.EncodeToString(j)
t := taskqueue.NewPOSTTask("/task", url.Values{"data": {data}})
ts[i] = t
}
taskqueue.AddMulti(c, ts, "")
}
// Make Messages
func handleTask(w http.ResponseWriter, r *http.Request) {
c := newContext(r)
data := r.FormValue("data")
if data == "" {
errorf(c, "No data")
return
}
j, err := base64.StdEncoding.DecodeString(data)
if err != nil {
errorf(c, "base64 DecodeString: %v", err)
return
}
e := new(linebot.Event)
err = json.Unmarshal(j, e)
if err != nil {
errorf(c, "json.Unmarshal: %v", err)
return
}
bot, err := botHandler.NewClient(linebot.WithHTTPClient(urlfetch.Client(c)))
if err != nil {
errorf(c, "newLINEBot: %v", err)
return
}
prof := bot.GetProfile(e.Source.UserID)
p, err := prof.WithContext(c).Do()
if err != nil {
errorf(c, "GetProfile: %v", err)
return
}
bot.PushMessage(e.Source.UserID)
// for Mayu
// msg := "おはようございます。" + p.DisplayName + "さん。"
// switch rand.Intn(4) {
// case 0:
// msg = "おはようございます。" + p.DisplayName + "さん。"
// case 1:
// msg = p.StatusMessage + "ってどういう意味なんですか?"
// case 2:
// msg = "あなたのそばにずっと居ますよ💝"
// case 3:
// msg = "Pさん…💝" + "\n" + p.PictureURL
// }
// for Example
msg := "おはようございます。" + p.DisplayName + "さん。"
m := linebot.NewTextMessage(msg)
if _, err = bot.ReplyMessage(e.ReplyToken, m).WithContext(c).Do(); err != nil {
errorf(c, "ReplayMessage: %v", err)
return
}
w.WriteHeader(200)
}
func newContext(r *http.Request) context.Context {
return appengine.NewContext(r)
}
func errorf(c context.Context, format string, args ...interface{}) {
log.Errorf(c, format, args...)
}