Make code reuse consts
This commit is contained in:
parent
5be952483f
commit
9bcc5ae765
1 changed files with 18 additions and 20 deletions
38
main.go
38
main.go
|
|
@ -9,6 +9,12 @@ import (
|
|||
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 {
|
||||
*api.ChatJoinRequest
|
||||
JoinReason string
|
||||
|
|
@ -71,13 +77,9 @@ func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update
|
|||
user.JoinReason = escapeMarkdown(update.Text)
|
||||
|
||||
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))
|
||||
approveButton := api.NewInlineKeyboardButtonData("✅ Approve", fmt.Sprintf("approve_%d", user.From.ID))
|
||||
rejectButton := api.NewInlineKeyboardButtonData("❌ Reject", fmt.Sprintf("reject_%d", user.From.ID))
|
||||
keyboard := api.NewInlineKeyboardMarkup(
|
||||
[]api.InlineKeyboardButton{approveButton, rejectButton},
|
||||
)
|
||||
keyboard := newApprovalKeyboard(user.From.ID)
|
||||
edit.ReplyMarkup = &keyboard
|
||||
edit.ParseMode = api.ModeMarkdown
|
||||
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.")
|
||||
bot.API.Send(ack)
|
||||
} else {
|
||||
// if user.Date+(60*60*5) < int(time.Now().Unix()) {
|
||||
|
||||
// } else {
|
||||
m := api.NewMessage(update.From.ID, "Your request is already pending approval.")
|
||||
bot.API.Send(m)
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -105,14 +103,8 @@ func (bot *Bot) handleJoinRequest(request *api.ChatJoinRequest) {
|
|||
bot.API.Send(m)
|
||||
|
||||
m = api.NewMessage(bot.Config.AdminChatId,
|
||||
fmt.Sprintf("New join request from _%s_\n\nJoin reason: (no reason provided yet)",
|
||||
request.From.String()))
|
||||
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
|
||||
fmt.Sprintf(AdminJoinRequestMsg, request.From.String(), "(awaiting user response)"))
|
||||
m.ReplyMarkup = newApprovalKeyboard(request.From.ID)
|
||||
m.ParseMode = api.ModeMarkdown
|
||||
if bot.Config.AdminChatTopicId != 0 {
|
||||
m.MessageThreadID = bot.Config.AdminChatTopicId
|
||||
|
|
@ -150,7 +142,7 @@ func (bot *Bot) handleCallbackQuery(query *api.CallbackQuery) {
|
|||
}
|
||||
bot.API.Send(r)
|
||||
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")))
|
||||
edit.ParseMode = api.ModeMarkdown
|
||||
bot.API.Send(edit)
|
||||
|
|
@ -168,7 +160,7 @@ func (bot *Bot) handleCallbackQuery(query *api.CallbackQuery) {
|
|||
}
|
||||
bot.API.Send(r)
|
||||
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")))
|
||||
edit.ParseMode = api.ModeMarkdown
|
||||
bot.API.Send(edit)
|
||||
|
|
@ -189,3 +181,9 @@ func escapeMarkdown(s string) string {
|
|||
replacer := strings.NewReplacer(replacements...)
|
||||
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})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue