Browse Source

Add initial bot

master
Matthew Faltys 8 years ago
commit
1f0cf3fc2e
  1. BIN
      .config.gcfg.swp
  2. 8
      config.gcfg
  3. BIN
      lorebot/.sbot.go.swp
  4. 121
      lorebot/lorebot.go

BIN
.config.gcfg.swp

Binary file not shown.

8
config.gcfg

@ -0,0 +1,8 @@
[lorebot]
loglevel = "debug"
bootstrapdelay = 1
APIToken = ""
[redis]
host = "localhost:6379"
password = ""

BIN
lorebot/.sbot.go.swp

Binary file not shown.

121
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
}
Loading…
Cancel
Save