forked from itsabot/itsabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaffold.go
77 lines (68 loc) · 1.54 KB
/
scaffold.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
package main
import (
"os"
"github.com/itsabot/abot/core"
)
func pluginScaffoldFile(dir, name string) string {
return `package ` + name + `
import (
"github.com/itsabot/abot/core/log"
"github.com/itsabot/abot/shared/datatypes"
"github.com/itsabot/abot/shared/plugin"
)
var p *dt.Plugin
const memKey = "firstToken"
func init() {
var err error
p, err = plugin.New("` + dir + `")
if err != nil {
log.Fatal("failed to build plugin ` + name + `", err)
}
plugin.SetKeywords(p,
dt.KeywordHandler{
Fn: kwDemo,
Trigger: &dt.StructuredInput{
Commands: []string{
"show",
},
Objects: []string{
"demo",
},
},
},
)
plugin.SetStates(p, [][]dt.State{[]dt.State{
{
OnEntry: func(in *dt.Msg) string {
return "This is a demo."
},
OnInput: func(in *dt.Msg) {
if len(in.Tokens) == 0 {
return
}
p.SetMemory(in, memKey, in.Tokens[0])
},
Complete: func(in *dt.Msg) (bool, string) {
return p.HasMemory(in, memKey), "I didn't understand that."
},
},
}})
p.SM.SetOnReset(func(in *dt.Msg) {
p.DeleteMemory(in, memKey)
})
if err = plugin.Register(p); err != nil {
p.Log.Fatalf("failed to register plugin ` + name + `. %s", err)
}
}
func kwDemo(in *dt.Msg) string {
return "It worked! You typed: " + in.Sentence
}`
}
func serverAbotEnv(name, curDir string) string {
return `PORT=` + os.Getenv("PORT") + `
ABOT_ENV=development
ABOT_PATH="` + curDir + `"
ABOT_DATABASE_URL="` + core.DBConnectionString(name) + `"
ABOT_SECRET=` + core.RandAlphaNumSeq(64) + `
ABOT_URL=http://localhost:$PORT`
}