Make code reuse consts

This commit is contained in:
Astra 2026-01-28 16:05:15 +00:00
parent 5be952483f
commit 9bcc5ae765

38
main.go
View file

@ -9,6 +9,12 @@ import (
api "github.com/OvyFlash/telegram-bot-api" api "github.com/OvyFlash/telegram-bot-api"
) )
const (
AdminJoinRequestMsg = "New join request from _%s_\n\nJoin reason: %s"
AdminApprovedMsg = "✅ Join #request approved for _%s_\n\nJoin reason: %s\nApproved by: %s\nApproved at: %s"
AdminRejectedMsg = "❌ Join #request rejected for _%s_\n\nJoin reason: %s\nRejected by: %s\nRejected at: %s"
)
type ExtendedChatJoinRequest struct { type ExtendedChatJoinRequest struct {
*api.ChatJoinRequest *api.ChatJoinRequest
JoinReason string JoinReason string
@ -71,13 +77,9 @@ func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update
user.JoinReason = escapeMarkdown(update.Text) user.JoinReason = escapeMarkdown(update.Text)
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID, edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
fmt.Sprintf("New join request from _%s_\n\nJoin reason: %s", fmt.Sprintf(AdminJoinRequestMsg,
user.From.String(), user.JoinReason)) user.From.String(), user.JoinReason))
approveButton := api.NewInlineKeyboardButtonData("✅ Approve", fmt.Sprintf("approve_%d", user.From.ID)) keyboard := newApprovalKeyboard(user.From.ID)
rejectButton := api.NewInlineKeyboardButtonData("❌ Reject", fmt.Sprintf("reject_%d", user.From.ID))
keyboard := api.NewInlineKeyboardMarkup(
[]api.InlineKeyboardButton{approveButton, rejectButton},
)
edit.ReplyMarkup = &keyboard edit.ReplyMarkup = &keyboard
edit.ParseMode = api.ModeMarkdown edit.ParseMode = api.ModeMarkdown
bot.API.Send(edit) bot.API.Send(edit)
@ -85,12 +87,8 @@ func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update
ack := api.NewMessage(update.From.ID, "Thank you! Your request has been sent to the admins for review.") ack := api.NewMessage(update.From.ID, "Thank you! Your request has been sent to the admins for review.")
bot.API.Send(ack) bot.API.Send(ack)
} else { } else {
// if user.Date+(60*60*5) < int(time.Now().Unix()) {
// } else {
m := api.NewMessage(update.From.ID, "Your request is already pending approval.") m := api.NewMessage(update.From.ID, "Your request is already pending approval.")
bot.API.Send(m) bot.API.Send(m)
// }
} }
} }
@ -105,14 +103,8 @@ func (bot *Bot) handleJoinRequest(request *api.ChatJoinRequest) {
bot.API.Send(m) bot.API.Send(m)
m = api.NewMessage(bot.Config.AdminChatId, m = api.NewMessage(bot.Config.AdminChatId,
fmt.Sprintf("New join request from _%s_\n\nJoin reason: (no reason provided yet)", fmt.Sprintf(AdminJoinRequestMsg, request.From.String(), "(awaiting user response)"))
request.From.String())) m.ReplyMarkup = newApprovalKeyboard(request.From.ID)
approveButton := api.NewInlineKeyboardButtonData("✅ Approve", fmt.Sprintf("approve_%d", request.From.ID))
rejectButton := api.NewInlineKeyboardButtonData("❌ Reject", fmt.Sprintf("reject_%d", request.From.ID))
keyboard := api.NewInlineKeyboardMarkup(
[]api.InlineKeyboardButton{approveButton, rejectButton},
)
m.ReplyMarkup = keyboard
m.ParseMode = api.ModeMarkdown m.ParseMode = api.ModeMarkdown
if bot.Config.AdminChatTopicId != 0 { if bot.Config.AdminChatTopicId != 0 {
m.MessageThreadID = bot.Config.AdminChatTopicId m.MessageThreadID = bot.Config.AdminChatTopicId
@ -150,7 +142,7 @@ func (bot *Bot) handleCallbackQuery(query *api.CallbackQuery) {
} }
bot.API.Send(r) bot.API.Send(r)
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID, edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
fmt.Sprintf("✅ Join #request approved for _%s_\n\nJoin reason: %s\nApproved by: %s\nApproved at: %s", fmt.Sprintf(AdminApprovedMsg,
user.From.String(), user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05"))) user.From.String(), user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05")))
edit.ParseMode = api.ModeMarkdown edit.ParseMode = api.ModeMarkdown
bot.API.Send(edit) bot.API.Send(edit)
@ -168,7 +160,7 @@ func (bot *Bot) handleCallbackQuery(query *api.CallbackQuery) {
} }
bot.API.Send(r) bot.API.Send(r)
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID, edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
fmt.Sprintf("❌ Join #request rejected for _%s_\n\nJoin reason: %s\nRejected by: %s\nRejected at: %s", fmt.Sprintf(AdminRejectedMsg,
user.From.String(), user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05"))) user.From.String(), user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05")))
edit.ParseMode = api.ModeMarkdown edit.ParseMode = api.ModeMarkdown
bot.API.Send(edit) bot.API.Send(edit)
@ -189,3 +181,9 @@ func escapeMarkdown(s string) string {
replacer := strings.NewReplacer(replacements...) replacer := strings.NewReplacer(replacements...)
return replacer.Replace(s) return replacer.Replace(s)
} }
func newApprovalKeyboard(userID int64) api.InlineKeyboardMarkup {
approveBtn := api.NewInlineKeyboardButtonData("✅ Approve", fmt.Sprintf("approve_%d", userID))
rejectBtn := api.NewInlineKeyboardButtonData("❌ Reject", fmt.Sprintf("reject_%d", userID))
return api.NewInlineKeyboardMarkup([]api.InlineKeyboardButton{approveBtn, rejectBtn})
}