You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

112 lines
2.9 KiB

package main
import (
"log"
"strings"
"samhofi.us/x/keybase"
"samhofi.us/x/keybase/types/chat1"
"samhofi.us/x/keybase/types/stellar1"
)
// RegisterHandlers is called by main to map these handler funcs to events
func (b *bot) registerHandlers() {
chat := b.chatHandler
conv := b.convHandler
wallet := b.walletHandler
err := b.errHandler
b.handlers = keybase.Handlers{
ChatHandler: &chat,
ConversationHandler: &conv,
WalletHandler: &wallet,
ErrorHandler: &err,
}
}
// chatHandler should handle all messages coming from the chat
func (b *bot) chatHandler(m chat1.MsgSummary) {
// only handle text, we don't really care about attachments
if m.Content.TypeName != "text" {
return
}
// if this chat message is a payment, add it to the bot payments
if m.Content.Text.Payments != nil {
b.handlePayment(m)
return
}
// Determine first if this is a command
if strings.HasPrefix(m.Content.Text.Body, "!") || strings.HasPrefix(m.Content.Text.Body, "@") {
// determine the root command
body := strings.ToLower(m.Content.Text.Body)
words := strings.Fields(body)
command := strings.Replace(words[0], "@", "", 1)
command = strings.Replace(command, "!", "", 1)
command = strings.ToLower(command)
// create the args
args := words[1:]
nargs := len(args)
switch command {
case b.k.Username:
if nargs > 0 {
switch args[0] {
case "set":
b.setKValue(m.ConvID, m.Id, args)
case "list":
b.listKValue(m.ConvID, m.Id, args)
}
}
case "jitsi":
if nargs == 0 {
b.handleMeeting(m)
} else if nargs >= 1 {
switch args[0] {
case "meet":
b.handleMeeting(m)
case "feedback":
b.handleFeedback(m)
case "hello":
fallthrough
case "help":
b.handleWelcome(m.ConvID)
default:
return
}
}
default:
return
}
}
}
// handle conversations (this fires when a new conversation is initiated)
// i.e. when someone opens a conversation to you but hasn't sent a message yet
func (b *bot) convHandler(m chat1.ConvSummary) {
switch m.Channel.MembersType {
case "team":
b.debug("Added to new team: @%s (%s) Sending welcome message", m.Channel.Name, m.Id)
case "impteamnative":
b.debug("New conversation found %s (%s) Sending welcome message", m.Channel.Name, m.Id)
default:
b.debug("New convID found %s, sending welcome message.", m.Id)
}
b.handleWelcome(m.Id)
}
// this handles wallet events, like when someone send you money in chat
func (b *bot) walletHandler(m stellar1.PaymentDetailsLocal) {
// if the payment is successful
if m.Summary.StatusSimplified == 3 {
// get the reply info and see if it exists
replyInfo := b.payments[m.Summary.Id]
if replyInfo.convID != "" {
b.k.ReplyByConvID(replyInfo.convID, replyInfo.msgID, "Thank you so much! I'll use this to offset my hosting costs!")
}
}
}
// this handles all errors returned from the keybase binary
func (b *bot) errHandler(m error) {
log.Println("---[ error ]---")
log.Println(p(m))
}