diff --git a/main.go b/main.go index 50e3f03..73c8033 100644 --- a/main.go +++ b/main.go @@ -87,10 +87,10 @@ func main() { userString = fmt.Sprintf("%s %s", update.Message.From.FirstName, update.Message.From.LastName) } 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) 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", reason)) b.API.Send(m) @@ -108,7 +108,7 @@ func main() { func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update *api.Message) { if user.JoinReason == "" { - user.JoinReason = escapeHTML(update.Text) + user.JoinReason = EscapeHTML(update.Text) userString := "" if user.From.UserName != "" { @@ -119,7 +119,7 @@ func (bot *Bot) handleJoinRequestResponse(user *ExtendedChatJoinRequest, update edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID, fmt.Sprintf(AdminJoinRequestMsg, userString, user.From.ID, user.JoinReason)) - keyboard := newApprovalKeyboard(user.From.ID) + keyboard := NewApprovalKeyboard(user.From.ID) edit.ReplyMarkup = &keyboard edit.ParseMode = api.ModeHTML bot.API.Send(edit) @@ -149,7 +149,7 @@ func (bot *Bot) handleJoinRequest(request *api.ChatJoinRequest) { } m = api.NewMessage(bot.Config.AdminChatId, 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 if bot.Config.AdminChatTopicId != 0 { m.MessageThreadID = bot.Config.AdminChatTopicId @@ -273,7 +273,7 @@ func (bot *Bot) sendFailureMessage(user *ExtendedChatJoinRequest, query *api.Cal bot.API.Request(callback) } -func escapeMarkdown(s string) string { +func EscapeMarkdown(s string) string { toEscape := []string{"*", "_", "`", "[", "]", "(", ")", "\\", "#", "-"} replacements := make([]string, 0, len(toEscape)*2) @@ -285,7 +285,7 @@ func escapeMarkdown(s string) string { return replacer.Replace(s) } -func escapeHTML(s string) string { +func EscapeHTML(s string) string { toEscape := []string{"&", "<", ">", "\"", "'"} replacements := make([]string, 0, len(toEscape)*2) @@ -298,21 +298,29 @@ func escapeHTML(s string) string { 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)) declineBtn := api.NewInlineKeyboardButtonData("Decline", fmt.Sprintf("decline_%d", userID)) return api.NewInlineKeyboardMarkup([]api.InlineKeyboardButton{approveBtn, declineBtn}) } 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") - joinReason = strings.TrimPrefix(lines[2], "Join reason: ") - declinedBy = strings.TrimPrefix(lines[3], "Declined by: ") - declinedAt = strings.TrimPrefix(lines[4], "Declined at: ") + + joinReason = string([]rune(lines[2])[len([]rune("Join reason: ")):]) + 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 } + +func LastIndexRuneInRunes(runes []rune, r rune) int { + for i := len(runes) - 1; i >= 0; i-- { + if runes[i] == r { + return i + } + } + return -1 +}