/
main.go
140 lines (126 loc) · 3.58 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
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"github.com/keybase/managed-bots/macrobot/macrobot"
"github.com/keybase/go-keybase-chat-bot/kbchat"
"github.com/keybase/go-keybase-chat-bot/kbchat/types/chat1"
"github.com/keybase/managed-bots/base"
"golang.org/x/sync/errgroup"
)
type BotServer struct {
*base.Server
opts base.Options
kbc *kbchat.API
}
func NewBotServer(opts base.Options) *BotServer {
return &BotServer{
Server: base.NewServer("macrobot", opts.Announcement, opts.AWSOpts, opts.MultiDSN, opts.ReadSelf, kbchat.RunOptions{
KeybaseLocation: opts.KeybaseLocation,
HomeDir: opts.Home,
}),
opts: opts,
}
}
const (
back = "`"
backs = "```"
)
func (s *BotServer) makeAdvertisement() kbchat.Advertisement {
createDesc := fmt.Sprintf("Create or update a macro for the current team or conversation. %s",
fmt.Sprintf(macrobot.CreateCmdHelp, backs, backs, back, back, back, back))
removeDesc := fmt.Sprintf(`Remove a macro from the current team or conversation. You must specify the name of the macro.
Examples:%s
!macro remove docs
!macro remove lunchflip%s`,
backs, backs)
cmds := []chat1.UserBotCommandInput{
{
Name: "macro create",
Description: "Create or update a macro for the current team or conversation",
ExtendedDescription: &chat1.UserBotExtendedDescription{
Title: `*!macro create* <name> <message>`,
DesktopBody: createDesc,
MobileBody: createDesc,
},
},
{
Name: "macro list",
Description: "List available macros for the current team or conversation",
},
{
Name: "macro remove",
Description: "Remove a macro from the current team or conversation",
ExtendedDescription: &chat1.UserBotExtendedDescription{
Title: `*!macro remove* <name>`,
DesktopBody: removeDesc,
MobileBody: removeDesc,
},
},
base.GetFeedbackCommandAdvertisement(s.kbc.GetUsername()),
}
return kbchat.Advertisement{
Alias: "Macro Bot",
Advertisements: []chat1.AdvertiseCommandAPIParam{
{
Typ: "public",
Commands: cmds,
},
},
}
}
func (s *BotServer) Go() (err error) {
if s.kbc, err = s.Start(s.opts.ErrReportConv); err != nil {
return err
}
sdb, err := sql.Open("mysql", s.opts.DSN)
if err != nil {
s.Errorf("failed to connect to MySQL: %s", err)
return err
}
defer sdb.Close()
db := macrobot.NewDB(sdb)
debugConfig := base.NewChatDebugOutputConfig(s.kbc, s.opts.ErrReportConv)
stats, err := base.NewStatsRegistry(debugConfig, s.opts.StathatEZKey)
if err != nil {
s.Debug("unable to create stats: %v", err)
return err
}
stats = stats.SetPrefix(s.Name())
handler := macrobot.NewHandler(stats, s.kbc, debugConfig, db)
httpSrv := macrobot.NewHTTPSrv(stats, debugConfig)
eg := &errgroup.Group{}
s.GoWithRecover(eg, func() error { return s.Listen(handler) })
s.GoWithRecover(eg, httpSrv.Listen)
s.GoWithRecover(eg, func() error { return s.HandleSignals(httpSrv, stats) })
s.GoWithRecover(eg, func() error { return s.AnnounceAndAdvertise(s.makeAdvertisement(), "I live.") })
if err := eg.Wait(); err != nil {
s.Debug("wait error: %s", err)
return err
}
return nil
}
func main() {
rc := mainInner()
os.Exit(rc)
}
func mainInner() int {
opts := base.NewOptions()
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
if err := opts.Parse(fs, os.Args); err != nil {
fmt.Printf("Unable to parse options: %v\n", err)
return 3
}
if len(opts.DSN) == 0 {
fmt.Printf("must specify a database DSN\n")
return 3
}
bs := NewBotServer(*opts)
if err := bs.Go(); err != nil {
fmt.Printf("error running chat loop: %s\n", err)
return 3
}
return 0
}