Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capture message catalog from whatsapp bussiness #517

Closed
hrizal opened this issue Feb 3, 2021 · 4 comments
Closed

capture message catalog from whatsapp bussiness #517

hrizal opened this issue Feb 3, 2021 · 4 comments

Comments

@hrizal
Copy link

hrizal commented Feb 3, 2021

As we know, we can create a catalog in the WhatsApp business, and those who contact can order products from the catalog into the shopping cart.

When the shopping cart is delivered,
the message the API receives is just a blank text, has anyone caught the shopping cart message?

@Romerito007
Copy link
Contributor

As we know, we can create a catalog in the WhatsApp business, and those who contact can order products from the catalog into the shopping cart.

When the shopping cart is delivered,
the message the API receives is just a blank text, has anyone caught the shopping cart message?

I implemented message received, I did not test the sending. ProductMessage and OrderMessage

@Romerito007
Copy link
Contributor

this is my implementation

`/*
OrderMessage represents a order message.
*/

type OrderMessage struct {
Info MessageInfo
OrderId string
Thumbnail []byte
ItemCount int32
Status proto.OrderMessage_OrderMessageOrderStatus
Surface proto.OrderMessage_OrderMessageOrderSurface
Message string
OrderTitle string
SellerJid string
Token string
ContextInfo ContextInfo
}

func getOrderMessage(msg *proto.WebMessageInfo) OrderMessage {
order := msg.GetMessage().GetOrderMessage()

orderMessage := OrderMessage{
	Info:        getMessageInfo(msg),
	OrderId:     order.GetOrderId(),
	Thumbnail:   order.GetThumbnail(),
	ItemCount:   order.GetItemCount(),
	Status:      order.GetStatus(),
	Surface:     order.GetSurface(),
	Message:     order.GetMessage(),
	OrderTitle:  order.GetOrderTitle(),
	SellerJid:   order.GetSellerJid(),
	Token:       order.GetToken(),
	ContextInfo: getMessageContext(order.GetContextInfo()),
}

return orderMessage

}

func getOrderMessageProto(msg OrderMessage) *proto.WebMessageInfo {
p := getInfoProto(&msg.Info)
contextInfo := getContextInfoProto(&msg.ContextInfo)

p.Message = &proto.Message{
	OrderMessage: &proto.OrderMessage{
		Thumbnail:   msg.Thumbnail,
		ItemCount:   &msg.ItemCount,
		Status:      &msg.Status,
		Surface:     &msg.Surface,
		Message:     &msg.Message,
		OrderTitle:  &msg.OrderTitle,
		SellerJid:   &msg.SellerJid,
		Token:       &msg.Token,
		ContextInfo: contextInfo,
	},
}

return p

}`

`/*
ProductMessage represents a product message.
*/

type ProductMessage struct {
Info MessageInfo
Product *proto.ProductSnapshot
BusinessOwnerJid string
Catalog *proto.CatalogSnapshot
ContextInfo ContextInfo
}

func getProductMessage(msg *proto.WebMessageInfo) ProductMessage {
prod := msg.GetMessage().GetProductMessage()

productMessage := ProductMessage{
	Info:             getMessageInfo(msg),
	Product:          prod.GetProduct(),
	BusinessOwnerJid: prod.GetBusinessOwnerJid(),
	Catalog:          prod.GetCatalog(),
	ContextInfo:      getMessageContext(prod.GetContextInfo()),
}

return productMessage

}

func getProductMessageProto(msg ProductMessage) *proto.WebMessageInfo {
p := getInfoProto(&msg.Info)
contextInfo := getContextInfoProto(&msg.ContextInfo)

p.Message = &proto.Message{
	ProductMessage: &proto.ProductMessage{
		Product:          msg.Product,
		BusinessOwnerJid: &msg.BusinessOwnerJid,
		Catalog:          msg.Catalog,
		ContextInfo:      contextInfo,
	},
}

return p

}`

`func ParseProtoMessage(msg *proto.WebMessageInfo) interface{} {

switch {

......

case msg.GetMessage().GetProductMessage() != nil:
	return getProductMessage(msg)

case msg.GetMessage().GetOrderMessage() != nil:
	return getOrderMessage(msg)

default:
	//cannot match message
	return ErrMessageTypeNotImplemented
}

}`

`/*
The ProductMessageHandler interface needs to be implemented to receive product messages dispatched by the dispatcher.
*/
type ProductMessageHandler interface {
Handler
HandleProductMessage(message ProductMessage)
}

/*
The OrderMessageHandler interface needs to be implemented to receive order messages dispatched by the dispatcher.
*/
type OrderMessageHandler interface {
Handler
HandleOrderMessage(message OrderMessage)
}`

`func (wac *Conn) handleWithCustomHandlers(message interface{}, handlers []Handler) {
switch m := message.(type) {
.........

case ProductMessage:
	for _, h := range handlers {
		if x, ok := h.(ProductMessageHandler); ok {
			if wac.shouldCallSynchronously(h) {
				x.HandleProductMessage(m)
			} else {
				go x.HandleProductMessage(m)
			}
		}
	}

case OrderMessage:
	for _, h := range handlers {
		if x, ok := h.(OrderMessageHandler); ok {
			if wac.shouldCallSynchronously(h) {
				x.HandleOrderMessage(m)
			} else {
				go x.HandleOrderMessage(m)
			}
		}
	}

     ........................

}`

@hrizal
Copy link
Author

hrizal commented Feb 4, 2021

you are amazing bro ..

@hrizal hrizal closed this as completed Feb 4, 2021
@beshoo
Copy link
Collaborator

beshoo commented Feb 4, 2021

@Romerito007 thanks for this great idea.
I would like to ask you to create a PL, so we can have this in the lib and all of us will enjoy this great option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants