package cmd import ( "fmt" "os" "os/signal" "syscall" "time" "dev.sum7.eu/genofire/golang-lib/file" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" xmppbot "dev.sum7.eu/genofire/yaja/bot" "dev.sum7.eu/genofire/yaja/xmpp" "dev.sum7.eu/genofire/yaja/xmpp/base" ) type botConfig struct { LogLevel log.Level `toml:"log_level"` JID *xmppbase.JID `toml:"jid"` Password string `toml:"password"` } // BotCMD represents the serve command var BotCMD = &cobra.Command{ Use: "bot", Short: "runs xmpp ping bot", Example: "yaja bot -c /etc/yaja.conf", Run: func(cmd *cobra.Command, args []string) { config := &botConfig{} if err := file.ReadTOML(configPath, config); err != nil { log.Fatal("unable to load config file:", err) } log.SetLevel(config.LogLevel) log.Infof("yaja bot starting with jid: %s", config.JID.String()) pingBog := xmppbot.NewBot(config.JID, config.Password) pingBog.AutoSubscribe = true botCounter := 0 pingBog.Handlers = append(pingBog.Handlers, &xmppbot.CommandMessageHander{Commands: map[string]func(*xmppbot.Bot, *xmpp.MessageClient, string){ // reaction on command ping "ping": func(bot *xmppbot.Bot, msg *xmpp.MessageClient, args string) { bot.Answer(msg, "pong") }, // reaction on command time "time": func(bot *xmppbot.Bot, msg *xmpp.MessageClient, args string) { bot.Answer(msg, time.Now().String()) }, "inc": func(bot *xmppbot.Bot, msg *xmpp.MessageClient, args string) { botCounter += 1 bot.Answer(msg, fmt.Sprintf("counter: %d", botCounter)) }, }}) go pingBog.Start() log.Infoln("yaja bot started") // Wait for INT/TERM sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) sig := <-sigs log.Println("received", sig) }, } func init() { BotCMD.Flags().StringVarP(&configPath, "config", "c", "yaja-bot.conf", "path to configuration file") RootCMD.AddCommand(BotCMD) }