From: Eduardo Date: Sat, 30 Mar 2024 18:02:14 +0000 (+0100) Subject: issues are now managed too X-Git-Url: http://git.edufdez.es/?a=commitdiff_plain;h=refs%2Fheads%2Fmain;p=sylphiette-bot.git issues are now managed too --- diff --git a/telegram.go b/telegram.go index 65e17d4..aa39626 100644 --- a/telegram.go +++ b/telegram.go @@ -188,7 +188,7 @@ func manageSerieRequest(chatId int64, text string) error { response = "🎥" } - // get from text whatever is after the first - and the end of the line + // get from text whatever is after the first - and the end of the line response += " " + getFirstLine(getTextAfterDash(text)) + "\n" // request can be: new, available @@ -207,12 +207,42 @@ func manageSerieRequest(chatId int64, text string) error { func manageIssueMessage(chatId int64, text string) error { var err error + var response string = "⚠️" - // issue can be: comment, resolved, reported // issues can be type: video, audio, subtitle + var regexVideo = regexp.MustCompile(`(?i)\bVideo\b`) + var regexAudio = regexp.MustCompile(`(?i)\bAudio\b`) + var regexSubtitle = regexp.MustCompile(`(?i)\bSubtitle\b`) + + if regexVideo.MatchString(text) { + response += "📺" + } else if regexAudio.MatchString(text) { + response += "🔊" + } else if regexSubtitle.MatchString(text) { + response += "💬" + } - //TODO: manage issues + // issue can be: comment, resolved, reported + var regexComment = regexp.MustCompile(`(?i)\bComment\b`) + var regexResolved = regexp.MustCompile(`(?i)\bResolved\b`) + var regexReported = regexp.MustCompile(`(?i)\bReported\b`) + + if regexComment.MatchString(text) { + response += " " + getFirstLine(getTextAfterDash(text)) + "\n" + response += "🔍 Incidencia:\n" + getSubstringBetween(text, getFirstLine(getTextAfterDash(text)), "Comment from") + "\n\n" + response += "🔧 Comentario:\n" + getSubstringBetween(text, "Comment from "+getCommentedBy(text)+": ", "") + "\n" + } else if regexResolved.MatchString(text) { + response += " " + getFirstLine(getTextAfterDash(text)) + "\n" + response += "Reportado por " + getReportedBy(text) + "\n\n" + response += "🔍 Incidencia:\n" + getSubstringBetween(text, getFirstLine(getTextAfterDash(text)), "Reported By:") + "\n\n" + response += "🎉 Resuelto! " + } else if regexReported.MatchString(text) { + response += " " + getFirstLine(getTextAfterDash(text)) + "\n" + response += "Reportado por " + getReportedBy(text) + "\n\n" + response += "🔍 Incidencia:\n" + getSubstringBetween(text, getFirstLine(getTextAfterDash(text)), "Reported By:") + "\n" + } + err = sendTelegramPhoto(chatId, lastMsg.Photo[0].FileID, response) return err } diff --git a/utils.go b/utils.go index 9f1594d..28cae8d 100644 --- a/utils.go +++ b/utils.go @@ -45,6 +45,42 @@ func getRequestedBy(s string) string { return "???" } +// Search for the text after "Reported By:" in a string and returns it +func getReportedBy(s string) string { + re := regexp.MustCompile(`(?i)Reported By:\s*(.*)`) + match := re.FindStringSubmatch(s) + if len(match) > 1 { + return strings.TrimSpace(match[1]) + } + return "???" +} + +func getCommentedBy(s string) string { + res := getSubstringBetween(s, "Comment from", ":") + return strings.TrimSpace(res) +} + +// Get the substring between two strings in a text +func getSubstringBetween(s string, from string, to string) string { + startIndex := strings.Index(s, from) + if startIndex == -1 { + return "" + } + startIndex += len(from) + + if to == "" { + return strings.TrimSpace(s[startIndex:]) + } + + endIndex := strings.Index(s[startIndex:], to) + if endIndex == -1 { + return "" + } + endIndex += startIndex + + return strings.TrimSpace(s[startIndex:endIndex]) +} + func normaliceString(s string) string { t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC, runes.If(runes.In(unicode.Latin), width.Fold, nil))