152 lines
4.5 KiB
Go
152 lines
4.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/locale"
|
|
"github.com/mymmrac/telego"
|
|
tu "github.com/mymmrac/telego/telegoutil"
|
|
)
|
|
|
|
type CallbackQueryHandler struct {
|
|
app iface.App
|
|
}
|
|
|
|
func NewCallbackQueryHandler(app iface.App) Handler {
|
|
return &CallbackQueryHandler{app: app}
|
|
}
|
|
|
|
func (h *CallbackQueryHandler) Handle(wh telego.Update) error {
|
|
cq := wh.CallbackQuery
|
|
user, err := h.app.DB().ForUser().ByTelegramID(cq.From.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if user != nil && user.ID > 0 {
|
|
return h.handleSetupKey(cq, user)
|
|
}
|
|
return errors.New("not implemented yet")
|
|
}
|
|
|
|
func (h *CallbackQueryHandler) handleSetupKey(cq *telego.CallbackQuery, user *model.User) error {
|
|
var pl util.Payload
|
|
if err := json.Unmarshal([]byte(cq.Data), &pl); err != nil {
|
|
return err
|
|
}
|
|
switch pl.Action {
|
|
case util.PayloadActionChooseKeyboard:
|
|
return h.handleChooseKeyboard(pl, cq.Message.GetMessageID(), user)
|
|
case util.PayloadActionYesNo:
|
|
answer := pl.YesNoAnswer()
|
|
switch answer.Type {
|
|
case util.QuestionTypeRedmine:
|
|
return h.handleAnswerRedmine(answer, cq.Message.GetMessageID(), user)
|
|
case util.QuestionTypeRedmineHours:
|
|
return h.handleAnswerRedmineHours(answer, pl.Chat, cq.Message.GetMessageID(), user)
|
|
}
|
|
default:
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *CallbackQueryHandler) handleChooseKeyboard(pl util.Payload, msgID int, user *model.User) error {
|
|
cr := h.app.DB().ForChat()
|
|
result := pl.KeyboardChoice()
|
|
if pl.User != user.TelegramID {
|
|
return nil
|
|
}
|
|
chat, err := cr.ByTelegramID(pl.Chat)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if chat == nil || chat.ID == 0 {
|
|
return nil
|
|
}
|
|
chat.KeyboardType = result.Type
|
|
if err := cr.Save(chat); err != nil {
|
|
return err
|
|
}
|
|
|
|
kbTypeName := "standard_vote_keyboard"
|
|
if result.Type == model.StoryPointsKeyboard {
|
|
kbTypeName = "sp_vote_keyboard"
|
|
}
|
|
loc := h.app.Localizer(user.Language)
|
|
_, _ = h.app.TG().EditMessageText(&telego.EditMessageTextParams{
|
|
ChatID: tu.ID(user.ChatID),
|
|
MessageID: msgID,
|
|
Text: loc.Template("chosen_keyboard", map[string]interface{}{"Name": loc.Message(kbTypeName)}),
|
|
})
|
|
_, err = h.app.TG().SendMessage(&telego.SendMessageParams{
|
|
ChatID: tu.ID(user.ChatID),
|
|
Text: loc.Message("ask_for_redmine"),
|
|
ParseMode: telego.ModeMarkdown,
|
|
ReplyMarkup: &telego.InlineKeyboardMarkup{InlineKeyboard: [][]telego.InlineKeyboardButton{
|
|
{
|
|
{
|
|
Text: loc.Message("yes"),
|
|
CallbackData: util.NewRedmineQuestionPayload(user.TelegramID, chat.TelegramID, true).String(),
|
|
},
|
|
{
|
|
Text: loc.Message("no"),
|
|
CallbackData: util.NewRedmineQuestionPayload(user.TelegramID, chat.TelegramID, false).String(),
|
|
},
|
|
},
|
|
}},
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (h *CallbackQueryHandler) handleAnswerRedmine(answer util.Answer, msgID int, user *model.User) error {
|
|
loc := h.app.Localizer(user.Language)
|
|
if !answer.Result {
|
|
_, _ = h.app.TG().EditMessageText(&telego.EditMessageTextParams{
|
|
ChatID: tu.ID(user.ChatID),
|
|
MessageID: msgID,
|
|
Text: loc.Message("redmine_will_not_be_configured"),
|
|
})
|
|
return sendSetupDoneMessage(h.app.TG(), user.ChatID, loc)
|
|
}
|
|
// @todo Ask for Redmine URL and proceed with asking for Redmine key in NewMessageHandler. Save both to Integration.
|
|
return nil
|
|
}
|
|
|
|
func (h *CallbackQueryHandler) handleAnswerRedmineHours(answer util.Answer, chatID int64, msgID int, user *model.User) error {
|
|
loc := h.app.Localizer(user.Language)
|
|
if !answer.Result {
|
|
_, _ = h.app.TG().EditMessageText(&telego.EditMessageTextParams{
|
|
ChatID: tu.ID(user.ChatID),
|
|
MessageID: msgID,
|
|
Text: loc.Message("redmine_hours_will_not_be_configured"),
|
|
})
|
|
return sendSetupDoneMessage(h.app.TG(), user.ChatID, loc)
|
|
}
|
|
cr := h.app.DB().ForChat()
|
|
chat, err := cr.ByTelegramIDWithIntegrations(chatID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, it := range chat.Integrations {
|
|
if it.Type == model.RedmineIntegration {
|
|
it.Params["save_hours"] = true
|
|
if err := h.app.DB().ForIntegration().Save(&it); err != nil {
|
|
return err
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return sendSetupDoneMessage(h.app.TG(), user.ChatID, loc)
|
|
}
|
|
|
|
func sendSetupDoneMessage(tg *telego.Bot, chatID int64, loc locale.Localizer) error {
|
|
_, err := tg.SendMessage(&telego.SendMessageParams{
|
|
ChatID: tu.ID(chatID),
|
|
Text: loc.Message("setup_done"),
|
|
})
|
|
return err
|
|
}
|