Rhymen / go-whatsapp

WhatsApp Web API
MIT License
2.07k stars 492 forks source link

capture message catalog from whatsapp bussiness #517

Closed hrizal closed 3 years ago

hrizal commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

you are amazing bro ..

beshoo commented 3 years ago

@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