You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.8 KiB
122 lines
2.8 KiB
8 years ago
|
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
|
||
|
}
|