Code refactor
This commit is contained in:
parent
5e37545782
commit
d2f7297d92
7 changed files with 534 additions and 330 deletions
72
handlers/join.go
Normal file
72
handlers/join.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
utils "git.zio.sh/astra/telegram-approval-join/pkg/utils"
|
||||
api "github.com/OvyFlash/telegram-bot-api"
|
||||
)
|
||||
|
||||
// HandleJoinRequestResponse records the user's join reason and notifies admins.
|
||||
func (bot *Bot) HandleJoinRequestResponse(user *ExtendedChatJoinRequest, update *api.Message) {
|
||||
if user.JoinReason == "" {
|
||||
user.JoinReason = utils.EscapeHTML(update.Text)
|
||||
userString := utils.BuildUserString(&user.From)
|
||||
|
||||
keyboard := utils.NewApprovalKeyboard(user.From.ID)
|
||||
utils.EditMessageWithKeyboard(bot.API, *bot.Config.AdminChatId, user.JoinRequestMessageID,
|
||||
fmt.Sprintf(AdminJoinRequestMsg,
|
||||
userString, user.From.ID, user.JoinReason), &keyboard)
|
||||
|
||||
utils.SendMessage(bot.API, update.From.ID, 0, "Thank you! Your request has been sent to the admins for review.")
|
||||
} else {
|
||||
utils.SendMessage(bot.API, update.From.ID, 0, "Your request is already pending approval.")
|
||||
}
|
||||
}
|
||||
|
||||
// HandleJoinRequest initiates join approval flow by sending entry message and admin notification.
|
||||
func (bot *Bot) HandleJoinRequest(request *api.ChatJoinRequest) {
|
||||
// if chat is not in config, ignore
|
||||
if *bot.Config.TargetChatId != request.Chat.ID {
|
||||
m := api.NewMessage(*bot.Config.AdminChatId,
|
||||
fmt.Sprintf("Received join request for chat %s (<code>%d</code>), but it's not in config, ignoring",
|
||||
request.Chat.Title, request.Chat.ID))
|
||||
leaveBtn := api.NewInlineKeyboardButtonData("Leave Chat", fmt.Sprintf("leave_%d", request.Chat.ID))
|
||||
m.ReplyMarkup = api.NewInlineKeyboardMarkup([]api.InlineKeyboardButton{leaveBtn})
|
||||
m.ParseMode = api.ModeHTML
|
||||
bot.API.Send(m)
|
||||
return
|
||||
}
|
||||
|
||||
utils.SendMessage(bot.API, request.From.ID, 0, bot.Config.EntryMessage)
|
||||
userString := utils.BuildUserString(&request.From)
|
||||
|
||||
m := api.NewMessage(*bot.Config.AdminChatId,
|
||||
fmt.Sprintf(AdminJoinRequestMsg, userString, request.From.ID, "(awaiting user response)"))
|
||||
m.ReplyMarkup = utils.NewApprovalKeyboard(request.From.ID)
|
||||
m.ParseMode = api.ModeHTML
|
||||
if topic := *bot.Config.AdminChatTopicId; topic != 0 {
|
||||
m.MessageThreadID = topic
|
||||
}
|
||||
r, err := bot.API.Send(m)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send join request to admin chat: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
bot.SetPendingUser(request.From.ID, &ExtendedChatJoinRequest{
|
||||
ChatJoinRequest: request,
|
||||
JoinReason: "",
|
||||
JoinRequestMessageID: r.MessageID,
|
||||
})
|
||||
}
|
||||
|
||||
// SendFailureMessage edits the admin request message to show a failure status.
|
||||
func (bot *Bot) SendFailureMessage(user *ExtendedChatJoinRequest, query *api.CallbackQuery, userString string) {
|
||||
utils.EditMessage(bot.API, *bot.Config.AdminChatId, user.JoinRequestMessageID,
|
||||
fmt.Sprintf(AdminFailedMsg, userString, user.From.ID, utils.EscapeHTML(user.JoinReason), "User not found in requests."))
|
||||
|
||||
callback := api.NewCallback(query.ID, "Join request failed.")
|
||||
bot.API.Request(callback)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue