Adding more function

This commit is contained in:
Matt Burchett 2018-07-18 14:43:32 -05:00
parent bdcc985f7a
commit 974e92a96d
3 changed files with 77 additions and 19 deletions

View File

@ -1,3 +1,5 @@
{
"Token": "546410284:AAHXKhEsAB5OZTrAXCFI4kiZSFjN5i0h_wk"
"Token": "546410284:AAHXKhEsAB5OZTrAXCFI4kiZSFjN5i0h_wk",
"SonarrAPIURL": "http://dvr.linuxrocker.com/tv/api/",
"SonarrAPIKey": "69e7a095a1c94f42b150380864a6c92a"
}

Binary file not shown.

92
main.go
View File

@ -3,19 +3,75 @@ package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"time"
tb "gopkg.in/tucnak/telebot.v2"
"github.com/yanzay/tbot"
)
// Configuration - Specify what to look for in Config file
type Configuration struct {
Token string
// Config - Specify what to look for in Config file
var Config struct {
BotToken string
SonarrAPIURL string
SonarrAPIKey string
PlexPyAPIURL string
PlexPyAPIKey string
RadarrAPIURL string
RadarrAPIKey string
CouchPotatoAPIURL string
CouchPotatoAPIKey string
PlexAPIURL string
PlexAPIKey string
}
// ReadConfig from file
func sonarrStatus(message *tbot.Message) {
response, err := http.Get(Config.SonarrAPIURL + "system/status?apikey=" + Config.SonarrAPIKey)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
message.Replyf("%s", responseData)
}
func sonarrVersion(message *tbot.Message) {
response, err := http.Get(Config.SonarrAPIURL + "system/status?apikey=" + Config.SonarrAPIKey)
if err != nil {
log.Fatal(err)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
type Version struct {
Version string `json:"version"`
}
version := Version{}
jsonErr := json.Unmarshal(responseData, &version)
if jsonErr != nil {
log.Fatal(jsonErr)
}
message.Replyf("%s", version.Version)
}
// func activeSteamers(message *tbot.Message) {
// response, err := http.Get(Config.PlexAPIURL + "api/v2?apikey=" + Config.PlexAPIKey + "&cmd=")
// }
func main() {
c := flag.String("c", "./config.json", "Specify the configuration file.")
flag.Parse()
@ -25,27 +81,27 @@ func main() {
}
defer file.Close()
decoder := json.NewDecoder(file)
Config := Configuration{}
err = decoder.Decode(&Config)
if err != nil {
log.Fatal("can't decode config JSON: ", err)
}
b, err := tb.NewBot(tb.Settings{
Token: Config.Token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
bot, err := tbot.NewServer(Config.BotToken)
if err != nil {
log.Fatal(err)
return
}
b.Handle("/ping", func(m *tb.Message) {
b.Send(m.Sender, "pong")
})
whitelist := []string{"WARBIRD199"}
bot.AddMiddleware(tbot.NewAuth(whitelist))
log.Print("Starting bot...")
bot.Handle("/ping", "pong!")
bot.HandleFunc("/sonarr_status", sonarrStatus)
bot.HandleFunc("/sonarr_version", sonarrVersion)
// Start Listening
err = bot.ListenAndServe()
log.Fatal(err)
b.Start()
}