mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-12-15 01:43:49 +01:00
Add basic bridging of contact messages to Matrix
This commit is contained in:
parent
934f98119f
commit
e4a160e650
4 changed files with 48 additions and 1 deletions
2
go.mod
2
go.mod
|
@ -15,7 +15,7 @@ require (
|
|||
gopkg.in/yaml.v2 v2.3.0
|
||||
maunium.net/go/mauflag v1.0.0
|
||||
maunium.net/go/maulogger/v2 v2.1.1
|
||||
maunium.net/go/mautrix v0.4.11
|
||||
maunium.net/go/mautrix v0.5.0-rc.2
|
||||
)
|
||||
|
||||
replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.2.8
|
||||
|
|
2
go.sum
2
go.sum
|
@ -65,3 +65,5 @@ maunium.net/go/mautrix v0.4.9 h1:zp4ihCuSfonsTbwOmBH5/akWwp4QEii+SU5QRAC9Foo=
|
|||
maunium.net/go/mautrix v0.4.9/go.mod h1:8Y+NqmROJyWYvvP4yPfX9tLM59VCfgE/kcQ0SeX68ho=
|
||||
maunium.net/go/mautrix v0.4.11 h1:cONVoAkD7AOvtzEMvuuq79Y+2vRrNKfpKZoR8HdyPAw=
|
||||
maunium.net/go/mautrix v0.4.11/go.mod h1:LnkFnB1yjCbb8V+upoEHDGvI/F38NHSTWYCe2RRJgSY=
|
||||
maunium.net/go/mautrix v0.5.0-rc.2 h1:ohx+dprvMS6Txm+suMx5pbjl0rjDpfftFxgXhx/+Usc=
|
||||
maunium.net/go/mautrix v0.5.0-rc.2/go.mod h1:LnkFnB1yjCbb8V+upoEHDGvI/F38NHSTWYCe2RRJgSY=
|
||||
|
|
41
portal.go
41
portal.go
|
@ -205,6 +205,8 @@ func (portal *Portal) handleMessage(msg PortalMessage) {
|
|||
portal.HandleMediaMessage(msg.source, data.Download, nil, data.Info, data.ContextInfo, data.Type, "", false)
|
||||
case whatsapp.DocumentMessage:
|
||||
portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Title, false)
|
||||
case whatsapp.ContactMessage:
|
||||
portal.HandleContactMessage(msg.source, data)
|
||||
case whatsappExt.MessageRevocation:
|
||||
portal.HandleMessageRevoke(msg.source, data)
|
||||
case FakeMessage:
|
||||
|
@ -1087,6 +1089,45 @@ func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessa
|
|||
portal.finishHandling(source, message.Info.Source, resp.EventID)
|
||||
}
|
||||
|
||||
func (portal *Portal) HandleContactMessage(source *User, message whatsapp.ContactMessage) {
|
||||
if !portal.startHandling(message.Info) {
|
||||
return
|
||||
}
|
||||
|
||||
intent := portal.GetMessageIntent(source, message.Info)
|
||||
if intent == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%s.vcf", message.DisplayName)
|
||||
|
||||
uploadResp, err := intent.UploadBytesWithName([]byte(message.Vcard), "text/vcard", fileName)
|
||||
if err != nil {
|
||||
portal.log.Errorfln("Failed to upload vcard of %s: %v", message.DisplayName, err)
|
||||
return
|
||||
}
|
||||
|
||||
content := &event.MessageEventContent{
|
||||
Body: fileName,
|
||||
MsgType: event.MsgFile,
|
||||
URL: uploadResp.ContentURI.CUString(),
|
||||
Info: &event.FileInfo{
|
||||
MimeType: "text/vcard",
|
||||
Size: len(message.Vcard),
|
||||
},
|
||||
}
|
||||
|
||||
portal.SetReply(content, message.ContextInfo)
|
||||
|
||||
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
||||
resp, err := portal.sendMessage(intent, event.EventMessage, content, int64(message.Info.Timestamp*1000))
|
||||
if err != nil {
|
||||
portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err)
|
||||
return
|
||||
}
|
||||
portal.finishHandling(source, message.Info.Source, resp.EventID)
|
||||
}
|
||||
|
||||
func (portal *Portal) sendMediaBridgeFailure(source *User, intent *appservice.IntentAPI, info whatsapp.MessageInfo, downloadErr error) {
|
||||
portal.log.Errorfln("Failed to download media for %s: %v", info.Id, downloadErr)
|
||||
resp, err := portal.sendMessage(intent, event.EventMessage, &event.MessageEventContent{
|
||||
|
|
4
user.go
4
user.go
|
@ -694,6 +694,10 @@ func (user *User) HandleDocumentMessage(message whatsapp.DocumentMessage) {
|
|||
user.putMessage(PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp})
|
||||
}
|
||||
|
||||
func (user *User) HandleContactMessage(message whatsapp.ContactMessage) {
|
||||
user.putMessage(PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp})
|
||||
}
|
||||
|
||||
func (user *User) HandleMessageRevoke(message whatsappExt.MessageRevocation) {
|
||||
user.putMessage(PortalMessage{message.RemoteJid, user, message, 0})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue