From 1f0cf3fc2ea33de2bc1db1bb5bf78fd1431335c0 Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Tue, 30 May 2017 11:30:25 -0500 Subject: [PATCH] Add initial bot --- .config.gcfg.swp | Bin 0 -> 12288 bytes config.gcfg | 8 +++ lorebot/.sbot.go.swp | Bin 0 -> 12288 bytes lorebot/lorebot.go | 121 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 .config.gcfg.swp create mode 100644 config.gcfg create mode 100644 lorebot/.sbot.go.swp create mode 100644 lorebot/lorebot.go diff --git a/.config.gcfg.swp b/.config.gcfg.swp new file mode 100644 index 0000000000000000000000000000000000000000..e045245626f4815eed8c4ed186441023768b200f GIT binary patch literal 12288 zcmeI&ze~eF6bJBECqYEff8dO(ZBh$bg@S@h2f;xLA|3MMk`^z$kh>~f?BKuRBItkN z=wINU;o$6xTtOTP9gE+C$K!G@?~)H>2uT$5y62+Z2#Cif(eq>dy0`XDpC?3l8Y(+6 z)ymxA>Vlv(w~QWKp?{|1aV|&J_$Dt@(ulP6V?9cTStEN5pHyZ=aCQtZS3q&l{*VmdP>KksyZAWv<{Cv zx+!HcG=1W}rx)EzeJe+^-fp`{Yin#77Kv2hq}r~@#Y$&N-bwYVIFZpfH(@S_=RG literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..9ee8bddc48098cd2364b06942f4421e80ca6d69f GIT binary patch literal 12288 zcmeI2%ZnUE9LFmO#*;&bIR+rsPFd#{Kx@&ri?Wr32 zF}rKB27KJSC?3oq!3W|&qJsVd5_}%?Br51ZFQSNuf(Je5x2n2#dS{aj@noO|J~LCV zU;XM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sDqr}wO+ zc@Qv!cj%cDhf`Fqt7C+B2YF_E5m|e$w%SX(I!b$XjXxFZ0fGaWqRD964_RS(ZAQQp zdT8bLz(%V}NUvvX#a$&JI%mAiyqTD>PQQ8wrOc4V{7!?#{t_LerYnO_p&45$T9E`< zt?D_?q@b%kAEH^Zgm9pKx~4>~H~49FIAw%4Tei2~?9FhbZhN zStY(oLZMQ?bgm@hHl#6$7?TyPqjXIBb84A%{e-$J&jaP-3pJMx4wwpB{n%61OzU-` zl$&#>@_KnXiU*{!tFu~iv+IpK@%3|y+;RT3nq(*qVBa=*t*ufB$fR*IrU*S-|iU0rr literal 0 HcmV?d00001 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 +}