telegram-join-approval-nuzzles/handlers/join.go
2026-04-02 13:29:12 +01:00

76 lines
3.1 KiB
Go

package handlers
import (
"fmt"
"log"
utils "git.zio.sh/astra/telegram-join-approval-nuzzles/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 != "" {
utils.SendMessage(bot.API, update.From.ID, 0, "Your request is already pending approval.")
return
}
user.JoinReason = utils.EscapeHTML(update.Text)
userString := utils.BuildUserString(&user.From)
keyboard := utils.NewApprovalKeyboard(user.From.ID)
if bot.Config.ReminderMessage != "" {
newButton := api.NewInlineKeyboardButtonData("Send Reminder", fmt.Sprintf("remind_%d", user.From.ID))
keyboard.InlineKeyboard[1] = append([]api.InlineKeyboardButton{newButton}, keyboard.InlineKeyboard[1]...)
}
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.")
}
// HandleJoinRequest initiates the join approval flow by sending the entry message and admin notification.
func (bot *Bot) HandleJoinRequest(request *api.ChatJoinRequest) {
// Check if user already has a pending request
if existingUser := bot.GetPendingUser(request.From.ID); existingUser != nil {
utils.SendMessage(bot.API, request.From.ID, 0,
"You have already requested to join. Please send a single message as your join reason.")
return
}
utils.SendMessage(bot.API, request.From.ID, 0, bot.Config.EntryMessage)
userString := utils.BuildUserString(&request.From)
keyboard := utils.NewApprovalKeyboard(request.From.ID)
if bot.Config.ReminderMessage != "" {
newButton := api.NewInlineKeyboardButtonData("Send Reminder", fmt.Sprintf("remind_%d", request.From.ID))
keyboard.InlineKeyboard[1] = append([]api.InlineKeyboardButton{newButton}, keyboard.InlineKeyboard[1]...)
}
m := api.NewMessage(*bot.Config.AdminChatId,
fmt.Sprintf(AdminJoinRequestMsg, userString, request.From.ID, "(awaiting user response)"))
m.ReplyMarkup = keyboard
m.ParseMode = api.ModeHTML
m.LinkPreviewOptions = api.LinkPreviewOptions{IsDisabled: true}
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."))
bot.API.Request(api.NewCallback(query.ID, "Join request failed."))
}