summaryrefslogtreecommitdiffstats
path: root/bridge/whatsappmulti/whatsapp.go
diff options
context:
space:
mode:
Diffstat (limited to 'bridge/whatsappmulti/whatsapp.go')
-rw-r--r--bridge/whatsappmulti/whatsapp.go72
1 files changed, 55 insertions, 17 deletions
diff --git a/bridge/whatsappmulti/whatsapp.go b/bridge/whatsappmulti/whatsapp.go
index 76e4ae46..6798d827 100644
--- a/bridge/whatsappmulti/whatsapp.go
+++ b/bridge/whatsappmulti/whatsapp.go
@@ -42,6 +42,11 @@ type Bwhatsapp struct {
userAvatars map[string]string
}
+type Replyable struct {
+ MessageID types.MessageID
+ Sender types.JID
+}
+
// New Create a new WhatsApp bridge. This will be called for each [whatsapp.<server>] entry you have in the config file
func New(cfg *bridge.Config) bridge.Bridger {
number := cfg.GetString(cfgNumber)
@@ -222,6 +227,10 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st
// Post document message
var message proto.Message
+ var ctx *proto.ContextInfo
+ if msg.ParentID != "" {
+ ctx, _ = b.getNewReplyContext(msg.ParentID)
+ }
message.DocumentMessage = &proto.DocumentMessage{
Title: &fi.Name,
@@ -233,6 +242,7 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
+ ContextInfo: ctx,
}
b.Log.Debugf("=> Sending %#v as a document", msg)
@@ -246,8 +256,6 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st
// Post an image message from the bridge to WhatsApp
// Handle, for sure image/jpeg, image/png and image/gif MIME types
func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (string, error) {
- groupJID, _ := types.ParseJID(msg.Channel)
-
fi := msg.Extra["file"][0].(config.FileInfo)
caption := msg.Username + fi.Comment
@@ -258,6 +266,10 @@ func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (strin
}
var message proto.Message
+ var ctx *proto.ContextInfo
+ if msg.ParentID != "" {
+ ctx, _ = b.getNewReplyContext(msg.ParentID)
+ }
message.ImageMessage = &proto.ImageMessage{
Mimetype: &filetype,
@@ -267,20 +279,16 @@ func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (strin
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
+ ContextInfo: ctx,
}
b.Log.Debugf("=> Sending %#v as an image", msg)
- ID := whatsmeow.GenerateMessageID()
- _, err = b.wc.SendMessage(context.TODO(), groupJID, &message, whatsmeow.SendRequestExtra{ID: ID})
-
- return ID, err
+ return b.sendMessage(msg, &message)
}
// Post a video message from the bridge to WhatsApp
func (b *Bwhatsapp) PostVideoMessage(msg config.Message, filetype string) (string, error) {
- groupJID, _ := types.ParseJID(msg.Channel)
-
fi := msg.Extra["file"][0].(config.FileInfo)
caption := msg.Username + fi.Comment
@@ -291,6 +299,10 @@ func (b *Bwhatsapp) PostVideoMessage(msg config.Message, filetype string) (strin
}
var message proto.Message
+ var ctx *proto.ContextInfo
+ if msg.ParentID != "" {
+ ctx, _ = b.getNewReplyContext(msg.ParentID)
+ }
message.VideoMessage = &proto.VideoMessage{
Mimetype: &filetype,
@@ -300,14 +312,12 @@ func (b *Bwhatsapp) PostVideoMessage(msg config.Message, filetype string) (strin
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
+ ContextInfo: ctx,
}
b.Log.Debugf("=> Sending %#v as a video", msg)
- ID := whatsmeow.GenerateMessageID()
- _, err = b.wc.SendMessage(context.TODO(), groupJID, &message, whatsmeow.SendRequestExtra{ID: ID})
-
- return ID, err
+ return b.sendMessage(msg, &message)
}
// Post audio inline
@@ -322,6 +332,10 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin
}
var message proto.Message
+ var ctx *proto.ContextInfo
+ if msg.ParentID != "" {
+ ctx, _ = b.getNewReplyContext(msg.ParentID)
+ }
message.AudioMessage = &proto.AudioMessage{
Mimetype: &filetype,
@@ -330,12 +344,12 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin
FileSha256: resp.FileSHA256,
FileLength: goproto.Uint64(resp.FileLength),
Url: &resp.URL,
+ ContextInfo: ctx,
}
b.Log.Debugf("=> Sending %#v as audio", msg)
- ID := whatsmeow.GenerateMessageID()
- _, err = b.wc.SendMessage(context.TODO(), groupJID, &message, whatsmeow.SendRequestExtra{ID: ID})
+ ID, err := b.sendMessage(msg, &message)
var captionMessage proto.Message
caption := msg.Username + fi.Comment + "\u2B06" // the char on the end is upwards arrow emoji
@@ -351,6 +365,9 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin
func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
groupJID, _ := types.ParseJID(msg.Channel)
+ extendedMsgID, _ := b.parseMessageID(msg.ID)
+ msg.ID = extendedMsgID.MessageID
+
b.Log.Debugf("=> Receiving %#v", msg)
// Delete message
@@ -400,14 +417,35 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
}
}
+ var message proto.Message
text := msg.Username + msg.Text
- var message proto.Message
+ // If we have a parent ID send an extended message
+ if msg.ParentID != "" {
+ replyContext, err := b.getNewReplyContext(msg.ParentID)
+
+ if err == nil {
+ message = proto.Message{
+ ExtendedTextMessage: &proto.ExtendedTextMessage{
+ Text: &text,
+ ContextInfo: replyContext,
+ },
+ }
+
+ return b.sendMessage(msg, &message)
+ }
+ }
message.Conversation = &text
+ return b.sendMessage(msg, &message)
+}
+
+func (b *Bwhatsapp) sendMessage(rmsg config.Message, message *proto.Message) (string, error) {
+ groupJID, _ := types.ParseJID(rmsg.Channel)
ID := whatsmeow.GenerateMessageID()
- _, err := b.wc.SendMessage(context.TODO(), groupJID, &message, whatsmeow.SendRequestExtra{ID: ID})
- return ID, err
+ _, err := b.wc.SendMessage(context.Background(), groupJID, message, whatsmeow.SendRequestExtra{ID: ID})
+
+ return getMessageIdFormat(*b.wc.Store.ID, ID), err
}