Adding the start to couchpotato and some middleware

This commit is contained in:
Matt Burchett 2020-05-24 22:29:08 -05:00
parent 69de0397a3
commit 5d4f13827c
7 changed files with 106 additions and 53 deletions

View File

@ -10,17 +10,23 @@ import (
// Config - This struct will hold configuration components.
type Config struct {
Telegram struct {
Token string `json:"token"`
ChatID string `json:"chatID"`
Admins []int `json:"admins"`
Token string `json:"token"`
ChatID string `json:"chatID"`
Admins []int `json:"admins"`
AuthorizedChats []string `json:"authorizedChats"`
} `json:"telegram"`
Sonarr struct {
URL string `json:"url"`
APIKey string `json:"apiKey"`
SeasonLimit int `json:"seasonLimit"`
ProfileID int `json:"proFileId"`
ProfileID int `json:"profileId"`
} `json:"sonarr"`
CouchPotato struct {
URL string `json:"url"`
APIKey string `json:"apiKey"`
ProfileID string `json:"profileId`
} `json:"couchpotato"`
}
//GetConfig gets the configuration values for the api using the file in the supplied configPath.

View File

@ -0,0 +1 @@
package couchpotato

View File

@ -0,0 +1,11 @@
package couchpotato
import (
"github.com/mattburchett/go_telegram/pkg/core/config"
"github.com/yanzay/tbot/v2"
)
// Search performs the lookup actions within CouchPotato
func Search(m *tbot.Message, config config.Config) ([]response, error) {
}

View File

@ -178,7 +178,7 @@ func Add(callback string, config config.Config) string {
if err != nil {
return err.Error()
}
rootFolderData, err := ioutil.ReadAll(rootFolderLookuQuery.Body)
rootFolderData, err := ioutil.ReadAll(rootFolderLookupQuery.Body)
if err != nil {
return err.Error()
}

View File

@ -0,0 +1,70 @@
package telegram
import (
"strings"
"github.com/yanzay/tbot/v2"
)
// Handler creates the active Telegram handlers.
func (tb *Bot) Handler() {
// Bot Healthcheck
tb.Bot.HandleMessage("/ping", func(m *tbot.Message) {
tb.Client.SendMessage(m.Chat.ID, "pong")
})
// telegram/sonar.go
tb.Bot.HandleMessage("/s", tb.sonarrSearch)
tb.Bot.HandleMessage("/admin sonarrStatus", tb.sonarrStatus)
// telegram/testhandler.go
tb.Bot.HandleMessage("/test", tb.testHandler)
// telegram/admin.go
tb.Bot.HandleMessage("/admin myID", tb.myID)
tb.Bot.HandleMessage("/admin chatID", tb.chatID)
// Help
tb.Bot.HandleMessage("/help$", tb.helpHandler)
tb.Bot.HandleMessage("/h$", tb.helpHandler)
// Callback Handler
tb.Bot.HandleCallback(tb.callbackHandler)
}
// callbackHandler handles callbacks.
func (tb *Bot) callbackHandler(cq *tbot.CallbackQuery) {
go func() {
tb.Client.AnswerCallbackQuery(cq.ID, tbot.OptText("Request received."))
tb.Client.DeleteMessage(tb.CallbackChatID, tb.CallbackMessageID)
}()
if strings.Contains(cq.Data, "tv_") {
tb.sonarrAdd(cq)
return
}
tb.Client.SendMessage(tb.CallbackChatID, cq.Data)
}
func (tb *Bot) helpHandler(m *tbot.Message) {
if !tb.whitelistHandler(m) {
return
}
tb.Client.SendMessage(m.Chat.ID, "USAGE:\n\n/movie <Movie Name> or /m <Movie Name>\n/show <TV Show Name> or /s <TV Show Name>\n\nEXAMPLES:\n\n/s The Walking Dead\n/m Avatar")
}
func (tb *Bot) whitelistHandler(m *tbot.Message) bool {
for _, id := range tb.Config.Telegram.AuthorizedChats {
if id == m.Chat.ID {
return true
}
}
tb.Client.SendMessage(m.Chat.ID, "This bot is not authorized for use in this chat.")
return false
}

View File

@ -11,6 +11,10 @@ import (
// Sonarr Search
func (tb *Bot) sonarrSearch(m *tbot.Message) {
if !tb.whitelistHandler(m) {
return
}
text := strings.TrimPrefix(strings.TrimPrefix(m.Text, "/s"), " ")
if len(text) == 0 {
tb.Client.SendMessage(m.Chat.ID, "You must specify a show. Type /help for help.")
@ -31,6 +35,11 @@ func (tb *Bot) sonarrSearch(m *tbot.Message) {
}})
}
if len(request) == 0 {
tb.Client.SendMessage(m.Chat.ID, "No results found, try harder.")
return
}
response, _ := tb.Client.SendMessage(m.Chat.ID, "Please select the show you would like to download.", tbot.OptInlineKeyboardMarkup(&tbot.InlineKeyboardMarkup{InlineKeyboard: inlineResponse}))
tb.CallbackMessageID = response.MessageID
tb.CallbackChatID = m.Chat.ID
@ -56,6 +65,10 @@ func (tb *Bot) sonarrAdd(cq *tbot.CallbackQuery) {
// sonarrStatus queries Sonarr for it's system status information.
func (tb *Bot) sonarrStatus(m *tbot.Message) {
if !tb.whitelistHandler(m) {
return
}
if tb.adminCheck(m.From.ID, false) {
request, err := sonarr.Status(m, tb.Config)

View File

@ -2,7 +2,6 @@ package telegram
import (
"log"
"strings"
"time"
"github.com/mattburchett/go_telegram/pkg/core/config"
@ -36,50 +35,3 @@ func (tb *Bot) New(token string) {
tb.Bot.Start()
}
// Handler creates the active Telegram handlers.
func (tb *Bot) Handler() {
// Bot Healthcheck
tb.Bot.HandleMessage("/ping", func(m *tbot.Message) {
tb.Client.SendMessage(m.Chat.ID, "pong")
})
// telegram/sonar.go
tb.Bot.HandleMessage("/s", tb.sonarrSearch)
tb.Bot.HandleMessage("/admin sonarrStatus", tb.sonarrStatus)
// telegram/testhandler.go
tb.Bot.HandleMessage("/test", tb.testHandler)
// telegram/admin.go
tb.Bot.HandleMessage("/admin myID", tb.myID)
tb.Bot.HandleMessage("/admin chatID", tb.chatID)
// Help
tb.Bot.HandleMessage("/help$", tb.helpHandler)
tb.Bot.HandleMessage("/h$", tb.helpHandler)
// Callback Handler
tb.Bot.HandleCallback(tb.callbackHandler)
}
// callbackHandler handles callbacks.
func (tb *Bot) callbackHandler(cq *tbot.CallbackQuery) {
go func() {
tb.Client.AnswerCallbackQuery(cq.ID, tbot.OptText("Request received."))
tb.Client.DeleteMessage(tb.CallbackChatID, tb.CallbackMessageID)
}()
if strings.Contains(cq.Data, "tv_") {
tb.sonarrAdd(cq)
return
}
tb.Client.SendMessage(tb.CallbackChatID, cq.Data)
}
func (tb *Bot) helpHandler(m *tbot.Message) {
tb.Client.SendMessage(m.Chat.ID, "USAGE:\n\n/movie <Movie Name> or /m <Movie Name>\n/show <TV Show Name> or /s <TV Show Name>\n\nEXAMPLES:\n\n/s The Walking Dead\n/m Avatar")
}