refactor, wip: poll create

This commit is contained in:
Pavel 2024-05-09 23:41:03 +03:00
parent d615a04e61
commit 3a8a7c2a5c
6 changed files with 156 additions and 108 deletions

View File

@ -29,6 +29,7 @@ func (h *CallbackQueryHandler) Handle(wh telego.Update) error {
if user != nil && user.ID > 0 { if user != nil && user.ID > 0 {
return h.handleSetupKey(cq, user) return h.handleSetupKey(cq, user)
} }
// @todo implement poker polls handling.
return errors.New("not implemented yet") return errors.New("not implemented yet")
} }

View File

@ -0,0 +1,28 @@
package chat
import (
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
)
type Poll struct {
iface.Base
}
func NewPoll(app iface.App, userID, chatID int64) *Poll {
return &Poll{iface.NewBase(app, userID, chatID)}
}
func (h *Poll) Handle(wh telego.Update) error {
if wh.Message.Chat.Type == telego.ChatTypePrivate {
_, err := h.App.TG().SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(wh.Message.Chat.ID),
Text: h.Localizer(wh.Message.From.LanguageCode).Message("use_this_command_in_group"),
ParseMode: telego.ModeMarkdown,
})
return err
}
// @todo implement poker polls.
return nil
}

View File

@ -2,6 +2,7 @@ package handler
import ( import (
"encoding/json" "encoding/json"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/chat"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/store" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/store"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
@ -20,6 +21,9 @@ func NewMessageHandler(app iface.App) Handler {
func (h *MessageHandler) Handle(wh telego.Update) error { func (h *MessageHandler) Handle(wh telego.Update) error {
if wh.Message.From != nil && if wh.Message.From != nil &&
wh.Message.Chat.Type == telego.ChatTypePrivate { wh.Message.Chat.Type == telego.ChatTypePrivate {
if util.MatchCommand("poll", wh.Message) {
return chat.NewPoll(h.app, wh.Message.From.ID, wh.Message.Chat.ID).Handle(wh)
}
if util.MatchCommand("start", wh.Message) { if util.MatchCommand("start", wh.Message) {
return wizard.NewRegister(h.app, wh.Message.From.ID, wh.Message.Chat.ID).Handle(wh) return wizard.NewRegister(h.app, wh.Message.From.ID, wh.Message.Chat.ID).Handle(wh)
} }

View File

@ -5,6 +5,7 @@ import (
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/store" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/store"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/locale"
"github.com/mymmrac/telego" "github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil" tu "github.com/mymmrac/telego/telegoutil"
"net/http" "net/http"
@ -31,7 +32,22 @@ func (h *RedmineSetup) Handle(wh telego.Update) error {
} }
loc := h.Localizer(wh.Message.From.LanguageCode) loc := h.Localizer(wh.Message.From.LanguageCode)
if h.Redmine.URL == "" { if h.Redmine.URL == "" {
h.Redmine.URL = h.processURL(wh.Message.Text) return h.handleURLStep(wh.Message.Text, loc)
}
if h.Redmine.Key == "" {
return h.handleKeyStep(wh.Message.Text, loc)
}
if h.Redmine.SPFieldName == "" && h.Redmine.WaitingForSPField {
return h.handleSPFieldStep(wh.Message.Text, loc)
}
return nil
}
func (h *RedmineSetup) handleURLStep(text string, loc locale.Localizer) error {
h.Redmine.URL = h.processURL(text)
if h.Redmine.URL == "" { if h.Redmine.URL == "" {
_, err := h.App.TG().SendMessage(&telego.SendMessageParams{ _, err := h.App.TG().SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(h.ChatID), ChatID: tu.ID(h.ChatID),
@ -49,8 +65,8 @@ func (h *RedmineSetup) Handle(wh telego.Update) error {
return err return err
} }
if h.Redmine.Key == "" { func (h *RedmineSetup) handleKeyStep(text string, loc locale.Localizer) error {
h.Redmine.Key = strings.TrimSpace(wh.Message.Text) h.Redmine.Key = strings.TrimSpace(text)
if h.Redmine.Key == "" { if h.Redmine.Key == "" {
_, err := h.App.TG().SendMessage(&telego.SendMessageParams{ _, err := h.App.TG().SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(h.ChatID), ChatID: tu.ID(h.ChatID),
@ -126,8 +142,8 @@ func (h *RedmineSetup) Handle(wh telego.Update) error {
return err return err
} }
if h.Redmine.SPFieldName == "" && h.Redmine.WaitingForSPField { func (h *RedmineSetup) handleSPFieldStep(text string, loc locale.Localizer) error {
h.Redmine.SPFieldName = strings.TrimSpace(wh.Message.Text) h.Redmine.SPFieldName = strings.TrimSpace(text)
if h.Redmine.SPFieldName == "" { if h.Redmine.SPFieldName == "" {
_, err := h.App.TG().SendMessage(&telego.SendMessageParams{ _, err := h.App.TG().SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(h.ChatID), ChatID: tu.ID(h.ChatID),
@ -165,9 +181,6 @@ func (h *RedmineSetup) Handle(wh telego.Update) error {
return util.SendSetupDone(h.App.TG(), h.ChatID, loc) return util.SendSetupDone(h.App.TG(), h.ChatID, loc)
} }
return nil
}
func (h *RedmineSetup) processURL(input string) string { func (h *RedmineSetup) processURL(input string) string {
uri, err := url.Parse(strings.TrimSpace(input)) uri, err := url.Parse(strings.TrimSpace(input))
if err != nil { if err != nil {

View File

@ -17,6 +17,7 @@ redmine_hours_will_be_configured: "👌 Vote results will be be sent to Redmine
redmine_poker_will_not_be_configured: "👌 Vote results won't be sent to Redmine." redmine_poker_will_not_be_configured: "👌 Vote results won't be sent to Redmine."
redmine_poker_will_be_configured: "✔️ Done, vote results will be sent to Redmine in the field *{{.Name}}*." redmine_poker_will_be_configured: "✔️ Done, vote results will be sent to Redmine in the field *{{.Name}}*."
chosen_keyboard: "{{.Name}} has been selected." chosen_keyboard: "{{.Name}} has been selected."
use_this_command_in_group: "😒 This command should only be used in your group."
please_send_redmine_url: "Please send the URL of your Redmine instance." please_send_redmine_url: "Please send the URL of your Redmine instance."
please_send_redmine_key: "Please send your API key that will be used by the bot to interact with Redmine. The bot will perform the following actions:\n\n• retrieving task data;\n• writing the result of the poker in the task (if configured in subsequent steps).\n\nThe access key for the API can be found on the left part of the page by [this link]({{.Origin}}/my/account)." please_send_redmine_key: "Please send your API key that will be used by the bot to interact with Redmine. The bot will perform the following actions:\n\n• retrieving task data;\n• writing the result of the poker in the task (if configured in subsequent steps).\n\nThe access key for the API can be found on the left part of the page by [this link]({{.Origin}}/my/account)."
invalid_redmine_credentials: "⚠️ Failed to verify connection with Redmine. Will try setting up again?" invalid_redmine_credentials: "⚠️ Failed to verify connection with Redmine. Will try setting up again?"

View File

@ -17,6 +17,7 @@ redmine_hours_will_be_configured: "👌 Оценка временных затр
redmine_poker_will_not_be_configured: "👌 Результат голосования не будет передаваться в Redmine." redmine_poker_will_not_be_configured: "👌 Результат голосования не будет передаваться в Redmine."
redmine_poker_will_be_configured: "✔️ Готово, результат голосования будет передаваться в Redmine в поле *{{.Name}}*." redmine_poker_will_be_configured: "✔️ Готово, результат голосования будет передаваться в Redmine в поле *{{.Name}}*."
chosen_keyboard: "Выбрана {{.Name}}." chosen_keyboard: "Выбрана {{.Name}}."
use_this_command_in_group: "😒 Эта команда должна использоваться только в групповом чате."
please_send_redmine_url: "Пожалуйста, отправьте ссылку на свой инстанс Redmine." please_send_redmine_url: "Пожалуйста, отправьте ссылку на свой инстанс Redmine."
please_send_redmine_key: "Отправьте свой API-ключ, который будет использоваться ботом для взаимодействия с Redmine. Бот будет выполнять следующие действия:\n\n• получение данных задачи;\n• запись результата покера в задачу (если будет настроено в следующих шагах).\n\nКлюч доступа к API можно найти в левой части страницы по [этой ссылке]({{.Origin}}/my/account)." please_send_redmine_key: "Отправьте свой API-ключ, который будет использоваться ботом для взаимодействия с Redmine. Бот будет выполнять следующие действия:\n\n• получение данных задачи;\n• запись результата покера в задачу (если будет настроено в следующих шагах).\n\nКлюч доступа к API можно найти в левой части страницы по [этой ссылке]({{.Origin}}/my/account)."
invalid_redmine_credentials: "⚠️ Не удалось проверить связь с Redmine. Будете пробовать настроить заново?" invalid_redmine_credentials: "⚠️ Не удалось проверить связь с Redmine. Будете пробовать настроить заново?"