Files
neo/context/message.go
T
FdandClaude Opus 4.7 e783284881 feat: add download commands (yt, ig, tt, fb) and middleware support
- Add core Middleware support via `core.Default.Use()`
- Implement download command handlers for YouTube, Instagram, TikTok, and Facebook
- Add regex-based URL validation for all download commands
- Add API client for fetching download links
- Add helpers for uploading and sending media to WhatsApp

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-07-15 10:33:58 +07:00

109 lines
3.4 KiB
Go

package context
import (
"strings"
waE2E "go.mau.fi/whatsmeow/proto/waE2E"
waTypes "go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
"google.golang.org/protobuf/proto"
)
// ParseMessageText extracts text content from a message event.
func ParseMessageText(evt *events.Message) string {
var msg *waE2E.Message
if evt.IsViewOnce {
msg = evt.Message.GetViewOnceMessage().GetMessage()
} else if evt.IsEphemeral {
msg = evt.Message.GetEphemeralMessage().GetMessage()
} else {
msg = evt.Message
}
switch {
case msg.GetConversation() != "":
return msg.GetConversation()
case msg.GetExtendedTextMessage().GetText() != "":
return msg.GetExtendedTextMessage().GetText()
case msg.GetImageMessage().GetCaption() != "":
return msg.GetImageMessage().GetCaption()
case msg.GetVideoMessage().GetCaption() != "":
return msg.GetVideoMessage().GetCaption()
default:
return ""
}
}
// GetQuotedMessage extracts the quoted message from context info.
func GetQuotedMessage(m *waE2E.Message) *waE2E.Message {
if m == nil {
return nil
}
switch {
case m.GetExtendedTextMessage().GetContextInfo() != nil:
return m.GetExtendedTextMessage().GetContextInfo().GetQuotedMessage()
case m.GetImageMessage().GetContextInfo() != nil:
return m.GetImageMessage().GetContextInfo().GetQuotedMessage()
case m.GetVideoMessage().GetContextInfo() != nil:
return m.GetVideoMessage().GetContextInfo().GetQuotedMessage()
case m.GetDocumentMessage().GetContextInfo() != nil:
return m.GetDocumentMessage().GetContextInfo().GetQuotedMessage()
case m.GetAudioMessage().GetContextInfo() != nil:
return m.GetAudioMessage().GetContextInfo().GetQuotedMessage()
case m.GetStickerMessage().GetContextInfo() != nil:
return m.GetStickerMessage().GetContextInfo().GetQuotedMessage()
default:
return nil
}
}
// WithReply creates ContextInfo that quotes the original message.
func WithReply(evt *events.Message) *waE2E.ContextInfo {
return &waE2E.ContextInfo{
StanzaID: &evt.Info.ID,
Participant: proto.String(evt.Info.Sender.String()),
QuotedMessage: evt.Message,
}
}
// GetQuotedStanzaID returns the stanza ID of the message being replied to, if any.
func GetQuotedStanzaID(m *waE2E.Message) string {
if m == nil {
return ""
}
switch {
case m.GetExtendedTextMessage().GetContextInfo() != nil:
return m.GetExtendedTextMessage().GetContextInfo().GetStanzaID()
case m.GetImageMessage().GetContextInfo() != nil:
return m.GetImageMessage().GetContextInfo().GetStanzaID()
case m.GetVideoMessage().GetContextInfo() != nil:
return m.GetVideoMessage().GetContextInfo().GetStanzaID()
case m.GetDocumentMessage().GetContextInfo() != nil:
return m.GetDocumentMessage().GetContextInfo().GetStanzaID()
case m.GetAudioMessage().GetContextInfo() != nil:
return m.GetAudioMessage().GetContextInfo().GetStanzaID()
case m.GetStickerMessage().GetContextInfo() != nil:
return m.GetStickerMessage().GetContextInfo().GetStanzaID()
default:
return ""
}
}
// ParseJID parses a JID string, adding @s.whatsapp.net if no server is specified.
func ParseJID(arg string) (waTypes.JID, bool) {
if arg == "" {
return waTypes.JID{}, false
}
if arg[0] == '+' {
arg = arg[1:]
}
if !strings.ContainsRune(arg, '@') {
return waTypes.NewJID(arg, waTypes.DefaultUserServer), true
}
recipient, err := waTypes.ParseJID(arg)
if err != nil || recipient.User == "" {
return recipient, false
}
return recipient, true
}