commit 1f0cf3fc2ea33de2bc1db1bb5bf78fd1431335c0 Author: Matthew Faltys Date: Tue May 30 11:30:25 2017 -0500 Add initial bot diff --git a/.config.gcfg.swp b/.config.gcfg.swp new file mode 100644 index 0000000..e045245 Binary files /dev/null and b/.config.gcfg.swp differ diff --git a/config.gcfg b/config.gcfg new file mode 100644 index 0000000..d039490 --- /dev/null +++ b/config.gcfg @@ -0,0 +1,8 @@ +[lorebot] + loglevel = "debug" + bootstrapdelay = 1 + APIToken = "" + +[redis] + host = "localhost:6379" + password = "" diff --git a/lorebot/.sbot.go.swp b/lorebot/.sbot.go.swp new file mode 100644 index 0000000..9ee8bdd Binary files /dev/null and b/lorebot/.sbot.go.swp differ diff --git a/lorebot/lorebot.go b/lorebot/lorebot.go new file mode 100644 index 0000000..260f061 --- /dev/null +++ b/lorebot/lorebot.go @@ -0,0 +1,121 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strings" + "time" + + "github.com/nlopes/slack" + "github.com/unixvoid/glogger" + "gopkg.in/gcfg.v1" + "gopkg.in/redis.v5" +) + +type Config struct { + Lorebot struct { + Loglevel string + BootstrapDelay time.Duration + APIToken string + } + Redis struct { + Host string + Password string + } +} + +var ( + config = Config{} +) + +func main() { + // read in config file + readConf() + + // initialize the logger with the configured loglevel + initLogger(config.Lorebot.Loglevel) + + // initialize redis connection + //redisClient, err := initRedisConnection() + //if err != nil { + // glogger.Debug.Printf("redis conneciton cannot be made, trying again in %d seconds", config.Lorebot.BootstrapDelay) + // time.Sleep(config.Lorebot.BootstrapDelay * time.Second) + // redisClient, err = initRedisConnection() + // if err != nil { + // glogger.Error.Println("redis connection cannot be made.") + // os.Exit(1) + // } + //} + //glogger.Debug.Println("connection to redis succeeded.") + //glogger.Info.Println("link to redis on", config.Redis.Host) + + api := slack.New(config.Lorebot.APIToken) + //api.SetDebug(true) + + rtm := api.NewRTM() + go rtm.ManageConnection() + +Loop: + for { + select { + case msg := <-rtm.IncomingEvents: + switch ev := msg.Data.(type) { + //case *slack.ConnectedEvent: + // //botId = ev.Info.User.ID + //case *slack.TeamJoinEvent: + // // Handle new user to client + case *slack.MessageEvent: + if ev.Type == "message" { + if ev.Text == "test" { + rtm.SendMessage(rtm.NewOutgoingMessage("MY NAME IS ..JHEFFFFF", ev.Channel)) + } + if strings.Contains(ev.Text, "lore") { + rtm.SendMessage(rtm.NewOutgoingMessage("THA LORE", ev.Channel)) + } + } + case *slack.RTMError: + fmt.Printf("Error: %s\n", ev.Error()) + case *slack.InvalidAuthEvent: + fmt.Printf("Invalid credentials") + break Loop + default: + //fmt.Printf("Unknown error") + //fmt.Printf("%v\n", msg.Data) + } + } + } +} + +func readConf() { + // init config file + err := gcfg.ReadFileInto(&config, "config.gcfg") + if err != nil { + panic(fmt.Sprintf("Could not load config.gcfg, error: %s\n", err)) + } +} + +func initLogger(logLevel string) { + // init logger + if logLevel == "debug" { + glogger.LogInit(os.Stdout, os.Stdout, os.Stdout, os.Stderr) + } else if logLevel == "cluster" { + glogger.LogInit(os.Stdout, os.Stdout, ioutil.Discard, os.Stderr) + } else if logLevel == "info" { + glogger.LogInit(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr) + } else { + glogger.LogInit(ioutil.Discard, ioutil.Discard, ioutil.Discard, os.Stderr) + } +} + +func initRedisConnection() (*redis.Client, error) { + // init redis connection + redisClient := redis.NewClient(&redis.Options{ + Addr: config.Redis.Host, + Password: config.Redis.Password, + DB: 0, + }) + + _, redisErr := redisClient.Ping().Result() + return redisClient, redisErr +}