-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathkbchat.go
68 lines (62 loc) · 1.77 KB
/
kbchat.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
package slackbot
import (
"fmt"
"log"
"github.com/keybase/go-keybase-chat-bot/kbchat"
"github.com/keybase/go-keybase-chat-bot/kbchat/types/chat1"
)
type KeybaseChatBotBackend struct {
name string
convID chat1.ConvIDStr
kbc *kbchat.API
}
func NewKeybaseChatBotBackend(name string, convID string, opts kbchat.RunOptions) (BotBackend, error) {
var err error
bot := &KeybaseChatBotBackend{
convID: chat1.ConvIDStr(convID),
name: name,
}
if bot.kbc, err = kbchat.Start(opts); err != nil {
return nil, err
}
return bot, nil
}
func (b *KeybaseChatBotBackend) SendMessage(text string, convID string) {
if chat1.ConvIDStr(convID) != b.convID {
// bail out if not on configured conv ID
log.Printf("SendMessage: refusing to send on non-configured convID: %s != %s\n", convID, b.convID)
return
}
if len(text) == 0 {
log.Printf("SendMessage: skipping blank message")
return
}
log.Printf("sending message: convID: %s text: %s", convID, text)
if _, err := b.kbc.SendMessageByConvID(chat1.ConvIDStr(convID), text); err != nil {
log.Printf("SendMessage: failed to send: %s\n", err)
}
}
func (b *KeybaseChatBotBackend) Listen(runner BotCommandRunner) {
sub, err := b.kbc.ListenForNewTextMessages()
if err != nil {
panic(fmt.Sprintf("failed to set up listen: %s", err))
}
commandPrefix := "!" + b.name
for {
msg, err := sub.Read()
if err != nil {
log.Printf("Listen: failed to read message: %s", err)
continue
}
if msg.Message.Content.TypeName != "text" {
continue
}
args := parseInput(msg.Message.Content.Text.Body)
if len(args) > 0 && args[0] == commandPrefix && b.convID == msg.Message.ConvID {
cmd := args[1:]
if err := runner.RunCommand(cmd, string(b.convID)); err != nil {
log.Printf("unable to run command: %s", err)
}
}
}
}