-
Notifications
You must be signed in to change notification settings - Fork 2
/
cortex.go
83 lines (70 loc) · 2.25 KB
/
cortex.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
package neocortex
import (
"context"
"errors"
"log"
"time"
)
type OutputResponse func(c *Context, output *Output) error
type HandleResolver func(c *Context, in *Input, out *Output, response OutputResponse) error
type MiddleHandler func(c *Context, message *Input, response OutputResponse) error
type ContextFabric func(ctx context.Context, info PersonInfo) *Context
type Engine struct {
done chan error
cognitive CognitiveService
channels []CommunicationChannel
registeredResolvers map[CommunicationChannel]map[*Matcher]*HandleResolver
generalResolver map[CommunicationChannel]*HandleResolver
registeredInjection map[CommunicationChannel]map[*Matcher]*InInjection
generalInjection map[CommunicationChannel]*InInjection
Repository Repository
ActiveDialogs map[*Context]*Dialog
api *API
Register map[string]string
Analytics *Analytics
dialogPerformanceFunc func(*Dialog) float64
secret string
}
func (engine *Engine) onNewContextCreated(c *Context) {
log.Println("creating new context: ", c.SessionID)
engine.ActiveDialogs[c] = newDialog()
}
func (engine *Engine) onContextIsDone(c *Context) {
for _, ch := range engine.channels {
ch.CallContextDone(c)
}
log.Println("closing context: ", c.SessionID)
if dialog, ok := engine.ActiveDialogs[c]; ok {
dialog.EndAt = time.Now()
if engine.Repository != nil {
dialog.Performance = engine.dialogPerformanceFunc(dialog)
err := engine.Repository.SaveDialog(dialog)
if err != nil {
engine.done <- err
}
}
delete(engine.ActiveDialogs, c)
log.Println("finally deleting: ", c.SessionID)
}
}
// RegisterAdmin you can register new admin for get info purpose
func (engine *Engine) RegisterAdmin(Username, Password string) error {
if engine.Register != nil {
engine.Register[Username] = Password
}
return nil
}
// getAdmin you can get the password of the admin
func (engine *Engine) getAdmin(Username string) (string, error) {
if val, ok := engine.Register[Username]; ok {
return val, nil
}
return "", nil
}
// RemoveAdmin let you remove the admin
func (engine *Engine) RemoveAdmin(Username string) error {
if _, ok := engine.Register[Username]; ok {
delete(engine.Register, Username)
}
return errors.New("no user found")
}