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
- Variables
- func FormatPollResponse(question, answer string) string
- func GetSuccessPollResponse() string
- func ParseIntensityPollResponse(response string, currentIntensity string) string
- type Client
- func (c *Client) GetClient() *whatsmeow.Client
- func (c *Client) SendIntensityAdjustmentPoll(ctx context.Context, to string, currentIntensity string) error
- func (c *Client) SendMessage(ctx context.Context, to string, body string) error
- func (c *Client) SendPromptButtons(ctx context.Context, to string, body string) error
- func (c *Client) SendTypingIndicator(ctx context.Context, to string, typing bool) error
- type MockClient
- func (m *MockClient) SendIntensityAdjustmentPoll(ctx context.Context, to string, currentIntensity string) error
- func (m *MockClient) SendMessage(ctx context.Context, to string, body string) error
- func (m *MockClient) SendPromptButtons(ctx context.Context, to string, body string) error
- func (m *MockClient) SendTypingIndicator(ctx context.Context, to string, typing bool) error
- type Option
- type Opts
- type SentMessage
- type TypingEvent
- type WhatsAppSender
Constants ¶
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
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 ¶
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
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 ¶
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 ¶
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 ¶
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) 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 ¶
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 ¶
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.
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 (*MockClient) SendPromptButtons ¶
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 ¶
SendTypingIndicator records typing indicator state changes for testing.
type Option ¶
type Option func(*Opts)
Option defines a configuration option for the WhatsApp client.
func WithNumericCode ¶
func WithNumericCode() Option
WithNumericCode instructs the WhatsApp client to use numeric login code instead of QR code.
func WithQRCodeOutput ¶
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 ¶
SentMessage represents a message sent via MockClient for testing
type TypingEvent ¶
TypingEvent captures mock typing indicator invocations for assertions.