added chat logging capabilities
This commit is contained in:
9
args.go
9
args.go
@ -16,7 +16,8 @@ func (b *bot) parseArgs(args []string) error {
|
||||
// then parse CLI args as overrides
|
||||
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||
cliConfig := botConfig{}
|
||||
flags.BoolVar(&cliConfig.Debug, "debug", false, "enables command debugging")
|
||||
flags.BoolVar(&cliConfig.Debug, "debug", false, "enables command debugging to stdout")
|
||||
flags.StringVar(&cliConfig.logConvIDStr, "log-convid", "", "sets the keybase chat1.ConvIDStr to log to for feedback")
|
||||
if err := flags.Parse(args[1:]); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -26,10 +27,16 @@ func (b *bot) parseArgs(args []string) error {
|
||||
if cliConfig.Debug == true {
|
||||
b.config.Debug = true
|
||||
}
|
||||
if cliConfig.logConvIDStr != "" {
|
||||
b.config.logConvIDStr = cliConfig.logConvIDStr
|
||||
}
|
||||
}
|
||||
|
||||
// then print the running options
|
||||
b.debug("Debug Enabled")
|
||||
if b.config.logConvIDStr != "" {
|
||||
b.debug("Logging to conversation %s", b.config.logConvIDStr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
18
main.go
18
main.go
@ -25,6 +25,7 @@ type bot struct {
|
||||
// fields must be exported for package env (reflect) to work
|
||||
type botConfig struct {
|
||||
Debug bool `env:"BOT_DEBUG" envDefault:"false"`
|
||||
logConvIDStr string `env:"BOT_LOG_CONVID" envDefault:""`
|
||||
}
|
||||
|
||||
// hold reply information when needed
|
||||
@ -37,6 +38,23 @@ type botReply struct {
|
||||
func (b *bot) debug(s string, a ...interface{}) {
|
||||
if b.config.Debug {
|
||||
log.Printf(s, a...)
|
||||
if b.config.logConvIDStr != "" {
|
||||
b.logToChat(s, a...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// logToChat will send this message to the keybase chat configured in b.logConv
|
||||
func (b *bot) logToChat(s string, a ...interface{}) {
|
||||
// if the ConvIdStr isn't blank try to log
|
||||
if b.config.logConvIDStr != "" {
|
||||
// if you can't send the message, log the error to stdout
|
||||
if _, err := b.k.SendMessageByConvID(chat1.ConvIDStr(b.config.logConvIDStr), s, a...); err != nil {
|
||||
log.Printf("Unable to log to keybase chat: %s", err)
|
||||
}
|
||||
} else {
|
||||
// otherwise (and you shouldn't be here but....) log it to stdout
|
||||
log.Println("Unable to log to keybase chat, logging ConvIDStr is not set")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user