tulir / whatsmeow

Go library for the WhatsApp web multidevice API
https://go.mau.fi/whatsmeow
Mozilla Public License 2.0
2.29k stars 430 forks source link

Text message sometimes empty string #588

Closed ekasetiawans closed 6 months ago

ekasetiawans commented 6 months ago

I’m facing issue that sometimes received message receiving a empty string. It’s only occured on some whatsapp account. Is there any explanation why this happen?

SoursopID commented 6 months ago

It's kinda hard to figure it out since you didn't provide more information about your case.

ekasetiawans commented 6 months ago

I just using simpel example whatsmeow in godoc, the conversation message has empty string

SoursopID commented 6 months ago

IINW, there's 2 type of text message in WhatsApp proto, Conversation & ExtendedTextMessage

Conversation commonly used in regular chat message that doesn't use Disappearing timer and then ExtendedTextMessage is a rich text message that can containText, Link preview, Ads, and other properties like quoting, forwarding, Expiration (for disappearing timer) .

reff : https://pkg.go.dev/go.mau.fi/whatsmeow@v0.0.0-20240507080416-01b0547014dc/binary/proto#Message

ekasetiawans commented 6 months ago

So we need to check which the correct message type right?

lucasvmx commented 6 months ago

So we need to check which the correct message type right?

Right.

You can use this function to check:

func hasTextMessage(msg *proto.Message) bool {
    extendedMsg := msg.GetExtendedTextMessage()

    return len(msg.GetConversation()) > 0 ||
        len(extendedMsg.GetContextInfo().GetQuotedMessage().GetConversation()) > 0 ||
        len(extendedMsg.GetText()) > 0
}
h4775346 commented 4 months ago

So we need to check which the correct message type right?

Right.

You can use this function to check:

func hasTextMessage(msg *proto.Message) bool {
  extendedMsg := msg.GetExtendedTextMessage()

  return len(msg.GetConversation()) > 0 ||
      len(extendedMsg.GetContextInfo().GetQuotedMessage().GetConversation()) > 0 ||
      len(extendedMsg.GetText()) > 0
}

Your Answer Worked For me after changing some code could be perfect

func GetMessageFromEvt(msg *waProto.Message) string {
    extendedMsg := msg.GetExtendedTextMessage()

    if len(msg.GetConversation()) > 0 {
        return msg.GetConversation()
    }
    if len(extendedMsg.GetContextInfo().GetQuotedMessage().GetConversation()) > 0 {
        return extendedMsg.GetContextInfo().GetQuotedMessage().GetConversation()
    }

    if len(extendedMsg.GetText()) > 0 {
        return extendedMsg.GetText()
    }
    return ""
}