203 lines
4.4 KiB
Go
203 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"git.massivebox.net/massivebox/go-catprinter"
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
_ "golang.org/x/image/webp"
|
|
"image"
|
|
_ "image/gif"
|
|
_ "image/jpeg"
|
|
"image/png"
|
|
_ "image/png"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func (ctx *Context) sendMessage(chatID int64, text string, keyboard ...tgbotapi.InlineKeyboardMarkup) int {
|
|
|
|
msg := tgbotapi.NewMessage(chatID, text)
|
|
msg.ParseMode = tgbotapi.ModeHTML
|
|
if len(keyboard) > 0 {
|
|
msg.ReplyMarkup = &keyboard[0]
|
|
}
|
|
|
|
sent, err := ctx.Bot.Send(msg)
|
|
if err != nil {
|
|
log.Println("Couldn't send message to user", chatID, err.Error())
|
|
}
|
|
|
|
return sent.MessageID
|
|
|
|
}
|
|
|
|
func (ctx *Context) sendImage(chatID int64, img image.Image, keyboard ...tgbotapi.InlineKeyboardMarkup) {
|
|
|
|
fImg, err := imageToFileBytes(img)
|
|
if err != nil {
|
|
log.Println("Couldn't encode image to send", chatID, err.Error())
|
|
return
|
|
}
|
|
|
|
msg := tgbotapi.NewPhoto(chatID, fImg)
|
|
if len(keyboard) > 0 {
|
|
msg.ReplyMarkup = &keyboard[0]
|
|
}
|
|
|
|
_, err = ctx.Bot.Send(msg)
|
|
if err != nil {
|
|
log.Println("Couldn't send image to user", chatID, err.Error())
|
|
}
|
|
|
|
}
|
|
|
|
func (ctx *Context) editImage(chatID int64, msgId int, img image.Image, keyboard ...tgbotapi.InlineKeyboardMarkup) {
|
|
|
|
fImg, err := imageToFileBytes(img)
|
|
if err != nil {
|
|
log.Println("Couldn't encode image to edit", chatID, err.Error())
|
|
return
|
|
}
|
|
|
|
msg := tgbotapi.EditMessageMediaConfig{
|
|
BaseEdit: tgbotapi.BaseEdit{
|
|
ChatID: chatID,
|
|
MessageID: msgId,
|
|
},
|
|
Media: tgbotapi.NewInputMediaPhoto(fImg),
|
|
}
|
|
|
|
if len(keyboard) > 0 {
|
|
msg.BaseEdit.ReplyMarkup = &keyboard[0]
|
|
}
|
|
|
|
_, err = ctx.Bot.Send(msg)
|
|
if err != nil {
|
|
log.Println("Couldn't send image edit to user", chatID, err.Error())
|
|
}
|
|
|
|
}
|
|
|
|
func (ctx *Context) editMessage(chatID int64, msgId int, text string, keyboard ...tgbotapi.InlineKeyboardMarkup) {
|
|
|
|
msg := tgbotapi.NewEditMessageText(chatID, msgId, text)
|
|
msg.ParseMode = tgbotapi.ModeHTML
|
|
if len(keyboard) > 0 {
|
|
msg.ReplyMarkup = &keyboard[0]
|
|
}
|
|
_, err := ctx.Bot.Send(msg)
|
|
if err != nil {
|
|
log.Println("Couldn't send edit to user", chatID, err.Error())
|
|
}
|
|
|
|
}
|
|
|
|
func (ctx *Context) callbackQuery(cqID string, text string, alert ...bool) {
|
|
var showAlert = false
|
|
if len(alert) > 0 {
|
|
showAlert = alert[0]
|
|
}
|
|
_, err := ctx.Bot.Request(tgbotapi.CallbackConfig{
|
|
CallbackQueryID: cqID,
|
|
Text: text,
|
|
ShowAlert: showAlert,
|
|
})
|
|
if err != nil {
|
|
log.Println("Couldn't send callback", cqID, err.Error())
|
|
}
|
|
}
|
|
|
|
func (ctx *Context) deleteMessage(msgID int, chatID int64) {
|
|
_, err := ctx.Bot.Request(tgbotapi.DeleteMessageConfig{
|
|
MessageID: msgID,
|
|
ChatID: chatID,
|
|
})
|
|
if err != nil {
|
|
log.Println("Couldn't send delete to user", chatID, err.Error())
|
|
}
|
|
}
|
|
|
|
func (ctx *Context) fileIDtoImage(fileID string) (image.Image, error) {
|
|
|
|
directURL, err := ctx.Bot.GetFileDirectURL(fileID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := http.Get(directURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
imgData, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
img, _, err := image.Decode(bytes.NewReader(imgData))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
func imageToFileBytes(img image.Image) (tgbotapi.FileBytes, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
err := png.Encode(buf, img)
|
|
if err != nil {
|
|
return tgbotapi.FileBytes{}, err
|
|
}
|
|
|
|
return tgbotapi.FileBytes{
|
|
Bytes: buf.Bytes(),
|
|
Name: "img.png",
|
|
}, nil
|
|
|
|
}
|
|
|
|
func getEditKeyboard(opts *catprinter.PrinterOptions, projectID int) tgbotapi.InlineKeyboardMarkup {
|
|
|
|
var (
|
|
emoji = '❌'
|
|
wl = "⬜️ Lighter"
|
|
wb = "⬛️ Darker"
|
|
)
|
|
|
|
if opts.Dither() {
|
|
emoji = '✅'
|
|
wl = "⬛️ Lower Strength"
|
|
wb = "⬜️ Higher Strength"
|
|
}
|
|
|
|
pid := strconv.Itoa(projectID)
|
|
|
|
return tgbotapi.NewInlineKeyboardMarkup(
|
|
tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData(string(emoji)+" Dithering", "wt"+pid),
|
|
),
|
|
tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData(wl, "wl"+pid),
|
|
tgbotapi.NewInlineKeyboardButtonData(wb, "wb"+pid),
|
|
),
|
|
tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData("🖨 Print", "wp"+pid),
|
|
tgbotapi.NewInlineKeyboardButtonData("🗑 Discard", "wd"+pid),
|
|
),
|
|
)
|
|
|
|
}
|
|
|
|
func getNextKeyboard() tgbotapi.InlineKeyboardMarkup {
|
|
return tgbotapi.NewInlineKeyboardMarkup(
|
|
tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData("✅ I have obtained my sticker", "next"),
|
|
),
|
|
)
|
|
}
|