telegram-join-approval-nuzzles/handlers/join.go

59 lines
2.2 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)
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) {
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."))
bot.API.Request(api.NewCallback(query.ID, "Join request failed."))
}