-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
333 lines (298 loc) · 9.57 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
"bitbucket.org/ckvist/twilio/twiml"
_ "github.com/go-sql-driver/mysql"
)
// MMSSecret holds all Watson and Twilio and other credentials
type MMSSecret struct {
JPetstoreURL string `json:"jpetstoreurl"`
WatsonSecret struct {
URL string `json:"url"`
Note string `json:"note"`
APIKey string `json:"api_key"`
APIKey2 string `json:"apikey"`
} `json:"watson"`
TwilioSecret struct {
AccountSID string `json:"sid"`
AccountToken string `json:"token"`
Number string `json:"number"`
} `json:"twilio"`
}
type ErrorInfo struct {
ErrorID string `json:"error_id"`
Description string `json:"description"`
}
type WarningInfo struct {
WarningID string `json:"warning_id"`
Description string `json:"description"`
}
type ClassResult struct {
Class string `json:"class"`
Score float64 `json:"score"`
TypeHierarchy string `json:"type_hierarchy,omitempty"`
}
type ClassifierResult struct {
Classes []ClassResult `json:"classes"`
ClassifierID string `json:"classifier_id"`
Name string `json:"name"`
}
type ClassifiedImage struct {
Classifiers []ClassifierResult `json:"classifiers"`
Image string `json:"image,omitempty"`
SourceURL string `json:"source_url"`
ResolvedURL string `json:"resolved_url"`
Error ErrorInfo `json:"error,omitempty"`
}
type ClassifiedImages struct {
CustomClasses int64 `json:"custom_classes"`
ImagesProcessed int64 `json:"images_processed"`
Images []ClassifiedImage `json:"images"`
Warnings []WarningInfo `json:"warnings,omitempty"`
}
type SimulatorResponse struct {
Response string `json:"response"`
IMAGEURL string `json:"imageURL"`
}
var mmsSecretsFileName = "mms-secrets.json"
var watsonVRURLStr string
var watsonVersion = "2016-05-20"
// var twilioSecrets TwilioSecret
// var watsonSecrets WatsonSecret
var mmsSecrets MMSSecret
var db *sql.DB
var (
productid string
category string
name string
descn string
listprice float64
attr1 string
)
func main() {
var mmsSecretFile []byte
// Get Watson secrets
mmsSecretFile, err := ioutil.ReadFile("/etc/secrets/" + mmsSecretsFileName)
if err != nil {
// If you can't find it in /etc/secrets, check the current dir
mmsSecretFile, err = ioutil.ReadFile(mmsSecretsFileName)
if err != nil {
fmt.Println(err.Error())
fmt.Println("Couldn't find file '" + mmsSecretsFileName + "' in /etc/secrets or current dir.")
os.Exit(1)
}
}
err = json.Unmarshal(mmsSecretFile, &mmsSecrets)
if err != nil {
fmt.Println("Something is wrong with your JSON : ", err)
}
fmt.Println("(Updated2) Watson URL: ", mmsSecrets.WatsonSecret.URL)
// if api_key is empty, check apikey
if mmsSecrets.WatsonSecret.APIKey == "" {
mmsSecrets.WatsonSecret.APIKey = mmsSecrets.WatsonSecret.APIKey2
}
watsonVRURLStr = mmsSecrets.WatsonSecret.URL + "/v3/classify"
// fmt.Println("Twilio SID: ", mmsSecrets.TwilioSecret.AccountSID)
// fmt.Println("Twilio Token: ", mmsSecrets.TwilioSecret.AccountToken)
// fmt.Println("Twilio Number: ", mmsSecrets.TwilioSecret.Number)
fmt.Println("JPetStore URL: ", mmsSecrets.JPetstoreURL)
http.HandleFunc("/sms/receive", receiveSMSHandler)
http.HandleFunc("/simulator/receive", receiveSimulatorHandler)
http.Handle("/", http.FileServer(http.Dir("./static")))
fmt.Println("http://localhost:8080/")
http.ListenAndServe(":8080", nil)
}
// Handles responses when using the web chat interface
func receiveSimulatorHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("New message received from simulator")
var Buf bytes.Buffer
//parse the data POSTed
r.ParseMultipartForm(32 << 20)
file, header, err := r.FormFile("picture") // file has the image
if err != nil {
fmt.Fprintf(w, "Error reading uploaded image")
fmt.Println("whoops:", err)
return
}
defer file.Close()
// grab the image
bodyImage := &bytes.Buffer{} // address of a buffer
writer := multipart.NewWriter(bodyImage) //writer that will write to bodyImage
part, err := writer.CreateFormFile("images_file", header.Filename)
if err != nil {
fmt.Println("whoops:", err)
}
_, err = io.Copy(part, file) //copy contents from file to part
err = writer.Close()
if err != nil {
fmt.Println("whoops:", err)
}
// call Watson visual recognition API
req, _ := http.NewRequest("POST", watsonVRURLStr, bodyImage)
req.Header.Add("Accept", "application/json")
req.Header.Set("Content-Type", writer.FormDataContentType())
req.SetBasicAuth("apikey", mmsSecrets.WatsonSecret.APIKey)
msgQ := req.URL.Query()
msgQ.Add("api_key", mmsSecrets.WatsonSecret.APIKey)
msgQ.Add("version", watsonVersion)
req.URL.RawQuery = msgQ.Encode()
client := &http.Client{}
watsonResp, err := client.Do(req)
if err != nil {
fmt.Println("Error calling Watson:", err)
fmt.Println("Trying again with http instead (Istio)")
// TODO: Need a better way of determining if running with ISTIO. env var?
// If running with Istio, communication with Envoy sidecar is http. Envoy will use HTTPS.
req.URL.Scheme = "http"
req.URL.Host = req.URL.Host + ":443"
watsonResp, err = client.Do(req)
if err != nil {
fmt.Println("Error calling Watson (Istio):", err)
}
}
// parse data from Watson
textResponse, dbMediaURL := parseWatsonResponse(watsonResp)
Buf.Reset()
// format response
sResponse := SimulatorResponse{
Response: textResponse,
IMAGEURL: dbMediaURL,
}
b, _ := json.Marshal(sResponse)
fmt.Fprintf(w, string(b))
}
// Handles responses when using the Twilio SMS interface
func receiveSMSHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("New SMS message received from Twilio")
err := r.ParseForm()
if err != nil {
fmt.Println("whoops:", err)
}
sender := r.FormValue("From")
// body := r.FormValue("Body")
numMedia, _ := strconv.Atoi(r.FormValue("NumMedia"))
mediaURL := r.FormValue("MediaUrl0")
// mediaType := r.FormValue("MediaContentType0")
twilioResponse := "No response generated"
dbMediaURL := ""
if numMedia == 0 {
twilioResponse = "No images provided"
} else {
req, _ := http.NewRequest("POST", watsonVRURLStr, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth("apikey", mmsSecrets.WatsonSecret.APIKey)
msgQ := req.URL.Query()
msgQ.Add("api_key", mmsSecrets.WatsonSecret.APIKey)
msgQ.Add("version", watsonVersion)
msgQ.Add("url", mediaURL)
req.URL.RawQuery = msgQ.Encode()
client := &http.Client{}
watsonResp, err := client.Do(req)
if err != nil {
fmt.Println("whoops:", err)
}
twilioResponse, dbMediaURL = parseWatsonResponse(watsonResp)
}
resp := twiml.NewResponse()
if dbMediaURL != "" {
resp.Action(twiml.Message{
Body: fmt.Sprintf(twilioResponse),
From: mmsSecrets.TwilioSecret.Number,
To: sender,
Media: dbMediaURL,
})
} else {
resp.Action(twiml.Message{
Body: fmt.Sprintf(twilioResponse),
From: mmsSecrets.TwilioSecret.Number,
To: sender,
})
}
resp.Send(w)
}
func parseWatsonResponse(watsonResp *http.Response) (string, string) {
watsonJSON := &ClassifiedImages{}
watsonBody, err := ioutil.ReadAll(watsonResp.Body)
if err != nil {
fmt.Println("whoops:", err)
}
err = json.Unmarshal(watsonBody, &watsonJSON)
if err != nil {
fmt.Println("whoops:", err)
}
var response = ""
var dbMediaURL = ""
bestResponse := watsonJSON.Images[0].Classifiers[0].Classes[0].Class
bestScore := watsonJSON.Images[0].Classifiers[0].Classes[0].Score
if bestScore > 0.5 {
// strip response to first word and lowercase
if strings.LastIndex(bestResponse, " ") != -1 {
bestResponse = bestResponse[:strings.LastIndex(bestResponse, " ")-1]
}
bestResponse = strings.ToLower(bestResponse)
dbLoc := "/jpetstore"
val, ok := os.LookupEnv("DB_LOCATION")
if ok {
dbLoc = val
} else {
response = bestResponse + " : Unable to connect to DB"
return response, dbMediaURL
}
db, err := sql.Open("mysql", "jpetstore:foobar@"+dbLoc)
if err != nil {
fmt.Println(err.Error()) // You should use proper error handling
}
// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
fmt.Println(err.Error()) // You should use proper error handling
}
defer db.Close()
rows, err := db.Query("SELECT * FROM product WHERE descn LIKE '%" + bestResponse + "%' OR name LIKE + '%" + bestResponse + "%'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
rows.Next()
err = rows.Scan(&productid, &category, &name, &descn)
if err != nil {
response = "Our catalog does not have a matching item for " + bestResponse
} else {
longName := name + ": " + descn[strings.LastIndex(descn, ">")+1:]
dbMediaURL = mmsSecrets.JPetstoreURL + descn[strings.LastIndex(descn, "../")+2:strings.LastIndex(descn, ">")-1]
if !strings.HasPrefix(dbMediaURL, "http") {
dbMediaURL = "http://" + dbMediaURL
}
var ()
rows, err = db.Query("SELECT listprice, attr1 FROM item WHERE productid LIKE '" + productid + "'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
rows.Next()
err = rows.Scan(&listprice, &attr1)
if err != nil {
log.Fatal(err)
}
longNamepostfix := " (" + attr1 + ") for $" + strconv.FormatFloat(listprice, 'f', 2, 64)
response = "Our catalog has a " + longName + longNamepostfix
}
} else {
response = "Photo cannot be adequately be classified. Please try another photo."
}
fmt.Println("response: ", response)
fmt.Println("dbMediaURL: ", dbMediaURL)
return response, dbMediaURL
}