Adding Sonarr support into the code base.
This commit is contained in:
@ -1,31 +1,36 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/yanzay/tbot/v2"
|
||||
)
|
||||
|
||||
func (tb *Bot) myID(m *tbot.Message) {
|
||||
if tb.AdminCheck(m) {
|
||||
if tb.adminCheck(m.From.ID, false) {
|
||||
tb.Client.SendMessage(m.Chat.ID, strconv.Itoa(m.From.ID))
|
||||
fmt.Println(m.From.ID)
|
||||
return
|
||||
}
|
||||
|
||||
tb.Client.SendMessage(m.Chat.ID, "You are not an authorized admin.")
|
||||
}
|
||||
|
||||
func (tb *Bot) chatID(m *tbot.Message) {
|
||||
tb.Client.SendMessage(m.Chat.ID, m.Chat.ID)
|
||||
if tb.adminCheck(m.From.ID, false) {
|
||||
tb.Client.SendMessage(m.Chat.ID, m.Chat.ID)
|
||||
return
|
||||
}
|
||||
|
||||
tb.Client.SendMessage(m.Chat.ID, "You are not an authorized admin.")
|
||||
}
|
||||
|
||||
// AdminCheck checks for valid bot admins.
|
||||
func (tb *Bot) AdminCheck(m *tbot.Message) bool {
|
||||
// adminCheck checks for valid bot admins.
|
||||
func (tb *Bot) adminCheck(id int, callback bool) bool {
|
||||
for _, admin := range tb.Config.Telegram.Admins {
|
||||
if m.From.ID == admin {
|
||||
if id == admin {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
tb.Client.SendMessage(m.Chat.ID, "You are not an authorized admin.")
|
||||
|
||||
return false
|
||||
}
|
||||
|
@ -2,22 +2,71 @@ package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mattburchett/go_telegram/pkg/service/sonarr"
|
||||
|
||||
"github.com/yanzay/tbot/v2"
|
||||
)
|
||||
|
||||
// Sonarr Search
|
||||
func (tb *Bot) sonarrSearch(m *tbot.Message) {
|
||||
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.")
|
||||
return
|
||||
}
|
||||
|
||||
request, err := sonarr.Search(m, tb.Config)
|
||||
if err != nil {
|
||||
tb.Client.SendMessage(m.Chat.ID, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
inlineResponse := make([][]tbot.InlineKeyboardButton, 0)
|
||||
for _, i := range request {
|
||||
inlineResponse = append(inlineResponse, []tbot.InlineKeyboardButton{{
|
||||
Text: i.Button,
|
||||
CallbackData: "tv_" + i.Callback,
|
||||
}})
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
// sonarrAdd will perform the add requests to Sonarr.
|
||||
func (tb *Bot) sonarrAdd(cq *tbot.CallbackQuery) {
|
||||
if strings.Contains(cq.Data, "+") {
|
||||
if tb.adminCheck(cq.From.ID, true) {
|
||||
tb.Client.SendMessage(tb.CallbackChatID, sonarr.Add(cq.Data, tb.Config))
|
||||
} else {
|
||||
tb.Client.AnswerCallbackQuery(cq.ID, tbot.OptText("This request is over the season limit."))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
tb.Client.SendMessage(tb.CallbackChatID, sonarr.Add(cq.Data, tb.Config))
|
||||
|
||||
}
|
||||
|
||||
// Admin Functions
|
||||
|
||||
// sonarrStatus queries Sonarr for it's system status information.
|
||||
func (tb *Bot) sonarrStatus(m *tbot.Message) {
|
||||
if tb.AdminCheck(m) {
|
||||
request, err := sonarr.SonarrStatus(m, tb.Config)
|
||||
if tb.adminCheck(m.From.ID, false) {
|
||||
request, err := sonarr.Status(m, tb.Config)
|
||||
|
||||
if err != nil {
|
||||
tb.Client.SendMessage(m.Chat.ID, fmt.Sprintf("%v: \n %v", request, err))
|
||||
} else {
|
||||
tb.Client.SendMessage(m.Chat.ID, "Sonarr Status:")
|
||||
tb.Client.SendMessage(m.Chat.ID, request)
|
||||
return
|
||||
}
|
||||
|
||||
tb.Client.SendMessage(m.Chat.ID, "Sonarr Status:")
|
||||
tb.Client.SendMessage(m.Chat.ID, request)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package telegram
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mattburchett/go_telegram/pkg/core/config"
|
||||
@ -17,6 +18,15 @@ type Bot struct {
|
||||
CallbackMessageID int
|
||||
}
|
||||
|
||||
// Stat middleware.
|
||||
func stat(h tbot.UpdateHandler) tbot.UpdateHandler {
|
||||
return func(u *tbot.Update) {
|
||||
start := time.Now()
|
||||
h(u)
|
||||
log.Printf("Handle time: %v", time.Now().Sub(start))
|
||||
}
|
||||
}
|
||||
|
||||
// New creates an active telegram bot and loads the handlers.
|
||||
func (tb *Bot) New(token string) {
|
||||
tb.Bot = tbot.New(token)
|
||||
@ -35,7 +45,8 @@ func (tb *Bot) Handler() {
|
||||
tb.Client.SendMessage(m.Chat.ID, "pong")
|
||||
})
|
||||
|
||||
// sonarr/admin.go
|
||||
// telegram/sonar.go
|
||||
tb.Bot.HandleMessage("/s", tb.sonarrSearch)
|
||||
tb.Bot.HandleMessage("/admin sonarrStatus", tb.sonarrStatus)
|
||||
|
||||
// telegram/testhandler.go
|
||||
@ -53,19 +64,20 @@ func (tb *Bot) Handler() {
|
||||
tb.Bot.HandleCallback(tb.callbackHandler)
|
||||
}
|
||||
|
||||
// Stat middleware.
|
||||
func stat(h tbot.UpdateHandler) tbot.UpdateHandler {
|
||||
return func(u *tbot.Update) {
|
||||
start := time.Now()
|
||||
h(u)
|
||||
log.Printf("Handle time: %v", time.Now().Sub(start))
|
||||
}
|
||||
}
|
||||
|
||||
// callbackHandler handles callbacks.
|
||||
func (tb *Bot) callbackHandler(cq *tbot.CallbackQuery) {
|
||||
tb.Client.EditMessageText(tb.CallbackChatID, tb.CallbackMessageID, "Callback received.")
|
||||
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) {
|
||||
|
Reference in New Issue
Block a user