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.
72 lines
1.6 KiB
72 lines
1.6 KiB
package main |
|
|
|
import ( |
|
"fmt" |
|
"os" |
|
"os/signal" |
|
"syscall" |
|
"time" |
|
|
|
"app/cmd" |
|
|
|
"github.com/kf5grd/keybasebot" |
|
"samhofi.us/x/keybase/v2" |
|
"samhofi.us/x/keybase/v2/types/chat1" |
|
) |
|
|
|
// We'll use this to create a writer for the Logger which will be able to write logs to |
|
// stdout, and optionally also to a Keybase chat conversation |
|
type kbWriter struct { |
|
convID chat1.ConvIDStr |
|
bot *keybasebot.Bot |
|
} |
|
|
|
func (k kbWriter) Write(p []byte) (n int, err error) { |
|
opt := keybase.SendMessageOptions{ |
|
ConversationID: k.convID, |
|
Message: keybase.SendMessageBody{Body: string(p)}, |
|
} |
|
go k.bot.KB.SendMessage("send", opt) |
|
fmt.Fprintf(os.Stdout, string(p)) |
|
return len(p), nil |
|
} |
|
|
|
func main() { |
|
opts := parseArgs(os.Args) |
|
b := keybasebot.New("", keybase.SetHomePath(opts.HomePath)) |
|
b.Debug = opts.Debug |
|
b.JSON = opts.JSON |
|
|
|
// set up the log writer |
|
w := kbWriter{ |
|
convID: chat1.ConvIDStr(opts.LogConvIDStr), |
|
bot: b, |
|
} |
|
b.LogWriter = w |
|
// register the bot commands |
|
b.Commands = append(b.Commands, |
|
keybasebot.BotCommand{ |
|
Name: "ping", |
|
Ad: &cmd.PingAd, |
|
Run: keybasebot.Adapt(cmd.SendPong, keybasebot.MessageType("text"), keybasebot.CommandPrefix("!ping")), |
|
}, |
|
keybasebot.BotCommand{ |
|
Name: "age", |
|
Ad: &cmd.AgeAd, |
|
Run: keybasebot.Adapt(cmd.Age, keybasebot.MessageType("text"), keybasebot.CommandPrefix("!age")), |
|
}, |
|
) |
|
// catch ctrl-c so we can clean up |
|
c := make(chan os.Signal) |
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
|
go func() { |
|
<-c |
|
b.Logger.Info("Caught SIGINT, cleaning up.") |
|
b.KB.ClearCommands() |
|
b.Logger.Info("Cleared command adverts. Bye.") |
|
time.Sleep(time.Second * 2) |
|
os.Exit(0) |
|
}() |
|
// then run |
|
b.Run() |
|
}
|
|
|