whatsapp

package
v0.0.0-...-c1e920f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package whatsapp wraps the Whatsmeow client for WhatsApp integration in PromptPipe.

It provides methods for sending messages and handling WhatsApp events.

Index

Constants

View Source
const (
	// DefaultSQLitePath is the default path for WhatsApp/whatsmeow SQLite database
	DefaultSQLitePath = "/var/lib/promptpipe/whatsmeow.db"
	// JIDSuffix is the WhatsApp JID suffix for regular users
	JIDSuffix = "s.whatsapp.net"
)

Constants for WhatsApp client configuration

View Source
const (
	// PollQuestion is the question text for the engagement poll
	PollQuestion = "Did you do it?"

	// IntensityPollQuestion is the question text for the intensity adjustment poll
	IntensityPollQuestion = "How's the intensity?"
)

Poll configuration constants These define the engagement poll sent with prompts and must match exactly between the sending (SendPromptButtons) and receiving (poll response handler) logic.

Variables

View Source
var IntensityPollOptions = map[string][]string{
	"low":    {"Keep current", "Increase"},
	"normal": {"Decrease", "Keep current", "Increase"},
	"high":   {"Decrease", "Keep current"},
}

IntensityPollOptions defines all possible intensity adjustment options

View Source
var PollOptions = []string{"Done", "Next time"}

PollOptions defines the available response options for the engagement poll. The order matters - keep consistent between sending and receiving.

Functions

func FormatPollResponse

func FormatPollResponse(question, answer string) string

FormatPollResponse formats a poll response into the standardized "Q: [question] A: [answer]" format. This ensures consistent formatting across all parts of the codebase that handle poll responses.

func GetSuccessPollResponse

func GetSuccessPollResponse() string

GetSuccessPollResponse returns the formatted poll response string for a successful habit completion. Use this for detecting when a user has clicked the "Done" button.

func ParseIntensityPollResponse

func ParseIntensityPollResponse(response string, currentIntensity string) string

ParseIntensityPollResponse parses an intensity poll response and returns the new intensity level. Returns empty string if the response is not a valid intensity adjustment response. Valid responses: "Decrease", "Keep current", "Increase"

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps the Whatsmeow client for modular use

func NewClient

func NewClient(opts ...Option) (*Client, error)

NewClient creates a new WhatsApp client, applying any provided options for customization. This handles WhatsApp/whatsmeow database configuration with proper validation and warnings.

func (*Client) GetClient

func (c *Client) GetClient() *whatsmeow.Client

GetClient returns the underlying whatsmeow client for event handling

func (*Client) SendIntensityAdjustmentPoll

func (c *Client) SendIntensityAdjustmentPoll(ctx context.Context, to string, currentIntensity string) error

SendIntensityAdjustmentPoll sends a poll asking the user to adjust their intervention intensity. The poll options are determined by the current intensity level (low/normal/high).

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, to string, body string) error

SendMessage sends a WhatsApp message to the specified recipient. It performs basic validation and relies on the service layer for phone number validation.

func (*Client) SendPromptButtons

func (c *Client) SendPromptButtons(ctx context.Context, to string, body string) error

SendPromptButtons sends a prompt message followed by a poll for "Did you do it?" with two options. This method maintains the original interface but now sends a poll instead of deprecated button messages.

func (*Client) SendTypingIndicator

func (c *Client) SendTypingIndicator(ctx context.Context, to string, typing bool) error

SendTypingIndicator updates the chat presence state (typing indicator) for a conversation.

type MockClient

type MockClient struct {
	SentMessages         []SentMessage
	PromptButtonMessages []SentMessage
	TypingEvents         []TypingEvent
}

MockClient implements the same interface as Client but does nothing (for tests) In tests, use whatsapp.NewMockClient() instead of NewClient to avoid real WhatsApp connections. Update api_test.go to use MockClient for waClient.

func NewMockClient

func NewMockClient() *MockClient

func (*MockClient) SendIntensityAdjustmentPoll

func (m *MockClient) SendIntensityAdjustmentPoll(ctx context.Context, to string, currentIntensity string) error

SendIntensityAdjustmentPoll records intensity adjustment poll messages for testing purposes.

func (*MockClient) SendMessage

func (m *MockClient) SendMessage(ctx context.Context, to string, body string) error

func (*MockClient) SendPromptButtons

func (m *MockClient) SendPromptButtons(ctx context.Context, to string, body string) error

SendPromptButtons records prompt button messages for testing purposes. Note: This now sends a poll instead of buttons, but maintains the interface name.

func (*MockClient) SendTypingIndicator

func (m *MockClient) SendTypingIndicator(ctx context.Context, to string, typing bool) error

SendTypingIndicator records typing indicator state changes for testing.

type Option

type Option func(*Opts)

Option defines a configuration option for the WhatsApp client.

func WithDBDSN

func WithDBDSN(dsn string) Option

WithDBDSN sets the WhatsApp/whatsmeow database connection string.

func WithNumericCode

func WithNumericCode() Option

WithNumericCode instructs the WhatsApp client to use numeric login code instead of QR code.

func WithQRCodeOutput

func WithQRCodeOutput(path string) Option

WithQRCodeOutput instructs the WhatsApp client to write the login QR code to the specified path.

type Opts

type Opts struct {
	DBDSN       string // WhatsApp/whatsmeow database connection string
	QRPath      string // path to write login QR code
	NumericCode bool   // use numeric login code instead of QR code
}

Opts holds configuration options for the WhatsApp client. This focuses solely on WhatsApp/whatsmeow database configuration and login settings.

type SentMessage

type SentMessage struct {
	To   string
	Body string
}

SentMessage represents a message sent via MockClient for testing

type TypingEvent

type TypingEvent struct {
	To     string
	Typing bool
}

TypingEvent captures mock typing indicator invocations for assertions.

type WhatsAppSender

type WhatsAppSender interface {
	SendMessage(ctx context.Context, to string, body string) error
	SendTypingIndicator(ctx context.Context, to string, typing bool) error
}

WhatsAppSender is an interface for sending WhatsApp messages (for production and testing)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL