110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
package group
|
|
|
|
import (
|
|
"fmt"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/store"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/integration"
|
|
"github.com/mymmrac/telego"
|
|
tu "github.com/mymmrac/telego/telegoutil"
|
|
"golang.org/x/text/language"
|
|
"strings"
|
|
)
|
|
|
|
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
|
|
}
|
|
chat, err := h.App.DB().ForChat().ByTelegramIDWithIntegrations(wh.Message.Chat.ID)
|
|
if err != nil {
|
|
_ = util.SendInternalError(h.App.TG(), wh.Message.Chat.ID, h.Localizer(language.English.String()))
|
|
return err
|
|
}
|
|
user, err := h.App.DB().ForUser().ByID(chat.UserID)
|
|
if err != nil {
|
|
_ = util.SendInternalError(h.App.TG(), wh.Message.Chat.ID, h.Localizer(language.English.String()))
|
|
return err
|
|
}
|
|
loc := h.Localizer(user.Language)
|
|
_ = loc
|
|
if len(wh.Message.Entities) != 1 ||
|
|
(len(wh.Message.Entities) == 1 && wh.Message.Entities[0].Type != telego.EntityTypeBotCommand) ||
|
|
(len(wh.Message.Entities) == 1 && wh.Message.Entities[0].Offset != 0) {
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
taskID int
|
|
canRedmine bool
|
|
)
|
|
taskInfo := strings.TrimSpace(wh.Message.Text[wh.Message.Entities[0].Length:])
|
|
if taskInfo != "" {
|
|
for _, integrationData := range chat.Integrations {
|
|
id, info := integration.New(integrationData, h.App.Log()).GetTaskInfo(taskInfo)
|
|
if id > 0 {
|
|
taskID = id
|
|
taskInfo = info
|
|
canRedmine = integrationData.Type == model.RedmineIntegration
|
|
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
dotOrColon := "."
|
|
if taskInfo != "" {
|
|
dotOrColon = ": "
|
|
}
|
|
|
|
var kb telego.ReplyMarkup
|
|
switch chat.KeyboardType {
|
|
case model.StandardKeyboard:
|
|
kb = util.StandardVoteKeyboard(wh.Message.Chat.ID, loc)
|
|
case model.StoryPointsKeyboard:
|
|
kb = util.StoryPointsVoteKeyboard(wh.Message.Chat.ID, loc)
|
|
}
|
|
|
|
msg, err := h.App.TG().SendMessage(&telego.SendMessageParams{
|
|
ChatID: tu.ID(wh.Message.Chat.ID),
|
|
Text: fmt.Sprintf("%s\n%s",
|
|
loc.Template("poker_start", map[string]interface{}{
|
|
"Name": wh.Message.From.Username,
|
|
"DotOrColon": dotOrColon,
|
|
"Subject": taskInfo,
|
|
}),
|
|
loc.Message("no_votes_yet"),
|
|
),
|
|
ReplyMarkup: kb,
|
|
ParseMode: telego.ModeMarkdown,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
store.Polls.Set(msg.MessageID, store.PollState{
|
|
Initiator: wh.Message.From.Username,
|
|
Subject: taskInfo,
|
|
TaskID: taskID,
|
|
Votes: []store.MemberVote{},
|
|
Result: store.PollResult{},
|
|
CanRedmine: canRedmine,
|
|
})
|
|
|
|
return nil
|
|
}
|