package handlers import ( "encoding/json" "fmt" "regexp" "strconv" "strings" ) // func sendCard func msgFilter(msg string) string { //replace @到下一个非空的字段 为 '' regex := regexp.MustCompile(`@[^ ]*`) return regex.ReplaceAllString(msg, "") } func parseContent(content string) string { //"{\"text\":\"@_user_1 hahaha\"}", //only get text content hahaha var contentMap map[string]interface{} err := json.Unmarshal([]byte(content), &contentMap) if err != nil { fmt.Println(err) } if contentMap["text"] == nil { return "" } text := contentMap["text"].(string) return msgFilter(text) } func processMessage(msg interface{}) (string, error) { msg = strings.TrimSpace(msg.(string)) msgB, err := json.Marshal(msg) if err != nil { return "", err } msgStr := string(msgB) if len(msgStr) >= 2 { msgStr = msgStr[1 : len(msgStr)-1] } return msgStr, nil } func processNewLine(msg string) string { return strings.Replace(msg, "\\n", ` `, -1) } func processQuote(msg string) string { return strings.Replace(msg, "\\\"", "\"", -1) } // 将字符中 \u003c 替换为 < 等等 func processUnicode(msg string) string { regex := regexp.MustCompile(`\\u[0-9a-fA-F]{4}`) return regex.ReplaceAllStringFunc(msg, func(s string) string { r, _ := regexp.Compile(`\\u`) s = r.ReplaceAllString(s, "") i, _ := strconv.ParseInt(s, 16, 32) return string(rune(i)) }) } func cleanTextBlock(msg string) string { msg = processNewLine(msg) msg = processUnicode(msg) msg = processQuote(msg) return msg } func parseFileKey(content string) string { var contentMap map[string]interface{} err := json.Unmarshal([]byte(content), &contentMap) if err != nil { fmt.Println(err) return "" } if contentMap["file_key"] == nil { return "" } fileKey := contentMap["file_key"].(string) return fileKey } func parseImageKey(content string) string { var contentMap map[string]interface{} err := json.Unmarshal([]byte(content), &contentMap) if err != nil { fmt.Println(err) return "" } if contentMap["image_key"] == nil { return "" } imageKey := contentMap["image_key"].(string) return imageKey }