Skip to content

Commit dcc66fc

Browse files
committed
sendfb: allow sending armadillo messages
1 parent 730b20c commit dcc66fc

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

proto/extra.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package armadillo
22

33
import (
4+
"google.golang.org/protobuf/proto"
5+
46
"go.mau.fi/whatsmeow/proto/waArmadilloApplication"
57
"go.mau.fi/whatsmeow/proto/waCommon"
68
"go.mau.fi/whatsmeow/proto/waConsumerApplication"
@@ -11,6 +13,11 @@ type MessageApplicationSub interface {
1113
IsMessageApplicationSub()
1214
}
1315

16+
type RealMessageApplicationSub interface {
17+
MessageApplicationSub
18+
proto.Message
19+
}
20+
1421
type Unsupported_BusinessApplication waCommon.SubProtocol
1522
type Unsupported_PaymentApplication waCommon.SubProtocol
1623
type Unsupported_Voip waCommon.SubProtocol

sendfb.go

+50-14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"google.golang.org/protobuf/proto"
2525

2626
waBinary "go.mau.fi/whatsmeow/binary"
27+
armadillo "go.mau.fi/whatsmeow/proto"
28+
"go.mau.fi/whatsmeow/proto/waArmadilloApplication"
2729
"go.mau.fi/whatsmeow/proto/waCommon"
2830
"go.mau.fi/whatsmeow/proto/waConsumerApplication"
2931
"go.mau.fi/whatsmeow/proto/waMsgApplication"
@@ -35,12 +37,13 @@ import (
3537
const FBMessageVersion = 3
3638
const FBMessageApplicationVersion = 2
3739
const FBConsumerMessageVersion = 1
40+
const FBArmadilloMessageVersion = 1
3841

3942
// SendFBMessage sends the given v3 message to the given JID.
4043
func (cli *Client) SendFBMessage(
4144
ctx context.Context,
4245
to types.JID,
43-
message *waConsumerApplication.ConsumerApplication,
46+
message armadillo.RealMessageApplicationSub,
4447
metadata *waMsgApplication.MessageApplication_Metadata,
4548
extra ...SendRequestExtra,
4649
) (resp SendResponse, err error) {
@@ -51,9 +54,37 @@ func (cli *Client) SendFBMessage(
5154
} else if len(extra) == 1 {
5255
req = extra[0]
5356
}
54-
consumerMessage, err := proto.Marshal(message)
55-
if err != nil {
56-
err = fmt.Errorf("failed to marshal consumer message: %w", err)
57+
var subproto waMsgApplication.MessageApplication_SubProtocolPayload
58+
subproto.FutureProof = waCommon.FutureProofBehavior_PLACEHOLDER.Enum()
59+
switch typedMsg := message.(type) {
60+
case *waConsumerApplication.ConsumerApplication:
61+
var consumerMessage []byte
62+
consumerMessage, err = proto.Marshal(typedMsg)
63+
if err != nil {
64+
err = fmt.Errorf("failed to marshal consumer message: %w", err)
65+
return
66+
}
67+
subproto.SubProtocol = &waMsgApplication.MessageApplication_SubProtocolPayload_ConsumerMessage{
68+
ConsumerMessage: &waCommon.SubProtocol{
69+
Payload: consumerMessage,
70+
Version: proto.Int32(FBConsumerMessageVersion),
71+
},
72+
}
73+
case *waArmadilloApplication.Armadillo:
74+
var armadilloMessage []byte
75+
armadilloMessage, err = proto.Marshal(typedMsg)
76+
if err != nil {
77+
err = fmt.Errorf("failed to marshal armadillo message: %w", err)
78+
return
79+
}
80+
subproto.SubProtocol = &waMsgApplication.MessageApplication_SubProtocolPayload_Armadillo{
81+
Armadillo: &waCommon.SubProtocol{
82+
Payload: armadilloMessage,
83+
Version: proto.Int32(FBArmadilloMessageVersion),
84+
},
85+
}
86+
default:
87+
err = fmt.Errorf("unsupported message type %T", message)
5788
return
5889
}
5990
if metadata == nil {
@@ -65,15 +96,7 @@ func (cli *Client) SendFBMessage(
6596
messageAppProto := &waMsgApplication.MessageApplication{
6697
Payload: &waMsgApplication.MessageApplication_Payload{
6798
Content: &waMsgApplication.MessageApplication_Payload_SubProtocol{
68-
SubProtocol: &waMsgApplication.MessageApplication_SubProtocolPayload{
69-
SubProtocol: &waMsgApplication.MessageApplication_SubProtocolPayload_ConsumerMessage{
70-
ConsumerMessage: &waCommon.SubProtocol{
71-
Payload: consumerMessage,
72-
Version: proto.Int32(FBConsumerMessageVersion),
73-
},
74-
},
75-
FutureProof: waCommon.FutureProofBehavior_PLACEHOLDER.Enum(),
76-
},
99+
SubProtocol: &subproto,
77100
},
78101
},
79102
Metadata: metadata,
@@ -310,7 +333,20 @@ type messageAttrs struct {
310333
PollType string
311334
}
312335

313-
func getAttrsFromFBMessage(msg *waConsumerApplication.ConsumerApplication) (attrs messageAttrs) {
336+
func getAttrsFromFBMessage(msg armadillo.MessageApplicationSub) (attrs messageAttrs) {
337+
switch typedMsg := msg.(type) {
338+
case *waConsumerApplication.ConsumerApplication:
339+
return getAttrsFromFBConsumerMessage(typedMsg)
340+
case *waArmadilloApplication.Armadillo:
341+
attrs.Type = "media"
342+
attrs.MediaType = "document"
343+
default:
344+
attrs.Type = "text"
345+
}
346+
return
347+
}
348+
349+
func getAttrsFromFBConsumerMessage(msg *waConsumerApplication.ConsumerApplication) (attrs messageAttrs) {
314350
switch payload := msg.GetPayload().GetPayload().(type) {
315351
case *waConsumerApplication.ConsumerApplication_Payload_Content:
316352
switch content := payload.Content.GetContent().(type) {

0 commit comments

Comments
 (0)