go_telegram/main.go

145 lines
2.7 KiB
Go
Raw Normal View History

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-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 {
// StreamCount int `json:"stream_count"`
// StreamCountDirectPlay int `json:"stream_count_direct_play"`
// StreamCountDirectStream int `json:"stream_count_direct_stream"`
// StreamCountTranscode int `json:"stream_count_transcode"`
// }
// type Response struct {
// Data Activity
// }
2018-07-19 16:03:25 +00:00
type Activity struct {
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)
}
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!")
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
}