🐛 修复原始消息内容中有html代码会被过滤的现象
This commit is contained in:
parent
017faf2594
commit
d36e8dde66
19
message.go
19
message.go
@ -8,7 +8,6 @@ import (
|
||||
"html"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -254,8 +253,7 @@ func (m *Message) Card() (*Card, error) {
|
||||
return nil, errors.New("card message required")
|
||||
}
|
||||
var card Card
|
||||
content := XmlFormString(m.Content)
|
||||
err := xml.Unmarshal([]byte(content), &card)
|
||||
err := xml.Unmarshal(stringToByte(m.Content), &card)
|
||||
return &card, err
|
||||
}
|
||||
|
||||
@ -265,8 +263,7 @@ func (m *Message) FriendAddMessageContent() (*FriendAddMessage, error) {
|
||||
return nil, errors.New("friend add message required")
|
||||
}
|
||||
var f FriendAddMessage
|
||||
content := XmlFormString(m.Content)
|
||||
err := xml.Unmarshal([]byte(content), &f)
|
||||
err := xml.Unmarshal(stringToByte(m.Content), &f)
|
||||
return &f, err
|
||||
}
|
||||
|
||||
@ -276,8 +273,7 @@ func (m *Message) RevokeMsg() (*RevokeMsg, error) {
|
||||
return nil, errors.New("recalled message required")
|
||||
}
|
||||
var r RevokeMsg
|
||||
content := XmlFormString(m.Content)
|
||||
err := xml.Unmarshal([]byte(content), &r)
|
||||
err := xml.Unmarshal(stringToByte(m.Content), &r)
|
||||
return &r, err
|
||||
}
|
||||
|
||||
@ -343,17 +339,12 @@ func (m *Message) init(bot *Bot) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if regexp.MustCompile(`^<`).MatchString(m.Content) {
|
||||
m.Content = html.UnescapeString(m.Content)
|
||||
}
|
||||
|
||||
// 处理消息中的换行
|
||||
m.Content = strings.Replace(m.Content, `<br/>`, "\n", -1)
|
||||
// 处理html转义字符
|
||||
m.Content = html.UnescapeString(m.Content)
|
||||
// 处理消息中的emoji表情
|
||||
m.Content = FormatEmoji(m.Content)
|
||||
if m.IsText() {
|
||||
m.Content = XmlFormString(m.Content)
|
||||
}
|
||||
}
|
||||
|
||||
// SendMessage 发送消息的结构体
|
||||
|
17
parser.go
17
parser.go
@ -7,15 +7,17 @@ import (
|
||||
"math/rand"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func ToBuffer(v interface{}) (*bytes.Buffer, error) {
|
||||
var buffer bytes.Buffer
|
||||
encoder := json.NewEncoder(&buffer)
|
||||
// 这里要设置进制html转义
|
||||
// 这里要设置禁止html转义
|
||||
encoder.SetEscapeHTML(false)
|
||||
err := encoder.Encode(v)
|
||||
return &buffer, err
|
||||
@ -25,6 +27,7 @@ func ToBuffer(v interface{}) (*bytes.Buffer, error) {
|
||||
func GetRandomDeviceId() string {
|
||||
rand.Seed(time.Now().Unix())
|
||||
var builder strings.Builder
|
||||
builder.Grow(16)
|
||||
builder.WriteString("e")
|
||||
for i := 0; i < 15; i++ {
|
||||
r := rand.Intn(9)
|
||||
@ -42,14 +45,6 @@ func getWebWxDataTicket(cookies []*http.Cookie) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// XmlFormString Form Xml 格式化
|
||||
func XmlFormString(text string) string {
|
||||
lt := strings.ReplaceAll(text, "<", "<")
|
||||
gt := strings.ReplaceAll(lt, ">", ">")
|
||||
br := strings.ReplaceAll(gt, "<br/>", "\n")
|
||||
return strings.ReplaceAll(br, "&amp;", "&")
|
||||
}
|
||||
|
||||
func getTotalDuration(delay ...time.Duration) time.Duration {
|
||||
var total time.Duration
|
||||
for _, d := range delay {
|
||||
@ -107,3 +102,7 @@ func scanJson(resp *http.Response, v interface{}) error {
|
||||
}
|
||||
return json.Unmarshal(buffer.Bytes(), v)
|
||||
}
|
||||
|
||||
func stringToByte(s string) []byte {
|
||||
return *(*[]byte)(unsafe.Pointer(&*(*reflect.StringHeader)(unsafe.Pointer(&s))))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user