-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
124 lines (113 loc) Β· 2.79 KB
/
api.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
package main
import (
"fmt"
"net/url"
"net/http"
"log"
"time"
"strings"
ajson "github.com/biezhi/agon/json"
"encoding/json"
)
// tuling api key
var tlKey string
// init config
func InitConf() error {
configMap, err := ajson.LoadJson("config.json")
if err != nil {
fmt.Println("readFile: ", err.Error())
return err
}
tlKey = configMap["tlKey"]
return nil
}
//get reply from tlAI
func tlAI(info string) string {
tuLingURL := fmt.Sprintf("http://www.tuling123.com/openapi/api?key=%s&info=%s", tlKey, url.QueryEscape(info))
resp, err := http.Get(tuLingURL)
if err != nil {
log.Println(err)
return ""
}
defer resp.Body.Close()
reply := new(tlReply)
decoder := json.NewDecoder(resp.Body) //decode reply from response body
decoder.Decode(reply)
return reply.Text
}
type tlReply struct {
code int
Text string `json:"text"`
}
//get reply from qinAI
func qinAI(info string) string {
//info = strings.Replace(info, " ", "+", -1)
qinURL := fmt.Sprintf("http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s", url.QueryEscape(info))
timeout := time.Duration(2 * time.Second)
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(qinURL)
//resp, err := http.Get(qinURL)
if err != nil {
return ""
}
defer resp.Body.Close()
reply := new(qinReply)
decoder := json.NewDecoder(resp.Body)
decoder.Decode(reply)
log.Printf("reply from qingyunke machine: %s", reply.Content)
wl := []string{"{br}", "\n", "θ²θ²", "Jarvis"}
srp := strings.NewReplacer(wl...)
ret := srp.Replace(reply.Content)
return ret
}
type qinReply struct {
result int
Content string `json:"content"`
}
//get reply from mitAI
func mitAI(info string) string {
//todo fix mitAI
return tlAI(info)
mitURL := "https://demo.pandorabots.com/atalk/mitsuku/mitsukudemo"
resp, err := http.PostForm(mitURL, url.Values{"input": {info}, "user_key": {"pb3568993377180953528873199695415106305"}})
if err != nil {
log.Printf(err.Error())
return ""
}
defer resp.Body.Close()
reply := new(mitReply)
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(reply)
if err != nil {
return ""
}
log.Printf("reply from mit machine: %s", reply.Responses[0])
return reply.Responses[0]
}
type mitReply struct {
Responses []string `json:"responses"`
}
//get reply from iceAI
func iceAI(info string) string {
iceURL := fmt.Sprintf("http://127.0.0.1:8008/openxiaoice/ask?q=%s", url.QueryEscape(info))
timeout := time.Duration(4 * time.Second)
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(iceURL)
if err != nil {
return ""
}
defer resp.Body.Close()
reply := new(iceReply)
decoder := json.NewDecoder(resp.Body)
decoder.Decode(reply)
log.Printf("reply from xiaoice: %s", reply.Answer)
return reply.Answer
}
type iceReply struct {
Code int `json:"code"`
Answer string `json:"answer"`
}