Files
PlexAutomatization/telegram-bot/telegram-bot.go

127 lines
3.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"log"
"strings"
"time"
plex "nikozavr.ru/plexautomatization/grpc"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
grpc "google.golang.org/grpc"
)
const (
address = "localhost:5000"
defaultName = "world"
)
func main() {
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := plex.NewAutomatizatorClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.AddRequest(ctx, &plex.RequestInfo{UserId: 213})
if err != nil {
log.Fatalf("could not exec: %v", err)
}
log.Printf("Result: %v", r.Message)
bot, err := tgbotapi.NewBotAPI("1518526346:AAEA5BpB3iZRsiBSYJZBnWBiOqeWrFT59rg")
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
userLists := make(map[int][]string)
processingList := make(map[int]bool)
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
log.Println(userLists)
processing, ok := processingList[update.Message.From.ID]
if !ok {
processingList[update.Message.From.ID] = false
processing = false
}
if processing {
list, ok := userLists[update.Message.From.ID]
if !ok {
userLists[update.Message.From.ID] = []string{update.Message.Text}
} else {
userLists[update.Message.From.ID] = append(list, update.Message.Text)
}
processingList[update.Message.From.ID] = false
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Запрос добавлен")
bot.Send(msg)
continue
}
if !update.Message.IsCommand() {
sticker := tgbotapi.NewStickerShare(update.Message.Chat.ID, "CAACAgIAAxkBAAEBzqVgDFwGwth9LYIyjZKcbPyYjec95gACAgEAAladvQpO4myBy0Dk_x4E")
bot.Send(sticker)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Не понимаю Вас( Попробуйте использовать команды.")
bot.Send(msg)
continue
}
answer := tgbotapi.NewMessage(update.Message.Chat.ID, "")
switch update.Message.Command() {
case "add":
trimmedCommandArguments := strings.TrimSpace(update.Message.CommandArguments())
if trimmedCommandArguments == "" {
processingList[update.Message.From.ID] = true
answer.Text = "Введите название фильма или сериала, который вы хотели бы добавить в библиотеку Plex."
} else {
list, ok := userLists[update.Message.From.ID]
if !ok {
userLists[update.Message.From.ID] = []string{trimmedCommandArguments}
} else {
userLists[update.Message.From.ID] = append(list, trimmedCommandArguments)
}
answer.Text = "Запрос добавлен"
}
case "list":
list, ok := userLists[update.Message.From.ID]
if !ok || len(list) == 0 {
answer.Text = "Список пуст"
} else {
answer.Text = "Список запросов:\n" + strings.Join(list, "\n")
}
default:
answer.Text = "Команда не распознана. Попробуйте ещё раз."
}
if _, err := bot.Send(answer); err != nil {
log.Panic(err)
}
}
}