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

Text message sometimes empty string #588

Closed
ekasetiawans opened this issue May 18, 2024 · 7 comments
Closed

Text message sometimes empty string #588

ekasetiawans opened this issue May 18, 2024 · 7 comments

Comments

@ekasetiawans
Copy link

ekasetiawans commented May 18, 2024

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
Copy link

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

@ekasetiawans
Copy link
Author

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

@SoursopID
Copy link

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
Copy link
Author

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

@lucasvmx
Copy link

lucasvmx commented May 18, 2024

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
}

@tulir tulir closed this as completed May 18, 2024
@h4775346
Copy link

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 ""
}

@stevegore
Copy link

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 ""
}

Chances are you want the reply instead of the quoted message. Proposed alternative:

func messageContent(msg *waE2E.Message) string {
	if msg == nil {
		return ""
	}

	if conversation := msg.GetConversation(); len(conversation) > 0 {
		return conversation
	}

	if extendedMsg := msg.GetExtendedTextMessage().GetText(); len(extendedMsg) > 0 {
		return extendedMsg
	}

	if editedContent := messageContent(msg.GetProtocolMessage().GetEditedMessage()); len(editedContent) > 0 {
		return editedContent
	}

	return ""
}

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

6 participants