Skip to content

Commit

Permalink
sendfb: allow sending armadillo messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 9, 2024
1 parent 730b20c commit dcc66fc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
7 changes: 7 additions & 0 deletions proto/extra.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package armadillo

import (
"google.golang.org/protobuf/proto"

"go.mau.fi/whatsmeow/proto/waArmadilloApplication"
"go.mau.fi/whatsmeow/proto/waCommon"
"go.mau.fi/whatsmeow/proto/waConsumerApplication"
Expand All @@ -11,6 +13,11 @@ type MessageApplicationSub interface {
IsMessageApplicationSub()
}

type RealMessageApplicationSub interface {
MessageApplicationSub
proto.Message
}

type Unsupported_BusinessApplication waCommon.SubProtocol
type Unsupported_PaymentApplication waCommon.SubProtocol
type Unsupported_Voip waCommon.SubProtocol
Expand Down
64 changes: 50 additions & 14 deletions sendfb.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"google.golang.org/protobuf/proto"

waBinary "go.mau.fi/whatsmeow/binary"
armadillo "go.mau.fi/whatsmeow/proto"
"go.mau.fi/whatsmeow/proto/waArmadilloApplication"
"go.mau.fi/whatsmeow/proto/waCommon"
"go.mau.fi/whatsmeow/proto/waConsumerApplication"
"go.mau.fi/whatsmeow/proto/waMsgApplication"
Expand All @@ -35,12 +37,13 @@ import (
const FBMessageVersion = 3
const FBMessageApplicationVersion = 2
const FBConsumerMessageVersion = 1
const FBArmadilloMessageVersion = 1

// SendFBMessage sends the given v3 message to the given JID.
func (cli *Client) SendFBMessage(
ctx context.Context,
to types.JID,
message *waConsumerApplication.ConsumerApplication,
message armadillo.RealMessageApplicationSub,
metadata *waMsgApplication.MessageApplication_Metadata,
extra ...SendRequestExtra,
) (resp SendResponse, err error) {
Expand All @@ -51,9 +54,37 @@ func (cli *Client) SendFBMessage(
} else if len(extra) == 1 {
req = extra[0]
}
consumerMessage, err := proto.Marshal(message)
if err != nil {
err = fmt.Errorf("failed to marshal consumer message: %w", err)
var subproto waMsgApplication.MessageApplication_SubProtocolPayload
subproto.FutureProof = waCommon.FutureProofBehavior_PLACEHOLDER.Enum()
switch typedMsg := message.(type) {
case *waConsumerApplication.ConsumerApplication:
var consumerMessage []byte
consumerMessage, err = proto.Marshal(typedMsg)
if err != nil {
err = fmt.Errorf("failed to marshal consumer message: %w", err)
return
}
subproto.SubProtocol = &waMsgApplication.MessageApplication_SubProtocolPayload_ConsumerMessage{
ConsumerMessage: &waCommon.SubProtocol{
Payload: consumerMessage,
Version: proto.Int32(FBConsumerMessageVersion),
},
}
case *waArmadilloApplication.Armadillo:
var armadilloMessage []byte
armadilloMessage, err = proto.Marshal(typedMsg)
if err != nil {
err = fmt.Errorf("failed to marshal armadillo message: %w", err)
return
}
subproto.SubProtocol = &waMsgApplication.MessageApplication_SubProtocolPayload_Armadillo{
Armadillo: &waCommon.SubProtocol{
Payload: armadilloMessage,
Version: proto.Int32(FBArmadilloMessageVersion),
},
}
default:
err = fmt.Errorf("unsupported message type %T", message)
return
}
if metadata == nil {
Expand All @@ -65,15 +96,7 @@ func (cli *Client) SendFBMessage(
messageAppProto := &waMsgApplication.MessageApplication{
Payload: &waMsgApplication.MessageApplication_Payload{
Content: &waMsgApplication.MessageApplication_Payload_SubProtocol{
SubProtocol: &waMsgApplication.MessageApplication_SubProtocolPayload{
SubProtocol: &waMsgApplication.MessageApplication_SubProtocolPayload_ConsumerMessage{
ConsumerMessage: &waCommon.SubProtocol{
Payload: consumerMessage,
Version: proto.Int32(FBConsumerMessageVersion),
},
},
FutureProof: waCommon.FutureProofBehavior_PLACEHOLDER.Enum(),
},
SubProtocol: &subproto,
},
},
Metadata: metadata,
Expand Down Expand Up @@ -310,7 +333,20 @@ type messageAttrs struct {
PollType string
}

func getAttrsFromFBMessage(msg *waConsumerApplication.ConsumerApplication) (attrs messageAttrs) {
func getAttrsFromFBMessage(msg armadillo.MessageApplicationSub) (attrs messageAttrs) {
switch typedMsg := msg.(type) {
case *waConsumerApplication.ConsumerApplication:
return getAttrsFromFBConsumerMessage(typedMsg)
case *waArmadilloApplication.Armadillo:
attrs.Type = "media"
attrs.MediaType = "document"
default:
attrs.Type = "text"
}
return
}

func getAttrsFromFBConsumerMessage(msg *waConsumerApplication.ConsumerApplication) (attrs messageAttrs) {
switch payload := msg.GetPayload().GetPayload().(type) {
case *waConsumerApplication.ConsumerApplication_Payload_Content:
switch content := payload.Content.GetContent().(type) {
Expand Down

0 comments on commit dcc66fc

Please sign in to comment.