Class: Whatsapp::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp/configuration.rb

Overview

Global configuration for whatsapp-ruby.

Examples:

Whatsapp.configure do |config|
  config.auth_dir = "/tmp/whatsapp_sessions"
  config.rate_limit_per_minute = 5
  config.heartbeat_interval = 60
end

Safety presets

Whatsapp.configure do |config|
  config.safety_preset = :strict   # conservative (default)
  config.safety_preset = :moderate # relaxed for trusted use
  config.safety_preset = :manual   # all values set manually
end

Constant Summary collapse

SAFETY_PRESETS =

Safety presets for anti-ban protection.

{
  strict: {
    rate_limit_per_minute: 6,
    rate_limit_per_hour: 40,
    rate_limit_per_day: 150,
    rate_limit_per_recipient_per_hour: 5,
    min_delay_between_messages: 3.0,
    delay_jitter: 5.0,
    max_new_chats_per_day: 10,
    post_connect_delay: 60.0,
    idle_threshold: 1800.0,
    warmup_messages: 5,
    warmup_delay_factor: 3.0,
    ramp_up_enabled: true,
    typing_simulation: true,
    auto_read_receipts: true,
    auto_presence_updates: false,
    ramp_up_days: 7
  },
  moderate: {
    rate_limit_per_minute: 10,
    rate_limit_per_hour: 80,
    rate_limit_per_day: 300,
    rate_limit_per_recipient_per_hour: 10,
    min_delay_between_messages: 2.0,
    delay_jitter: 3.0,
    max_new_chats_per_day: 20,
    post_connect_delay: 30.0,
    idle_threshold: 3600.0,
    warmup_messages: 3,
    warmup_delay_factor: 2.0,
    ramp_up_enabled: true,
    typing_simulation: false,
    auto_read_receipts: false,
    auto_presence_updates: false,
    ramp_up_days: 3
  },
  manual: {},
  off: {
    rate_limit_per_minute: 999,
    rate_limit_per_hour: 9999,
    rate_limit_per_day: 99_999,
    rate_limit_per_recipient_per_hour: 999,
    min_delay_between_messages: 0.0,
    delay_jitter: 0.0,
    max_new_chats_per_day: 9999,
    post_connect_delay: 0.0,
    idle_threshold: 999_999.0,
    warmup_messages: 0,
    warmup_delay_factor: 0.0,
    ramp_up_enabled: false,
    typing_simulation: false,
    auto_read_receipts: false,
    auto_presence_updates: false,
    ramp_up_days: 0
  }
}.freeze

Connection collapse

Rate Limiting collapse

Anti-Ban Safety collapse

Webhook Security collapse

Logging collapse

Heartbeat collapse

Message Handling collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/whatsapp/configuration.rb', line 215

def initialize
  # Connection
  @auth_dir = default_auth_dir
  @log_path = $stdout
  @bridge_timeout = 15
  @node_command = "node"
  @http_open_timeout = 5
  @http_read_timeout = 10
  @http_write_timeout = 10
  @auto_reconnect = true
  @max_reconnect_attempts = 5
  @reconnect_interval = 3

  # Rate limiting — strict preset by default
  @rate_limiting_enabled = true
  @safety_preset = :strict
  apply_safety_preset!(:strict)

  # Webhook security
  @webhook_secret = nil

  # Logging
  @log_level = :info

  # Heartbeat
  @heartbeat_enabled = true
  @heartbeat_interval = 30 # seconds

  # Message handler
  @message_handler = nil
end

Instance Attribute Details

#auth_dirString

Returns directory for storing WhatsApp auth state.

Returns:

  • (String)

    directory for storing WhatsApp auth state



22
23
24
# File 'lib/whatsapp/configuration.rb', line 22

def auth_dir
  @auth_dir
end

#auto_presence_updatesBoolean

Returns whether to send presence updates (available/unavailable) on connect/disconnect.

Returns:

  • (Boolean)

    whether to send presence updates (available/unavailable) on connect/disconnect



109
110
111
# File 'lib/whatsapp/configuration.rb', line 109

def auto_presence_updates
  @auto_presence_updates
end

#auto_read_receiptsBoolean

Returns whether to auto-send read receipts for incoming messages.

Returns:

  • (Boolean)

    whether to auto-send read receipts for incoming messages



105
106
107
# File 'lib/whatsapp/configuration.rb', line 105

def auto_read_receipts
  @auto_read_receipts
end

#auto_reconnectBoolean

Returns whether to auto-reconnect on disconnect.

Returns:

  • (Boolean)

    whether to auto-reconnect on disconnect



43
44
45
# File 'lib/whatsapp/configuration.rb', line 43

def auto_reconnect
  @auto_reconnect
end

#bridge_timeoutInteger

Returns seconds to wait for the bridge to become ready.

Returns:

  • (Integer)

    seconds to wait for the bridge to become ready



28
29
30
# File 'lib/whatsapp/configuration.rb', line 28

def bridge_timeout
  @bridge_timeout
end

#delay_jitterFloat

Returns maximum random jitter in seconds (gaussian distribution).

Returns:

  • (Float)

    maximum random jitter in seconds (gaussian distribution)



71
72
73
# File 'lib/whatsapp/configuration.rb', line 71

def delay_jitter
  @delay_jitter
end

#heartbeat_enabledBoolean

Returns whether periodic health checks are enabled.

Returns:

  • (Boolean)

    whether periodic health checks are enabled



141
142
143
# File 'lib/whatsapp/configuration.rb', line 141

def heartbeat_enabled
  @heartbeat_enabled
end

#heartbeat_intervalInteger

Returns health check interval in seconds.

Returns:

  • (Integer)

    health check interval in seconds



144
145
146
# File 'lib/whatsapp/configuration.rb', line 144

def heartbeat_interval
  @heartbeat_interval
end

#http_open_timeoutInteger

Returns HTTP open timeout in seconds for bridge requests.

Returns:

  • (Integer)

    HTTP open timeout in seconds for bridge requests



34
35
36
# File 'lib/whatsapp/configuration.rb', line 34

def http_open_timeout
  @http_open_timeout
end

#http_read_timeoutInteger

Returns HTTP read timeout in seconds for bridge requests.

Returns:

  • (Integer)

    HTTP read timeout in seconds for bridge requests



37
38
39
# File 'lib/whatsapp/configuration.rb', line 37

def http_read_timeout
  @http_read_timeout
end

#http_write_timeoutInteger

Returns HTTP write timeout in seconds for bridge requests.

Returns:

  • (Integer)

    HTTP write timeout in seconds for bridge requests



40
41
42
# File 'lib/whatsapp/configuration.rb', line 40

def http_write_timeout
  @http_write_timeout
end

#idle_thresholdFloat

Returns idle threshold in seconds — if no message sent for this long, the ramp-up warmup kicks in.

Returns:

  • (Float)

    idle threshold in seconds — if no message sent for this long, the ramp-up warmup kicks in



88
89
90
# File 'lib/whatsapp/configuration.rb', line 88

def idle_threshold
  @idle_threshold
end

#log_levelSymbol

Returns log level (:debug, :info, :warn, :error, :fatal).

Returns:

  • (Symbol)

    log level (:debug, :info, :warn, :error, :fatal)



134
135
136
# File 'lib/whatsapp/configuration.rb', line 134

def log_level
  @log_level
end

#log_pathString, IO

Returns log output path or IO object.

Returns:

  • (String, IO)

    log output path or IO object



25
26
27
# File 'lib/whatsapp/configuration.rb', line 25

def log_path
  @log_path
end

#max_new_chats_per_dayInteger

Returns max new conversations initiated per day.

Returns:

  • (Integer)

    max new conversations initiated per day



81
82
83
# File 'lib/whatsapp/configuration.rb', line 81

def max_new_chats_per_day
  @max_new_chats_per_day
end

#max_reconnect_attemptsInteger

Returns maximum number of reconnection attempts.

Returns:

  • (Integer)

    maximum number of reconnection attempts



46
47
48
# File 'lib/whatsapp/configuration.rb', line 46

def max_reconnect_attempts
  @max_reconnect_attempts
end

#message_handlerClass, ...

Returns handler class for incoming messages (must respond to +.new.call(message, session:)+).

Returns:

  • (Class, String, nil)

    handler class for incoming messages (must respond to +.new.call(message, session:)+)



152
153
154
# File 'lib/whatsapp/configuration.rb', line 152

def message_handler
  @message_handler
end

#min_delay_between_messagesFloat

Returns minimum delay between messages in seconds.

Returns:

  • (Float)

    minimum delay between messages in seconds



68
69
70
# File 'lib/whatsapp/configuration.rb', line 68

def min_delay_between_messages
  @min_delay_between_messages
end

#node_commandString

Returns path to the Node.js binary.

Returns:

  • (String)

    path to the Node.js binary



31
32
33
# File 'lib/whatsapp/configuration.rb', line 31

def node_command
  @node_command
end

#post_connect_delayFloat

Returns delay in seconds before first message after connection.

Returns:

  • (Float)

    delay in seconds before first message after connection



84
85
86
# File 'lib/whatsapp/configuration.rb', line 84

def post_connect_delay
  @post_connect_delay
end

#ramp_up_daysInteger

Returns number of days for progressive account warmup. Day 1 = limits / ramp_up_days, linearly increasing to full limits. Set to 0 to disable.

Returns:

  • (Integer)

    number of days for progressive account warmup. Day 1 = limits / ramp_up_days, linearly increasing to full limits. Set to 0 to disable.



114
115
116
# File 'lib/whatsapp/configuration.rb', line 114

def ramp_up_days
  @ramp_up_days
end

#ramp_up_enabledBoolean

Returns whether ramp-up after idle is enabled.

Returns:

  • (Boolean)

    whether ramp-up after idle is enabled



97
98
99
# File 'lib/whatsapp/configuration.rb', line 97

def ramp_up_enabled
  @ramp_up_enabled
end

#ramp_up_started_atTime?

Returns when the account started sending (for warmup calculation). Auto-set on first message if nil.

Returns:

  • (Time, nil)

    when the account started sending (for warmup calculation). Auto-set on first message if nil.



118
119
120
# File 'lib/whatsapp/configuration.rb', line 118

def ramp_up_started_at
  @ramp_up_started_at
end

#rate_limit_per_dayInteger

Returns max messages per day (global).

Returns:

  • (Integer)

    max messages per day (global)



62
63
64
# File 'lib/whatsapp/configuration.rb', line 62

def rate_limit_per_day
  @rate_limit_per_day
end

#rate_limit_per_hourInteger

Returns max messages per hour (global).

Returns:

  • (Integer)

    max messages per hour (global)



59
60
61
# File 'lib/whatsapp/configuration.rb', line 59

def rate_limit_per_hour
  @rate_limit_per_hour
end

#rate_limit_per_minuteInteger

Returns max messages per minute (global).

Returns:

  • (Integer)

    max messages per minute (global)



56
57
58
# File 'lib/whatsapp/configuration.rb', line 56

def rate_limit_per_minute
  @rate_limit_per_minute
end

#rate_limit_per_recipient_per_hourInteger

Returns max messages per hour per recipient.

Returns:

  • (Integer)

    max messages per hour per recipient



65
66
67
# File 'lib/whatsapp/configuration.rb', line 65

def rate_limit_per_recipient_per_hour
  @rate_limit_per_recipient_per_hour
end

#rate_limiting_enabledBoolean

Returns whether rate limiting is enabled.

Returns:

  • (Boolean)

    whether rate limiting is enabled



74
75
76
# File 'lib/whatsapp/configuration.rb', line 74

def rate_limiting_enabled
  @rate_limiting_enabled
end

#reconnect_intervalInteger

Returns base interval (seconds) between reconnection attempts.

Returns:

  • (Integer)

    base interval (seconds) between reconnection attempts



49
50
51
# File 'lib/whatsapp/configuration.rb', line 49

def reconnect_interval
  @reconnect_interval
end

#safety_presetSymbol

Returns current safety preset.

Returns:

  • (Symbol)

    current safety preset



261
262
263
# File 'lib/whatsapp/configuration.rb', line 261

def safety_preset
  @safety_preset
end

#typing_simulationBoolean

Returns whether to auto-send "composing" presence before messages. When enabled, send_message will automatically send a typing indicator proportional to the message length before actually sending.

Returns:

  • (Boolean)

    whether to auto-send "composing" presence before messages. When enabled, send_message will automatically send a typing indicator proportional to the message length before actually sending.



102
103
104
# File 'lib/whatsapp/configuration.rb', line 102

def typing_simulation
  @typing_simulation
end

#warmup_delay_factorFloat

Returns extra delay multiplier applied during warmup (decays linearly).

Returns:

  • (Float)

    extra delay multiplier applied during warmup (decays linearly)



94
95
96
# File 'lib/whatsapp/configuration.rb', line 94

def warmup_delay_factor
  @warmup_delay_factor
end

#warmup_messagesInteger

Returns number of messages over which to ramp up after idle.

Returns:

  • (Integer)

    number of messages over which to ramp up after idle



91
92
93
# File 'lib/whatsapp/configuration.rb', line 91

def warmup_messages
  @warmup_messages
end

#webhook_secretString?

Returns HMAC-SHA256 secret for verifying webhook payloads. When set, the bridge signs all WebSocket events and the Rails middleware rejects unsigned or invalid payloads.

Returns:

  • (String, nil)

    HMAC-SHA256 secret for verifying webhook payloads. When set, the bridge signs all WebSocket events and the Rails middleware rejects unsigned or invalid payloads.



127
128
129
# File 'lib/whatsapp/configuration.rb', line 127

def webhook_secret
  @webhook_secret
end