Repository navigation
Expand file tree
/
Copy pathcore.go
More file actions
556 lines (452 loc) · 15.7 KB
/
Copy pathcore.go
File metadata and controls
556 lines (452 loc) · 15.7 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package GoBot
import (
"bufio"
"encoding/json"
"fmt"
"io"
"math/rand"
"os"
"reflect"
"strings"
"time"
)
var userInputs string
type GoBot struct {
Intent interface{} //This defines user intention
Story interface{} //This drives conversational flow (dialog)
State GoBotLifecycle
}
type GoBotForm struct {
Header string
Form []Form
Answers []FormAnswer
IntentAction []string
IntentCancel []string
ConfirmMessage string
ActionMessage string
CancelMessage string
}
type GoBotInput struct {
Header string
Form Form
IntentAction []string
IntentCancel []string
SuccessInputMessage string
ErrorInputMessage string
}
type GoBotChoice struct {
Header string
SuccessChoiceMessage string
ErrorChoiceMessage string
IntentAction []string
IntentCancel []string
Choices []string
}
type Form struct {
Variable string
Hint string
}
type FormAnswer struct {
Variable string
Value string
}
func NewGoBot(intents interface{}, stories interface{}) *GoBot {
return &GoBot{
Intent: intents,
Story: stories,
State: *NewLifecycle(),
}
}
func (gobot *GoBot) FindMessageKey(message string) string {
matchedKey := ""
storyType := ""
matchMessage := NewMatch("*" + strings.ToLower(message) + "*")
//Find a story where the message belongs
//Loop through all keys of intents
outerMatched := false
for key, intent := range gobot.Intent.(map[string]interface{}) {
//From Key loop in key slice to find message match
for _, intentMessage := range intent.([]string) {
outerMatched = matchMessage.Matches(strings.ToLower(intentMessage))
if outerMatched {
matchedKey = key
//Find story type
goBotStories := gobot.Story.(map[string]interface{})
storyObj := goBotStories[key]
if storyObj != nil {
story := storyObj.(map[string]interface{})
storyType = story["type"].(string)
if storyType == "form" {
storyForm := story[matchedKey+"_form"].(GoBotForm)
//Set GoBot Lifecycle Key, and type
gobot.State.SetState(key, storyType)
//Get Next story if any
nextStory := story["next"]
if nextStory != nil {
//Find type of next story
goBotStories := gobot.Story.(map[string]interface{})
storyObj := goBotStories[nextStory.(string)]
if storyObj != nil {
story := storyObj.(map[string]interface{})
gobot.State.SetNextStory(nextStory.(string), story["type"].(string))
} else {
fmt.Println("Please set type on ", nextStory.(string), " story")
} // TODO HANDLING ERRORs
} else {
gobot.State.SetNextStory("", "")
}
//Get all form ids and set them to gobot lifecycle
for _, form := range storyForm.Form {
gobot.State.ActiveFormIds = append(gobot.State.ActiveFormIds, form.Variable)
}
} else if storyType == "choices" {
//Set GoBot Lifecycle Key, and type
gobot.State.SetState(key, storyType)
//Get Next story if any
nextStory := story["next"]
if nextStory != nil {
//Find type of next story
goBotStories := gobot.Story.(map[string]interface{})
storyObj := goBotStories[nextStory.(string)]
if storyObj != nil {
story := storyObj.(map[string]interface{})
gobot.State.SetNextStory(nextStory.(string), story["type"].(string))
} else {
fmt.Println("Please set type on ", nextStory.(string), " story")
}
} else {
gobot.State.SetNextStory("", "")
}
} else if storyType == "input" {
gobot.State.SetState(key, storyType)
//Get Next story if any
nextStory := story["next"]
if nextStory != nil {
//Find type of next story
goBotStories := gobot.Story.(map[string]interface{})
storyObj := goBotStories[nextStory.(string)]
if storyObj != nil {
story := storyObj.(map[string]interface{})
gobot.State.SetNextStory(nextStory.(string), story["type"].(string))
} else {
fmt.Println("Please set type on ", nextStory.(string), " story")
}
}
} else {
//Echo response to user only
//Set GoBot Lifecycle Key, and type
gobot.State.SetState(key, "response")
//Get Next story if any
nextStory := story["next"]
if nextStory != nil {
gobot.State.NextStory = nextStory.(string)
}
}
} else {
//Return Error() --> no such story
fmt.Println()
}
break
}
}
if outerMatched {
break
}
}
return matchedKey
}
func (gobot *GoBot) Playground(id string) {
scanner := bufio.NewScanner(os.Stdin)
for {
if userInputs == "" {
fmt.Print("Start chatting with your Bot:\n")
}
// reads user input until \n by default
scanner.Scan()
// Holds the string that was scanned
text := scanner.Text()
if len(text) != 0 {
// fmt.Println(text)
userInputs = text
key, response := gobot.Chat(text, id)
fmt.Println(gobot.State.SavedResults)
fmt.Println("Intent: " + key + " ----- " + "Response: " + response)
} else {
// exit if user entered an empty string
break
}
}
// handle error
if scanner.Err() != nil {
fmt.Println("Error: ", scanner.Err())
}
}
func (gobot *GoBot) Chat(message string, id string) (string, string) {
key := gobot.FindMessageKey(message)
goBotStories := gobot.Story.(map[string]interface{})
if key != "" {
storyObj := goBotStories[key]
if key != "cancel" {
if storyObj != nil {
story := storyObj.(map[string]interface{})
activeKey, activeStoryType := gobot.State.GetState()
if activeKey == key && activeStoryType == "form" {
return gobot.CheckingGoBotState(key, activeKey, activeStoryType)
} else if activeKey == key && activeStoryType == "choices" {
return gobot.CheckingGoBotState(key, activeKey, activeStoryType)
} else if activeKey == key && activeStoryType == "input" {
return gobot.CheckingGoBotState(key, activeKey, activeStoryType)
} else {
}
//Check message type (string or []string{})
switch messageType := story["message"].(type) {
case string:
return key, story["message"].(string)
case []string:
messageSlice := story["message"].([]string)
//Randomize response (not suitable for security but to randomize our response is OK)
rand.Seed(time.Now().UnixNano())
randomValue := rand.Intn(len(messageSlice))
return key, messageSlice[randomValue]
default:
fmt.Printf("[]string: %v", messageType)
return key, "GoBot only support string and []string!"
}
} else {
// fmt.Println("Key when no interface: ", key)
// fmt.Println(key)
if !reflect.DeepEqual(gobot.State.ActiveForm, GoBotForm{}) {
//check if user has entered value for IntentAction OR IntentCancel
hasAction := containsInSlices(gobot.State.ActiveForm.IntentAction, message)
hasCancel := containsInSlices(gobot.State.ActiveForm.IntentCancel, message)
//Do this before clearly gobot lifecycle in memory
actionMessage := gobot.State.ActiveForm.ActionMessage
actionCancel := gobot.State.ActiveForm.CancelMessage
actionConfirm := gobot.State.ActiveForm.CancelMessage
gobot.State.ActiveForm = GoBotForm{}
gobot.State.ActiveCounter = 0
gobot.State.ActiceStoryType = ""
gobot.State.ActiveStory = ""
gobot.State.ActiveFormIds = []string{}
if hasAction {
return key, actionMessage
} else if hasCancel {
return key, actionCancel
} else {
return key, actionConfirm
}
} else if !reflect.DeepEqual(gobot.State.ActiveChoice, GoBotChoice{}) {
hasAction := containsInSlices(gobot.State.ActiveChoice.IntentAction, message)
hasCancel := containsInSlices(gobot.State.ActiveChoice.IntentCancel, message)
//Do this before clearly gobot lifecycle in memory
// successChoiceMessage := gobot.State.ActiveChoice.SuccessChoiceMessage
errorChoiceMessage := gobot.State.ActiveChoice.ErrorChoiceMessage
choice := gobot.State.ActiveChoiceValue
gobot.State.ActiveChoice = GoBotChoice{}
gobot.State.ActiveChoiceValue = ""
if hasAction {
fmt.Print("Has Action")
return key, choice
} else if hasCancel {
fmt.Print("Has Cancel")
return key, errorChoiceMessage
} else {
fmt.Print("Has Else nothing!")
return key, errorChoiceMessage
}
} else if !reflect.DeepEqual(gobot.State.ActiveInput, GoBotInput{}) {
hasAction := containsInSlices(gobot.State.ActiveInput.IntentAction, message)
hasCancel := containsInSlices(gobot.State.ActiveInput.IntentCancel, message)
//Do this before clearly gobot lifecycle in memory
// successChoiceMessage := gobot.State.ActiveChoice.SuccessChoiceMessage
errorInputMessage := gobot.State.ActiveInput.ErrorInputMessage
input := gobot.State.ActiveInputValue
gobot.State.ActiveInput = GoBotInput{}
gobot.State.ActiveChoiceValue = ""
if hasAction {
return key, input
} else if hasCancel {
return key, errorInputMessage
} else {
return key, errorInputMessage
}
} else {
fmt.Println("interface is nil")
return key, ""
}
}
} else {
// fmt.Println("Key: ", key)
// fmt.Println("Karibu tena")
//Reset GoBotLifecycle
gobot.State = GoBotLifecycle{}
return key, "Karibu tena"
}
} else {
if !reflect.DeepEqual(gobot.State.ActiveForm, GoBotForm{}) {
gobot.State.ActiveForm.Answers = append(gobot.State.ActiveForm.Answers, FormAnswer{
Variable: gobot.State.ActiveFormIds[gobot.State.ActiveCounter],
Value: message,
})
//Increment so we can go to next form data
if gobot.State.ActiveCounter != len(gobot.State.ActiveFormIds)-1 {
gobot.State.ActiveCounter += 1
return key, gobot.State.ActiveForm.Form[gobot.State.ActiveCounter].Hint
} else {
//End of the form data is reached, so send the summary to user so as to comfirm
/*
Your name?
Adam
Your age?
20
*/
answerObject := make(map[string]interface{})
prevKey, _ := gobot.State.GetState()
nextKey, nextType := gobot.State.GetNextStory()
summary := gobot.State.ActiveForm.ConfirmMessage + "\n\n"
for i := 0; i < len(gobot.State.ActiveForm.Answers); i++ {
if nextKey != "" {
answerObject[key] = map[string]interface{}{
gobot.State.ActiveForm.Answers[i].Variable: gobot.State.ActiveForm.Answers[i].Value,
}
}
summary += gobot.State.ActiveForm.Answers[i].Variable + "\n" + gobot.State.ActiveForm.Answers[i].Value + "\n"
}
//If it meant all requirement.... it goes to next story
if nextKey != "" {
//save the result according to story key in Gobot lifecycle object
gobot.State.SavedResults[prevKey] = answerObject
//After saving the results ,,,,, Free GoBotForm to be used by another story
if nextKey != prevKey {
gobot.State.ActiveForm = GoBotForm{}
gobot.State.ActiveCounter = 0
//Set state for upcoming story
gobot.State.SetState(nextKey, nextType)
activeKey, activeStoryType := gobot.State.GetState()
return gobot.CheckingGoBotState(nextKey, activeKey, activeStoryType)
}
} else {
fmt.Println("No next story")
gobot.State.SetNextStory("", "")
}
return key, summary
}
} else if !reflect.DeepEqual(gobot.State.ActiveChoice, GoBotChoice{}) {
answerObject := ""
prevKey, _ := gobot.State.GetState()
nextKey, nextType := gobot.State.GetNextStory()
for _, choice := range gobot.State.ActiveChoice.Choices {
if strings.ToLower(choice) == strings.ToLower(message) {
if nextKey != "" {
answerObject = message
}
}
}
//If it meant all requirement.... it goes to next story
if nextKey != "" {
//save the result according to story key in Gobot lifecycle object
gobot.State.SavedResults[prevKey] = answerObject
//After saving the results ,,,,, Free GoBotForm to be used by another story
if nextKey != prevKey {
gobot.State.ActiveChoice = GoBotChoice{}
//Set state for upcoming story
gobot.State.SetState(nextKey, nextType)
activeKey, activeStoryType := gobot.State.GetState()
return gobot.CheckingGoBotState(nextKey, activeKey, activeStoryType)
}
} else {
//save the result according to story key in Gobot lifecycle object
gobot.State.SavedResults[prevKey] = answerObject
gobot.State.SetNextStory("", "")
return prevKey, gobot.State.ActiveChoice.SuccessChoiceMessage + " " + gobot.State.ActiveChoiceValue
}
if prevKey == nextKey {
return prevKey, gobot.State.ActiveChoice.SuccessChoiceMessage + " " + gobot.State.ActiveChoiceValue
} else {
return "key", gobot.State.ActiveChoice.ErrorChoiceMessage
}
} else if !reflect.DeepEqual(gobot.State.ActiveInput, GoBotInput{}) {
prevKey, _ := gobot.State.GetState()
nextKey, nextType := gobot.State.GetNextStory()
gobot.State.ActiveInputValue = message
if nextKey != "" {
//save the result according to story key in Gobot lifecycle object
gobot.State.SavedResults[prevKey] = message
//After saving the results ,,,,, Free GoBotForm to be used by another story
if nextKey != prevKey {
gobot.State.ActiveInput = GoBotInput{}
//Set state for upcoming story
gobot.State.SetState(nextKey, nextType)
activeKey, activeStoryType := gobot.State.GetState()
return gobot.CheckingGoBotState(nextKey, activeKey, activeStoryType)
}
} else {
gobot.State.SavedResults[prevKey] = message
gobot.State.SetNextStory("", "")
}
return prevKey, gobot.State.ActiveInput.SuccessInputMessage
//If it meant all requirement.... it goes to next story
} else {
fallbackObject := goBotStories["fallback"].(map[string]interface{})
//Check message type (string or []string{})
switch messageType := fallbackObject["message"].(type) {
case string:
return key, fallbackObject["message"].(string)
case []string:
messageSlice := fallbackObject["message"].([]string)
//Randomize response (not suitable for security but to randomize our response is OK)
rand.Seed(time.Now().UnixNano())
randomValue := rand.Intn(len(messageSlice))
return key, messageSlice[randomValue]
default:
fmt.Printf("[]string: %v", messageType)
return key, "GoBot only support string and []string!"
}
}
}
}
func (gobot *GoBot) CheckingGoBotState(key string, activeKey string, activeStoryType string) (string, string) {
goBotStories := gobot.Story.(map[string]interface{})
storyObj := goBotStories[key]
story := storyObj.(map[string]interface{})
if key != "cancel" {
if activeKey == key && activeStoryType == "form" {
storyForm := story[key+"_form"].(GoBotForm)
if reflect.DeepEqual(gobot.State.ActiveForm, GoBotForm{}) {
gobot.State.ActiveCounter = 0 //This will help us iterate through forms data slice
gobot.State.ActiveForm = storyForm
//Ask a first form data?
return key, storyForm.Form[gobot.State.ActiveCounter].Hint
}
} else if activeKey == key && activeStoryType == "choices" {
storyForm := story[key+"_choices"].(GoBotChoice)
if reflect.DeepEqual(gobot.State.ActiveForm, GoBotForm{}) {
gobot.State.ActiveChoice = storyForm
//Ask a first form data?
return key, storyForm.Header
}
//others
} else if activeKey == key && activeStoryType == "input" {
storyForm := story[key+"_input"].(GoBotInput)
if reflect.DeepEqual(gobot.State.ActiveForm, GoBotForm{}) {
gobot.State.ActiveInput = storyForm
//Ask a first form data?
return key, storyForm.Header
}
} else {
//Other
return key, ""
}
return key, ""
} else {
return key, "Karibu tena"
}
}
func (gobot *GoBot) FromJSON(r io.Reader) error {
e := json.NewDecoder(r)
return e.Decode(gobot)
}
func (gobot *GoBot) ToJSON(w io.Writer) error {
e := json.NewEncoder(w)
return e.Encode(gobot)
}