2018-07-16 18:44:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
2018-07-18 19:43:32 +00:00
|
|
|
"io/ioutil"
|
2018-07-16 18:44:27 +00:00
|
|
|
"log"
|
2018-07-18 19:43:32 +00:00
|
|
|
"net/http"
|
2018-07-16 18:44:27 +00:00
|
|
|
"os"
|
|
|
|
|
2018-07-18 19:43:32 +00:00
|
|
|
"github.com/yanzay/tbot"
|
2018-07-16 18:44:27 +00:00
|
|
|
)
|
|
|
|
|
2018-07-19 16:05:12 +00:00
|
|
|
// Config - Set layout
|
2018-07-18 19:43:32 +00:00
|
|
|
var Config struct {
|
2018-07-19 16:05:12 +00:00
|
|
|
BotToken string
|
|
|
|
SonarrAPIURL string
|
|
|
|
SonarrAPIKey string
|
|
|
|
PlexPyAPIURL string
|
|
|
|
PlexPyAPIKey string
|
|
|
|
RadarrAPIURL string
|
|
|
|
RadarrAPIKey string
|
|
|
|
CouchPotatoAPIURL string
|
|
|
|
CouchPotatoAPIKey string
|
|
|
|
PlexAPIURL string
|
|
|
|
PlexAPIKey string
|
2018-07-16 18:44:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:43:32 +00:00
|
|
|
func sonarrStatus(message *tbot.Message) {
|
2018-07-19 16:03:25 +00:00
|
|
|
r, err := http.Get(Config.SonarrAPIURL + "system/status?apikey=" + Config.SonarrAPIKey)
|
2018-07-18 19:43:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:03:25 +00:00
|
|
|
rd, err := ioutil.ReadAll(r.Body)
|
2018-07-18 19:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:03:25 +00:00
|
|
|
message.Replyf("%s", rd)
|
2018-07-18 19:43:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func sonarrVersion(message *tbot.Message) {
|
2018-07-19 16:03:25 +00:00
|
|
|
r, err := http.Get(Config.SonarrAPIURL + "system/status?apikey=" + Config.SonarrAPIKey)
|
2018-07-18 19:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:03:25 +00:00
|
|
|
rd, err := ioutil.ReadAll(r.Body)
|
2018-07-18 19:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Version struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:03:25 +00:00
|
|
|
v := Version{}
|
|
|
|
jv := json.Unmarshal(rd, &v)
|
|
|
|
if jv != nil {
|
|
|
|
log.Fatal(jv)
|
2018-07-18 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 16:03:25 +00:00
|
|
|
message.Replyf("%s", v.Version)
|
2018-07-18 19:43:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-20 19:33:28 +00:00
|
|
|
func sayHandler(message *tbot.Message) {
|
|
|
|
message.Reply("You said: " + message.Vars["text"])
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:03:25 +00:00
|
|
|
func activeSteamers(message *tbot.Message) {
|
|
|
|
r, err := http.Get(Config.PlexPyAPIURL + "?apikey=" + Config.PlexPyAPIKey + "&cmd=get_activity")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rd, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Activity struct {
|
2018-07-20 14:04:05 +00:00
|
|
|
Response struct {
|
|
|
|
Data struct {
|
|
|
|
StreamCount string `json:"stream_count"`
|
|
|
|
} `json:"data"`
|
|
|
|
} `json:"response"`
|
2018-07-19 16:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a := Activity{}
|
|
|
|
ja := json.Unmarshal(rd, &a)
|
|
|
|
if ja != nil {
|
|
|
|
log.Fatal(ja)
|
|
|
|
}
|
|
|
|
|
2018-07-20 14:04:05 +00:00
|
|
|
message.Replyf("Stream Count: %v", a.Response.Data.StreamCount)
|
2018-07-19 16:03:25 +00:00
|
|
|
}
|
2018-07-18 19:43:32 +00:00
|
|
|
|
2018-07-16 18:44:27 +00:00
|
|
|
func main() {
|
|
|
|
c := flag.String("c", "./config.json", "Specify the configuration file.")
|
|
|
|
flag.Parse()
|
|
|
|
file, err := os.Open(*c)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("can't open config file: ", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
err = decoder.Decode(&Config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("can't decode config JSON: ", err)
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:43:32 +00:00
|
|
|
bot, err := tbot.NewServer(Config.BotToken)
|
2018-07-16 18:44:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:43:32 +00:00
|
|
|
whitelist := []string{"WARBIRD199"}
|
|
|
|
bot.AddMiddleware(tbot.NewAuth(whitelist))
|
|
|
|
|
|
|
|
bot.Handle("/ping", "pong!")
|
|
|
|
|
2018-07-20 19:33:28 +00:00
|
|
|
bot.HandleFunc("/say {text}", sayHandler)
|
|
|
|
|
2018-07-18 19:43:32 +00:00
|
|
|
bot.HandleFunc("/sonarr_status", sonarrStatus)
|
|
|
|
|
|
|
|
bot.HandleFunc("/sonarr_version", sonarrVersion)
|
2018-07-16 18:44:27 +00:00
|
|
|
|
2018-07-19 16:03:25 +00:00
|
|
|
bot.HandleFunc("/activity", activeSteamers)
|
|
|
|
|
2018-07-18 19:43:32 +00:00
|
|
|
// Start Listening
|
|
|
|
err = bot.ListenAndServe()
|
|
|
|
log.Fatal(err)
|
2018-07-16 18:44:27 +00:00
|
|
|
|
|
|
|
}
|