Files
golifehk/adaptors/tg/botsend.go
2026-03-10 15:18:34 +08:00

68 lines
1.6 KiB
Go

package tg
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
query "github.com/tgckpg/golifehk/query"
)
func BotSendText(bot *tgbotapi.BotAPI, update *tgbotapi.Update, mesg *string) {
var msg tgbotapi.MessageConfig
msg = tgbotapi.NewMessage(update.Message.Chat.ID, *mesg)
msg.ParseMode = "MarkdownV2"
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
}
func BotSend(bot *tgbotapi.BotAPI, update *tgbotapi.Update, qResult query.IQueryResult) (bool, error) {
var msg tgbotapi.MessageConfig
mesg, err := qResult.Message()
if err != nil {
// not sent, error
return false, err
}
mesgType := qResult.DataType()
switch mesgType {
case "IGNORE":
// not sent with no error tells the parent to look for other processors
return false, nil
}
var chatId int64
if update.Message != nil {
chatId = update.Message.Chat.ID
msg.ReplyToMessageID = update.Message.MessageID
}
if update.CallbackQuery != nil {
chatId = update.CallbackQuery.Message.Chat.ID
}
msg = tgbotapi.NewMessage(chatId, mesg)
msg.ParseMode = "MarkdownV2"
switch mesgType {
case "PlainText":
case "Table":
buttonRows := [][]tgbotapi.InlineKeyboardButton{}
for _, row := range qResult.GetTableData() {
buttons := []tgbotapi.InlineKeyboardButton{}
for _, cell := range row {
button := tgbotapi.NewInlineKeyboardButtonData(cell.Name, cell.Value)
buttons = append(buttons, button)
}
buttonRows = append(buttonRows, buttons)
}
msg.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(buttonRows...)
}
bot.Send(msg)
return true, nil
}