forked from liujianping/wechat
/
app.go
294 lines (256 loc) · 6.36 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
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
package wechat
import (
"fmt"
"time"
"encoding/xml"
"io/ioutil"
"github.com/astaxie/beego/config"
"net/http"
"os"
"path/filepath"
"sync"
"errors"
"github.com/liujianping/wechat/entry"
)
type CallbackInterface interface {
Initialize(app *WeChatApp, api *ApiClient)
MsgText(txt *entry.TextRequest, back chan interface{})
MsgImage(img *entry.ImageRequest, back chan interface{})
MsgVoice(voice *entry.VoiceRequest, back chan interface{})
MsgVideo(video *entry.VideoRequest, back chan interface{})
MsgLink(link *entry.LinkRequest, back chan interface{})
Location(location *entry.LocationRequest, back chan interface{})
EventSubscribe(appoid string, oid string, back chan interface{})
EventUnsubscribe(appoid string, oid string, back chan interface{})
EventMenu(appoid string, oid string, key string, back chan interface{})
}
type Handle func (app *WeChatApp, data []byte, back chan []byte)
type WeChatApp struct{
AppHost string
AppPort int
AppURI string
AppToken string
AppId string
AppSecret string
AppPath string
Config config.ConfigContainer
menu *entry.Menu
cb CallbackInterface
handle Handle
api *ApiClient
once sync.Once
}
func NewWeChatApp() *WeChatApp {
app := new(WeChatApp)
app.AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
os.Chdir(app.AppPath)
app.AppHost = ""
app.AppPort = 80
app.AppURI = "/"
app.AppToken = ""
app.AppId = ""
app.AppSecret = ""
return app
}
func (app *WeChatApp) SetConfig(adapter, file string) error {
var err error
app.Config, err = config.NewConfig(adapter, file)
if err != nil {
return err
} else {
app.AppHost = app.Config.String("AppHost")
if v, err := app.Config.Int("AppPort"); err == nil {
app.AppPort = v
}
if v := app.Config.String("AppURI"); v != "" {
app.AppURI = v
}
if v := app.Config.String("AppToken"); v != "" {
app.AppToken = v
}
if v := app.Config.String("AppId"); v != "" {
app.AppId = v
}
if v := app.Config.String("AppSecret"); v != "" {
app.AppSecret = v
}
}
return nil
}
func (app *WeChatApp) SetMenu(menu *entry.Menu){
app.menu = menu
}
func (app *WeChatApp) SetCallback(callback CallbackInterface) {
app.cb = callback
}
func (app *WeChatApp) SetHandle(handle Handle) {
app.handle = handle
}
func (app *WeChatApp) Run() {
defer func(){
if x := recover(); x != nil {
fmt.Println("wechat : app panic for <", x, ">")
}
}()
if err := app.initialize(); err != nil {
panic(err)
}
http.HandleFunc(app.AppURI, app.uri)
http.ListenAndServe(fmt.Sprintf("%s:%d", app.AppHost, app.AppPort), nil)
}
func (app *WeChatApp) initialize() error{
if app.AppId == "" || app.AppToken == "" || app.AppSecret == "" {
return errors.New("wechat: app id or token or secret not setting!")
}
if app.cb == nil && app.handle == nil {
return errors.New("wechat: handle & callback both unset")
}
app.api = NewApiClient(app.AppToken, app.AppId, app.AppSecret)
if app.cb != nil {
app.cb.Initialize(app, app.api)
}
return nil
}
func (app *WeChatApp) buildMenu(){
if app.menu != nil {
if err := app.api.RemoveMenu(); err != nil {
panic(err)
}
if err := app.api.CreateMenu(app.menu); err != nil {
panic(err)
}
}
}
func (app *WeChatApp) uri(wr http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
signature := req.FormValue("signature")
timestamp := req.FormValue("timestamp")
nonce := req.FormValue("nonce")
echostr := req.FormValue("echostr")
if app.api.Signature(signature, timestamp, nonce) == true {
wr.Write([]byte(echostr))
app.once.Do(app.buildMenu)
} else {
wr.Write([]byte(""))
}
} else {
if app.handle != nil {
}
if err := app.execute(wr, req); err != nil {
Warn("wechat:", err)
}
}
}
func (app *WeChatApp) execute(wr http.ResponseWriter, req *http.Request) error {
data, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
Debug("wechat: data \n", string(data))
raw := make(chan []byte)
defer close(raw)
ch := make(chan interface{})
defer close(ch)
timeout := make(chan bool, 1)
defer close(timeout)
go func(c chan bool) {
defer func(){
if x := recover(); x != nil {
Debug("wechat: ", x)
}
}()
time.Sleep(3e9) // 等待3秒钟
c <- true
}(timeout)
if app.handle != nil {
go app.handle(app, data, raw)
}
if app.cb != nil {
request := &entry.Request{}
err = xml.Unmarshal(data, request)
if err != nil {
return err
}
event := request.Event
msgType := request.MsgType
if "event" == msgType {
//! event
switch (event){
case "subscribe":
go app.cb.EventSubscribe(request.ToUserName, request.FromUserName, ch)
case "unsubscribe":
go app.cb.EventUnsubscribe(request.ToUserName, request.FromUserName, ch)
case "CLICK":
go app.cb.EventMenu(request.ToUserName, request.FromUserName, request.EventKey, ch)
case "LOCATION":
location := &entry.LocationRequest{}
err = xml.Unmarshal(data, location)
if err != nil {
return err
}
go app.cb.Location(location, ch)
default:
return errors.New("unknown event ")
}
} else {
//! other msg
switch (msgType){
case "text":
text := &entry.TextRequest{}
err = xml.Unmarshal(data, text)
if err != nil {
return err
}
go app.cb.MsgText(text, ch)
case "image":
image := &entry.ImageRequest{}
err = xml.Unmarshal(data, image)
if err != nil {
return err
}
go app.cb.MsgImage(image, ch)
case "voice":
voice := &entry.VoiceRequest{}
err = xml.Unmarshal(data, voice)
if err != nil {
return err
}
go app.cb.MsgVoice(voice, ch)
case "video":
video := &entry.VideoRequest{}
err = xml.Unmarshal(data, video)
if err != nil {
return err
}
go app.cb.MsgVideo(video, ch)
case "location":
location := &entry.LocationRequest{}
err = xml.Unmarshal(data, location)
if err != nil {
return err
}
go app.cb.Location(location, ch)
case "link":
link := &entry.LinkRequest{}
err = xml.Unmarshal(data, link)
if err != nil {
return err
}
go app.cb.MsgLink(link, ch)
}
}
}
select{
case r := <-raw:
Debug("wechat: get response \n", string(r))
wr.Write(r)
case b := <-ch:
response,_ := xml.Marshal(b)
Debug("wechat: get response \n", string(response))
wr.Write(response)
case <-timeout:
Warn("wechat: timeout for null response")
wr.Write([]byte(""))
}
return nil
}