-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtuling.go
72 lines (57 loc) · 1.41 KB
/
tuling.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
package main
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"github.com/KevinGong2013/wechat"
)
type tuling struct {
key string
bot *wechat.WeChat
}
func newTuling(key string, bot *wechat.WeChat) *tuling {
return &tuling{key, bot}
}
func (t *tuling) autoReplay(data wechat.EventMsgData) {
// if data.IsSendedByMySelf {
// return
// }
// replay, err := t.response(data.Content, data.FromUserName, data.FromGGID)
// if err != nil {
// logger.Error(err)
// t.bot.SendTextMsg(`你接着说 ... `, data.FromUserName)
// } else {
// t.bot.SendTextMsg(replay, data.FromUserName)
// }
}
func (t *tuling) response(msg, to, userid string) (string, error) {
values := url.Values{}
values.Add(`key`, t.key)
values.Add(`info`, msg)
values.Add(`userid`, userid)
resp, err := http.PostForm(`http://www.tuling123.com/openapi/api`, values)
if err != nil {
return ``, err
}
reader := resp.Body
defer resp.Body.Close()
result := make(map[string]interface{})
err = json.NewDecoder(reader).Decode(&result)
if err != nil {
return ``, err
}
code := result[`code`].(float64)
if code == 100000 {
text := result[`text`].(string)
return text, nil
} else if code == 200000 {
text := result[`text`].(string)
url := result[`url`].(string)
return text + `
` + url, nil
}
logger.Errorf(`info: [%s], userid: [%s]`, msg, userid)
logger.Error(result)
return ``, errors.New(`tuling api unkonw error`)
}