Skip to content

Commit

Permalink
feat: improved user acceptance flow
Browse files Browse the repository at this point in the history
  • Loading branch information
hrvadl committed Jun 9, 2024
1 parent 8ea4f93 commit 7ca6bee
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ linters:
- gochecknoinits # Checks that no init functions are present in Go code.
- godot # Check if comments end in a period
- godox # Tool for detection of FIXME, TODO and other comment keywords
- err113 # Golang linter to check the errors handling expressions
- gomnd # An analyzer to detect magic numbers
- ireturn # Accept Interfaces, Return Concrete Types
- interfacebloat # A linter that checks the number of methods inside an interface
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/callback/callbackdata/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const (
DeclineDecision = "decline"
)

func NewAgreeWithGroupID(groupdID int64) string {
return fmt.Sprintf("%s_%v", AgreeDecision, groupdID)
func NewAgreeWithGroupID(groupdID int64, msgID int) string {
return fmt.Sprintf("%s_%d_%d", AgreeDecision, groupdID, msgID)
}

func NewDeclineWithGroupID(groupdID int64) string {
return fmt.Sprintf("%s_%v", DeclineDecision, groupdID)
func NewDeclineWithGroupID(groupdID int64, msgID int) string {
return fmt.Sprintf("%s_%d_%d", DeclineDecision, groupdID, msgID)
}
18 changes: 14 additions & 4 deletions internal/handler/callback/callbackdata/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

type Payload struct {
Decision string
GroupID int64
Decision string
GroupID int64
MessageID int
}

func Parse(data string) (*Payload, error) {
splits := strings.Split(data, "_")
if len(splits) != 2 { //nolint:gomnd,mnd
if len(splits) != 3 { //nolint:gomnd,mnd
return nil, fmt.Errorf("invalid callback query data token: %v", splits)
}

Expand All @@ -22,10 +23,19 @@ func Parse(data string) (*Payload, error) {
return nil, fmt.Errorf("invalid groupID in callback query data: %s", splits[1])
}

messageID, err := strconv.Atoi(splits[2])
if err != nil {
return nil, fmt.Errorf("invalid groupID in callback query data: %s", splits[1])
}

decision := splits[0]
if decision != AgreeDecision && decision != DeclineDecision {
return nil, fmt.Errorf("invalid callback data for terms of use decision: %v", decision)
}

return &Payload{decision, groupID}, nil
return &Payload{
Decision: decision,
GroupID: groupID,
MessageID: messageID,
}, nil
}
8 changes: 4 additions & 4 deletions internal/handler/callback/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func (h *handler) callbackQuery(ctx context.Context, bot *telego.Bot, query tele
msg = fmt.Sprintf(messages.Decline, viper.GetString("admin-username"))
}

_, err = bot.SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(query.From.ID),
Text: msg,
ReplyMarkup: tu.ReplyKeyboardRemove(),
_, err = bot.EditMessageText(&telego.EditMessageTextParams{
MessageID: data.MessageID,
ChatID: tu.ID(query.From.ID),
Text: msg,
})
if err != nil {
log.Error("Sending decision message failed", slog.Any("error", err))
Expand Down
34 changes: 22 additions & 12 deletions internal/handler/join/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,40 @@ func (h *handler) chatJoinRequest(ctx context.Context, bot *telego.Bot, request
return
}

_, err := bot.SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(request.From.ID),
ParseMode: telego.ModeHTML,
Text: messages.JoinHeader + messages.Rules,
})
if err != nil {
log.Error("Sending TermsOfUse and Rules message failed", slog.Any("error", err))
}

msg := tu.Message(tu.ID(request.From.ID), messages.JoinFooter)
toEdit, err := bot.SendMessage(msg)
if err != nil {
log.Error("Sending terms of use failed", slog.Any("error", err))
}

k := tu.InlineKeyboard(
tu.InlineKeyboardRow(
telego.InlineKeyboardButton{
Text: AgreeText,
CallbackData: callbackdata.NewAgreeWithGroupID(request.Chat.ID),
CallbackData: callbackdata.NewAgreeWithGroupID(request.Chat.ID, toEdit.MessageID),
},
telego.InlineKeyboardButton{
Text: DontAgreeText,
CallbackData: callbackdata.NewDeclineWithGroupID(request.Chat.ID),
CallbackData: callbackdata.NewDeclineWithGroupID(request.Chat.ID, toEdit.MessageID),
},
),
)

_, err := bot.SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(request.From.ID),
ParseMode: telego.ModeHTML,
Text: messages.JoinHeader + messages.Rules,
_, err = bot.EditMessageReplyMarkup(&telego.EditMessageReplyMarkupParams{
ReplyMarkup: k,
ChatID: tu.ID(request.From.ID),
MessageID: toEdit.MessageID,
})
if err != nil {
log.Error("Sending TermsOfUse and Rules message failed", slog.Any("error", err))
}

msg := tu.Message(tu.ID(request.From.ID), messages.JoinFooter).WithReplyMarkup(k).WithProtectContent()
if _, err := bot.SendMessage(msg); err != nil {
log.Error("Sending terms of use failed", slog.Any("error", err))
log.Error("Editing callback query failed", slog.Any("error", err))
}
}
2 changes: 0 additions & 2 deletions internal/messages/terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const JoinHeader = `

//nolint:lll
const JoinFooter = `
Перед тим як прийняти рішення, будь ласка, привітайтесь з ботом, написавши довільне повідомлення. Після цього ви зможете натиснути на потрібну кнопну та отримати результат вашого запиту.
Приймаючи запрошення в цю групу ви автоматично:
1. Засуджуєте війну рф проти України.
2. Не визнаєте тимчасовано окупованії українські території субʼєктом рф.
Expand Down

0 comments on commit 7ca6bee

Please sign in to comment.