mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-12-14 17:33:48 +01:00
Add WhatsApp->Matrix formatting
This commit is contained in:
parent
a7f7295528
commit
0b7d23e897
2 changed files with 31 additions and 2 deletions
|
@ -20,9 +20,9 @@
|
||||||
* [ ] Topic
|
* [ ] Topic
|
||||||
* [ ] Initial room metadata
|
* [ ] Initial room metadata
|
||||||
* WhatsApp → Matrix
|
* WhatsApp → Matrix
|
||||||
* [ ] Message content
|
* [x] Message content
|
||||||
* [x] Plain text
|
* [x] Plain text
|
||||||
* [ ] Formatted messages
|
* [x] Formatted messages
|
||||||
* [x] Media/files
|
* [x] Media/files
|
||||||
* [ ] Message deletions
|
* [ ] Message deletions
|
||||||
* [x] Avatars
|
* [x] Avatars
|
||||||
|
|
29
portal.go
29
portal.go
|
@ -20,10 +20,12 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
"image"
|
"image"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -299,7 +301,26 @@ func (portal *Portal) SetReply(content *gomatrix.Content, info whatsapp.MessageI
|
||||||
content.SetReply(event)
|
content.SetReply(event)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var codeBlockRegex = regexp.MustCompile("```((?:.|\n)+?)```")
|
||||||
|
var italicRegex = regexp.MustCompile("([\\s>~*]|^)_(.+?)_([^a-zA-Z\\d]|$)")
|
||||||
|
var boldRegex = regexp.MustCompile("([\\s>_~]|^)\\*(.+?)\\*([^a-zA-Z\\d]|$)")
|
||||||
|
var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)")
|
||||||
|
|
||||||
|
var whatsAppFormat = map[*regexp.Regexp]string{
|
||||||
|
codeBlockRegex: "<pre>$1</pre>",
|
||||||
|
italicRegex: "$1<em>$2</em>$3",
|
||||||
|
boldRegex: "$1<strong>$2</strong>$3",
|
||||||
|
strikethroughRegex: "$1<del>$2</del>$3",
|
||||||
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) ParseWhatsAppFormat(input string) string {
|
||||||
|
output := html.EscapeString(input)
|
||||||
|
for regex, replacement := range whatsAppFormat {
|
||||||
|
output = regex.ReplaceAllString(output, replacement)
|
||||||
|
}
|
||||||
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
||||||
|
@ -322,8 +343,15 @@ func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
||||||
Body: message.Text,
|
Body: message.Text,
|
||||||
MsgType: gomatrix.MsgText,
|
MsgType: gomatrix.MsgText,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
htmlBody := portal.ParseWhatsAppFormat(message.Text)
|
||||||
|
if htmlBody != message.Text {
|
||||||
|
content.FormattedBody = htmlBody
|
||||||
|
content.Format = gomatrix.FormatHTML
|
||||||
|
}
|
||||||
portal.SetReply(&content, message.Info)
|
portal.SetReply(&content, message.Info)
|
||||||
|
|
||||||
|
intent.UserTyping(portal.MXID, false, 0)
|
||||||
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(message.Info.Timestamp*1000))
|
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(message.Info.Timestamp*1000))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err)
|
portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err)
|
||||||
|
@ -407,6 +435,7 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn
|
||||||
content.MsgType = gomatrix.MsgFile
|
content.MsgType = gomatrix.MsgFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intent.UserTyping(portal.MXID, false, 0)
|
||||||
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(info.Timestamp*1000))
|
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(info.Timestamp*1000))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Errorfln("Failed to handle message %s: %v", info.Id, err)
|
portal.log.Errorfln("Failed to handle message %s: %v", info.Id, err)
|
||||||
|
|
Loading…
Reference in a new issue