-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
190 lines (156 loc) · 3.77 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"regexp"
"time"
_ "github.com/azzzak/alice-nearby/statik"
flags "github.com/jessevdk/go-flags"
"github.com/rakyll/statik/fs"
)
// Opts with cli commands and flags
type Opts struct {
Webhook string `long:"webhook" env:"WEBHOOK" required:"true" description:"url to webhook"`
Port string `long:"port" short:"p" env:"PORT" required:"false" default:"2345" description:"port to web frontend"`
}
type question struct {
Message int `json:"message_id"`
Session string `json:"session_id"`
Skill string `json:"skill_id"`
User string `json:"user_id"`
Question string `json:"question"`
}
type packet struct {
Request *Request `json:"request"`
Response *Response `json:"response"`
}
var opts Opts
func main() {
_, err := flags.Parse(&opts)
if err != nil {
// fmt.Println(err)
os.Exit(1)
}
// fmt.Println(opts)
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
rand.Seed(time.Now().UnixNano())
http.HandleFunc("/app", listen)
http.Handle("/", http.StripPrefix("/", http.FileServer(statikFS)))
// http.Handle("/", http.FileServer(http.Dir("./public")))
http.ListenAndServe(fmt.Sprintf(":%s", opts.Port), nil)
}
func makeRequest(p question) *Request {
r := Request{}
r.Meta.ClientID = "ru.yandex.searchplugin/7.16 (none none; android 4.4.2)"
r.Meta.Locale = "ru-RU"
r.Meta.Timezone = "UTC"
r.Request.Command = p.Question
r.Request.Utterance = p.Question
r.Request.Type = CommandTypeVoice
re := regexp.MustCompile(`[\s,.]`)
tmp := re.Split(p.Question, -1)
var tokens = []string{}
for _, v := range tmp {
if v != "" && v != "-" {
tokens = append(tokens, v)
}
}
r.Request.NLU.Tokens = tokens
r.Request.NLU.Entities = []struct {
Tokens struct {
Start int `json:"start"`
End int `json:"end"`
} `json:"tokens"`
Type string `json:"type"`
Value *json.RawMessage `json:"value"`
}{}
r.Session.MessageID = p.Message
if p.Session != "" {
r.Session.SessionID = p.Session
} else {
r.Session.SessionID = generateID()
}
if p.Skill != "" {
r.Session.SkillID = p.Skill
} else {
r.Session.SkillID = generateID()
}
if p.User != "" {
r.Session.UserID = p.User
} else {
r.Session.UserID = generateID()
}
r.Version = "1.0"
return &r
}
func generateID() string {
str := "0123456789abcdef"
var buf bytes.Buffer
for i := 0; i < 32; i++ {
if i == 8 || i == 16 || i == 24 {
buf.Write([]byte("-"))
}
rnd := rand.Intn(len(str))
buf.Write([]byte{str[rnd]})
}
return buf.String()
}
func useWebhook(u *Request) *Response {
var client = &http.Client{
Timeout: time.Second * 3,
}
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(u)
if err != nil {
fmt.Println(err)
}
res, err := client.Post(opts.Webhook, "application/json; charset=utf-8", b)
if err != nil {
fmt.Println(err)
return nil
}
defer res.Body.Close()
var body Response
err = json.NewDecoder(res.Body).Decode(&body)
if err != nil {
fmt.Println(err)
}
// fmt.Printf("%s -> %s\n", u.Request.Command, body.Response.Text)
return &body
}
func listen(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var p question
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil && err != io.EOF {
fmt.Println(err)
}
// fmt.Printf("%+v\n", p)
request := makeRequest(p)
resp := useWebhook(request)
if resp == nil {
return
}
profile := packet{
request,
resp,
}
js, err := json.MarshalIndent(profile, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}