GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
package whatsapp
import (
"context"
"go.mau.fi/whatsmeow"
)
type deviceContextKey struct{}
// ContextWithDevice stores a device instance into the provided context for per-request scoping.
func ContextWithDevice(ctx context.Context, device *DeviceInstance) context.Context {
if ctx == nil {
return context.Background()
}
return context.WithValue(ctx, deviceContextKey{}, device)
}
// DeviceFromContext retrieves a device instance from context if present.
func DeviceFromContext(ctx context.Context) (*DeviceInstance, bool) {
if ctx == nil {
return nil, false
}
if value := ctx.Value(deviceContextKey{}); value != nil {
if inst, ok := value.(*DeviceInstance); ok {
return inst, true
}
}
return nil, false
}
type sendMessageIDContextKey struct{}
// ContextWithSendMessageID asks the send path to use the given WhatsApp
// message id for the next message sent with this context instead of a random
// one. An empty id leaves the context unchanged.
func ContextWithSendMessageID(ctx context.Context, messageID string) context.Context {
if ctx == nil || messageID == "" {
return ctx
}
return context.WithValue(ctx, sendMessageIDContextKey{}, messageID)
}
// SendMessageIDFromContext returns the id set by ContextWithSendMessageID.
func SendMessageIDFromContext(ctx context.Context) string {
if ctx == nil {
return ""
}
id, _ := ctx.Value(sendMessageIDContextKey{}).(string)
return id
}
// ClientFromContext returns the client stored in the device context.
// If a device was explicitly set in context but has no client (not logged in), returns nil.
// Only falls back to global client when no device was set in context (backward compatibility).
func ClientFromContext(ctx context.Context) *whatsmeow.Client {
if inst, ok := DeviceFromContext(ctx); ok && inst != nil {
// Device was explicitly set - return its client (may be nil if not logged in)
return inst.GetClient()
}
// No device in context - fall back to global client for backward compatibility
return GetClient()
}
// pollSecretContextKey carries a durable scheduled occurrence's poll secret.
type pollSecretContextKey struct{}
// ContextWithPollSecret carries the persisted secret for a scheduled poll.
func ContextWithPollSecret(ctx context.Context, secret []byte) context.Context {
if len(secret) == 0 {
return ctx
}
return context.WithValue(ctx, pollSecretContextKey{}, append([]byte(nil), secret...))
}
// PollSecretFromContext returns a copy of the scheduled poll secret, if any.
func PollSecretFromContext(ctx context.Context) []byte {
if ctx == nil {
return nil
}
secret, _ := ctx.Value(pollSecretContextKey{}).([]byte)
return append([]byte(nil), secret...)
}