This commit is contained in:
Astra 2026-02-15 17:34:37 +00:00
parent 26269d30f9
commit 94a0061454

38
main.go
View file

@ -87,10 +87,10 @@ func main() {
userString = fmt.Sprintf("%s %s", update.Message.From.FirstName, update.Message.From.LastName) userString = fmt.Sprintf("%s %s", update.Message.From.FirstName, update.Message.From.LastName)
} }
if strings.TrimPrefix(lines[3], "Declined by: ") == userString { if strings.TrimPrefix(lines[3], "Declined by: ") == userString {
reason := escapeHTML(update.Message.Text) reason := EscapeHTML(update.Message.Text)
userID, username, joinReason, declinedBy, declinedAt := GetInfoFromMsg(repliedMsg.Text) userID, username, joinReason, declinedBy, declinedAt := GetInfoFromMsg(repliedMsg.Text)
if strings.HasPrefix(update.Message.Text, "+") { if strings.HasPrefix(update.Message.Text, "+") {
reason = escapeHTML(update.Message.Text[1:]) reason = EscapeHTML(update.Message.Text[1:])
m := api.NewMessage(userID, fmt.Sprintf("Your join request was declined for the following reason:\n\n%s", m := api.NewMessage(userID, fmt.Sprintf("Your join request was declined for the following reason:\n\n%s",
reason)) reason))
b.API.Send(m) b.API.Send(m)
@ -108,7 +108,7 @@ func main() {
func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update *api.Message) { func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update *api.Message) {
if user.JoinReason == "" { if user.JoinReason == "" {
user.JoinReason = escapeHTML(update.Text) user.JoinReason = EscapeHTML(update.Text)
userString := "" userString := ""
if user.From.UserName != "" { if user.From.UserName != "" {
@ -119,7 +119,7 @@ func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID, edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
fmt.Sprintf(AdminJoinRequestMsg, fmt.Sprintf(AdminJoinRequestMsg,
userString, user.From.ID, user.JoinReason)) userString, user.From.ID, user.JoinReason))
keyboard := newApprovalKeyboard(user.From.ID) keyboard := NewApprovalKeyboard(user.From.ID)
edit.ReplyMarkup = &keyboard edit.ReplyMarkup = &keyboard
edit.ParseMode = api.ModeHTML edit.ParseMode = api.ModeHTML
bot.API.Send(edit) bot.API.Send(edit)
@ -149,7 +149,7 @@ func (bot *Bot) handleJoinRequest(request *api.ChatJoinRequest) {
} }
m = api.NewMessage(bot.Config.AdminChatId, m = api.NewMessage(bot.Config.AdminChatId,
fmt.Sprintf(AdminJoinRequestMsg, userString, request.From.ID, "(awaiting user response)")) fmt.Sprintf(AdminJoinRequestMsg, userString, request.From.ID, "(awaiting user response)"))
m.ReplyMarkup = newApprovalKeyboard(request.From.ID) m.ReplyMarkup = NewApprovalKeyboard(request.From.ID)
m.ParseMode = api.ModeHTML m.ParseMode = api.ModeHTML
if bot.Config.AdminChatTopicId != 0 { if bot.Config.AdminChatTopicId != 0 {
m.MessageThreadID = bot.Config.AdminChatTopicId m.MessageThreadID = bot.Config.AdminChatTopicId
@ -273,7 +273,7 @@ func (bot *Bot) sendFailureMessage(user *ExtendedChatJoinRequest, query *api.Cal
bot.API.Request(callback) bot.API.Request(callback)
} }
func escapeMarkdown(s string) string { func EscapeMarkdown(s string) string {
toEscape := []string{"*", "_", "`", "[", "]", "(", ")", "\\", "#", "-"} toEscape := []string{"*", "_", "`", "[", "]", "(", ")", "\\", "#", "-"}
replacements := make([]string, 0, len(toEscape)*2) replacements := make([]string, 0, len(toEscape)*2)
@ -285,7 +285,7 @@ func escapeMarkdown(s string) string {
return replacer.Replace(s) return replacer.Replace(s)
} }
func escapeHTML(s string) string { func EscapeHTML(s string) string {
toEscape := []string{"&", "<", ">", "\"", "'"} toEscape := []string{"&", "<", ">", "\"", "'"}
replacements := make([]string, 0, len(toEscape)*2) replacements := make([]string, 0, len(toEscape)*2)
@ -298,21 +298,29 @@ func escapeHTML(s string) string {
return replacer.Replace(s) return replacer.Replace(s)
} }
func newApprovalKeyboard(userID int64) api.InlineKeyboardMarkup { func NewApprovalKeyboard(userID int64) api.InlineKeyboardMarkup {
approveBtn := api.NewInlineKeyboardButtonData("Approve", fmt.Sprintf("approve_%d", userID)) approveBtn := api.NewInlineKeyboardButtonData("Approve", fmt.Sprintf("approve_%d", userID))
declineBtn := api.NewInlineKeyboardButtonData("Decline", fmt.Sprintf("decline_%d", userID)) declineBtn := api.NewInlineKeyboardButtonData("Decline", fmt.Sprintf("decline_%d", userID))
return api.NewInlineKeyboardMarkup([]api.InlineKeyboardButton{approveBtn, declineBtn}) return api.NewInlineKeyboardMarkup([]api.InlineKeyboardButton{approveBtn, declineBtn})
} }
func GetInfoFromMsg(msg string) (userId int64, username, joinReason, declinedBy, declinedAt string) { func GetInfoFromMsg(msg string) (userId int64, username, joinReason, declinedBy, declinedAt string) {
start := strings.Index(msg, "[")
end := strings.Index(msg, "]")
userID, _ := strconv.Atoi(msg[start+1 : end])
username = msg[31 : start-1]
lines := strings.Split(msg, "\n") lines := strings.Split(msg, "\n")
joinReason = strings.TrimPrefix(lines[2], "Join reason: ")
declinedBy = strings.TrimPrefix(lines[3], "Declined by: ") joinReason = string([]rune(lines[2])[len([]rune("Join reason: ")):])
declinedAt = strings.TrimPrefix(lines[4], "Declined at: ") declinedBy = string([]rune(lines[3])[len([]rune("Declined by: ")):])
declinedAt = string([]rune(lines[4])[len([]rune("Declined at: ")):])
index := LastIndexRuneInRunes([]rune(lines[0]), '[')
userID, _ := strconv.Atoi(string([]rune(lines[0])[index+1 : len([]rune(lines[0]))-1]))
return int64(userID), username, joinReason, declinedBy, declinedAt return int64(userID), username, joinReason, declinedBy, declinedAt
} }
func LastIndexRuneInRunes(runes []rune, r rune) int {
for i := len(runes) - 1; i >= 0; i-- {
if runes[i] == r {
return i
}
}
return -1
}