PublicPrintBot/handler.go

97 lines
2.9 KiB
Go
Raw Normal View History

2024-09-18 13:18:58 +00:00
package main
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"strconv"
)
func (ctx *Context) handleUpdate(update tgbotapi.Update) {
if update.Message != nil {
ctx.messageHandler(update)
}
if update.CallbackQuery != nil {
ctx.callbackQueryHandler(update)
}
}
func (ctx *Context) callbackQueryHandler(update tgbotapi.Update) {
if !ctx.isAuth(update.CallbackQuery.From.ID) || len(update.CallbackQuery.Data) < 3 {
ctx.callbackQuery(update.CallbackQuery.ID, "❌ Unauthorized")
return
}
if update.CallbackQuery.Data == "next" {
ctx.Next <- update.CallbackQuery.From.ID
ctx.callbackQuery(update.CallbackQuery.ID, "✅ Confirmed")
return
}
queryType := update.CallbackQuery.Data[0]
queryCmd := rune(update.CallbackQuery.Data[1])
queryID, err := strconv.Atoi(update.CallbackQuery.Data[2:])
if err != nil {
return
}
if queryType == 'w' {
ctx.editWip(queryCmd, queryID, update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.MessageID, update.CallbackQuery.ID)
}
}
func (ctx *Context) messageHandler(update tgbotapi.Update) {
if ctx.isAuth(update.Message.From.ID) {
if update.Message.Sticker != nil {
ctx.createProject(update.Message.Sticker.FileID, update.Message.From.ID)
}
if update.Message.Photo != nil {
if len(update.Message.Photo) > 0 {
ctx.createProject(update.Message.Photo[len(update.Message.Photo)-1].FileID, update.Message.From.ID)
}
}
if update.Message.Document != nil {
ctx.createProject(update.Message.Document.FileID, update.Message.From.ID)
}
} else {
if update.Message.Sticker != nil || update.Message.Photo != nil || update.Message.Document != nil {
ctx.sendMessage(update.Message.From.ID, "🛑 You currently <b>don't have the authorization to print.</b> Check for a passcode near the printer and send it here as a message.")
}
}
if update.Message.Text == "/start" {
ctx.startHandler(update)
}
if update.Message.Text == ctx.Settings.Password && !ctx.isAuth(update.Message.From.ID) {
ctx.PassedPasswordCheck = append(ctx.PassedPasswordCheck, update.Message.From.ID)
ctx.sendMessage(update.Message.From.ID, "✅ Authorized")
}
if update.Message.From.ID == ctx.Settings.Admin && update.Message.Text == "/next" {
ctx.Next <- update.Message.From.ID
ctx.sendMessage(update.Message.From.ID, "sent, left: "+strconv.Itoa(len(ctx.Next)))
}
}
func (ctx *Context) startHandler(update tgbotapi.Update) {
txt := "👋 <b>Welcome to PublicPrintBot!</b> Send me a sticker, a photo or file and I will print it for you."
if !ctx.isAuth(update.Message.From.ID) {
txt += "\n\n🛑 You currently <b>don't have the authorization to print.</b> Check for a passcode near the printer and send it here as a message."
}
kb := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonURL("📚 Code", "https://git.massivebox.net/massivebox/publicprintbot"),
),
)
ctx.sendMessage(update.Message.From.ID, txt, kb)
}