Class: Whatsapp::Session
- Inherits:
-
Object
- Object
- Whatsapp::Session
- Includes:
- Concerns::GroupMethods, Concerns::MessageSending
- Defined in:
- lib/whatsapp/session.rb
Overview
A named WhatsApp session with its own bridge, auth state, and events.
Each session represents one WhatsApp account. Sessions are managed by SessionManager and can be accessed via +Whatsapp.session("name")+.
Constant Summary collapse
- STATUSES =
Valid session statuses.
%i[disconnected connecting qr_pending connected].freeze
- EVENT_CALLBACKS =
Event callbacks delegated to the client.
%i[ on_message on_message_any on_qr on_connected on_message_status on_message_sent on_message_delivered on_message_read on_presence_update on_message_receipt on_poll_vote on_contacts_update ].freeze
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
The underlying WhatsApp client.
-
#heartbeat ⇒ Heartbeat
readonly
The health check instance for this session.
-
#name ⇒ String
readonly
Session name (used as directory name for auth state).
-
#qr_data ⇒ String?
readonly
Base64-encoded QR code data (when status is +:qr_pending+).
-
#status ⇒ Symbol
readonly
Current session status (see STATUSES).
Instance Method Summary collapse
-
#business_profile(phone) ⇒ Hash
Get a contact's business profile.
-
#connect ⇒ self
Start the bridge and connect to WhatsApp.
-
#connected? ⇒ Boolean
Whether the session is connected to WhatsApp.
-
#disconnect ⇒ self
Disconnect from WhatsApp and stop the bridge.
-
#events ⇒ Events
Access the event emitter.
-
#fetch_status(phone) ⇒ Hash
Get a contact's status/about text.
-
#initialize(name, config: Whatsapp.configuration) ⇒ Session
constructor
A new instance of Session.
-
#on(event_name) {|data| ... } ⇒ void
Register a handler for a custom event.
-
#on_connected(&block) ⇒ void
Register a handler for successful connection.
-
#on_contacts_update(&block) ⇒ void
Register a handler for contact list updates.
-
#on_message {|message| ... } ⇒ void
Register a handler for new incoming messages.
-
#on_message_any {|message| ... } ⇒ void
Register a handler for all messages (live + history sync).
-
#on_message_delivered(&block) ⇒ void
Register a handler for "delivered" status.
-
#on_message_read(&block) ⇒ void
Register a handler for "read" status.
-
#on_message_receipt(&block) ⇒ void
Register a handler for message receipt updates.
-
#on_message_sent(&block) ⇒ void
Register a handler for "sent" status.
-
#on_message_status {|data| ... } ⇒ void
Register a handler for message status changes.
-
#on_poll_vote(&block) ⇒ void
Register a handler for poll vote results.
-
#on_presence_update(&block) ⇒ void
Register a handler for presence updates.
-
#on_qr {|qr| ... } ⇒ void
Register a handler for QR code events.
-
#on_whatsapp?(phone) ⇒ Hash
Check if a phone number is registered on WhatsApp.
-
#profile_picture(phone, high_res: false) ⇒ Hash
Get a contact's profile picture URL.
-
#qr_pending? ⇒ Boolean
Whether the session is waiting for a QR code scan.
-
#rate_limit_stats ⇒ Hash
Get rate limiting statistics.
-
#read_messages(keys) ⇒ Hash
Mark messages as read.
-
#safety_status ⇒ Hash
Get comprehensive safety status for monitoring.
-
#send_message(to:, text: nil, media: nil, location: nil, contact: nil, react: nil, poll: nil, quoted_message_id: nil, unsafe: false) ⇒ Message
Send a message through this session.
-
#send_presence(type, to: nil) ⇒ Hash
Send a presence update through this session.
-
#subscribe_presence(jid) ⇒ Hash
Subscribe to presence updates for a JID.
-
#to_h ⇒ Hash
Session info as a hash.
Methods included from Concerns::MessageSending
#react, #reply, #send_audio, #send_contact, #send_document, #send_image, #send_location, #send_poll, #send_sticker, #send_video
Methods included from Concerns::GroupMethods
#add_group_participants, #create_group, #group, #groups, #remove_group_participants, #update_group
Constructor Details
#initialize(name, config: Whatsapp.configuration) ⇒ Session
Returns a new instance of Session.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/whatsapp/session.rb', line 46 def initialize(name, config: Whatsapp.configuration) @name = name @config = build_session_config(config) @client = Client.new(config: @config) @status = :disconnected @qr_data = nil @heartbeat = Heartbeat.new(self, interval: config.heartbeat_interval) @mutex = Mutex.new setup_event_tracking end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns the underlying WhatsApp client.
33 34 35 |
# File 'lib/whatsapp/session.rb', line 33 def client @client end |
#heartbeat ⇒ Heartbeat (readonly)
Returns the health check instance for this session.
42 43 44 |
# File 'lib/whatsapp/session.rb', line 42 def heartbeat @heartbeat end |
#name ⇒ String (readonly)
Returns session name (used as directory name for auth state).
30 31 32 |
# File 'lib/whatsapp/session.rb', line 30 def name @name end |
#qr_data ⇒ String? (readonly)
Returns base64-encoded QR code data (when status is +:qr_pending+).
36 37 38 |
# File 'lib/whatsapp/session.rb', line 36 def qr_data @qr_data end |
#status ⇒ Symbol (readonly)
Returns current session status (see STATUSES).
39 40 41 |
# File 'lib/whatsapp/session.rb', line 39 def status @status end |
Instance Method Details
#business_profile(phone) ⇒ Hash
Get a contact's business profile.
156 157 158 |
# File 'lib/whatsapp/session.rb', line 156 def business_profile(phone) @client.business_profile(phone) end |
#connect ⇒ self
Start the bridge and connect to WhatsApp.
60 61 62 63 64 65 |
# File 'lib/whatsapp/session.rb', line 60 def connect @mutex.synchronize { @status = :connecting } @client.connect @heartbeat.start if @config.heartbeat_enabled self end |
#connected? ⇒ Boolean
Whether the session is connected to WhatsApp.
83 84 85 |
# File 'lib/whatsapp/session.rb', line 83 def connected? status == :connected end |
#disconnect ⇒ self
Disconnect from WhatsApp and stop the bridge.
70 71 72 73 74 75 76 77 78 |
# File 'lib/whatsapp/session.rb', line 70 def disconnect @heartbeat.stop @client.disconnect @mutex.synchronize do @status = :disconnected @qr_data = nil end self end |
#events ⇒ Events
Access the event emitter.
246 247 248 |
# File 'lib/whatsapp/session.rb', line 246 def events @client.events end |
#fetch_status(phone) ⇒ Hash
Get a contact's status/about text.
148 149 150 |
# File 'lib/whatsapp/session.rb', line 148 def fetch_status(phone) @client.fetch_status(phone) end |
#on(event_name) {|data| ... } ⇒ void
This method returns an undefined value.
Register a handler for a custom event.
225 226 227 |
# File 'lib/whatsapp/session.rb', line 225 def on(event_name, &block) @client.on(event_name, &block) end |
#on_connected(&block) ⇒ void
This method returns an undefined value.
Register a handler for successful connection.
|
|
# File 'lib/whatsapp/session.rb', line 177
|
#on_contacts_update(&block) ⇒ void
This method returns an undefined value.
Register a handler for contact list updates.
214 215 216 217 218 |
# File 'lib/whatsapp/session.rb', line 214 EVENT_CALLBACKS.each do |method_name| define_method(method_name) do |&block| @client.send(method_name, &block) end end |
#on_message {|message| ... } ⇒ void
This method returns an undefined value.
Register a handler for new incoming messages.
|
|
# File 'lib/whatsapp/session.rb', line 160
|
#on_message_any {|message| ... } ⇒ void
This method returns an undefined value.
Register a handler for all messages (live + history sync).
|
|
# File 'lib/whatsapp/session.rb', line 166
|
#on_message_delivered(&block) ⇒ void
This method returns an undefined value.
Register a handler for "delivered" status.
|
|
# File 'lib/whatsapp/session.rb', line 190
|
#on_message_read(&block) ⇒ void
This method returns an undefined value.
Register a handler for "read" status.
|
|
# File 'lib/whatsapp/session.rb', line 194
|
#on_message_receipt(&block) ⇒ void
This method returns an undefined value.
Register a handler for message receipt updates.
|
|
# File 'lib/whatsapp/session.rb', line 202
|
#on_message_sent(&block) ⇒ void
This method returns an undefined value.
Register a handler for "sent" status.
|
|
# File 'lib/whatsapp/session.rb', line 186
|
#on_message_status {|data| ... } ⇒ void
This method returns an undefined value.
Register a handler for message status changes.
|
|
# File 'lib/whatsapp/session.rb', line 181
|
#on_poll_vote(&block) ⇒ void
This method returns an undefined value.
Register a handler for poll vote results.
|
|
# File 'lib/whatsapp/session.rb', line 206
|
#on_presence_update(&block) ⇒ void
This method returns an undefined value.
Register a handler for presence updates.
|
|
# File 'lib/whatsapp/session.rb', line 198
|
#on_qr {|qr| ... } ⇒ void
This method returns an undefined value.
Register a handler for QR code events.
|
|
# File 'lib/whatsapp/session.rb', line 172
|
#on_whatsapp?(phone) ⇒ Hash
Check if a phone number is registered on WhatsApp.
131 132 133 |
# File 'lib/whatsapp/session.rb', line 131 def on_whatsapp?(phone) @client.on_whatsapp?(phone) end |
#profile_picture(phone, high_res: false) ⇒ Hash
Get a contact's profile picture URL.
140 141 142 |
# File 'lib/whatsapp/session.rb', line 140 def profile_picture(phone, high_res: false) @client.profile_picture(phone, high_res: high_res) end |
#qr_pending? ⇒ Boolean
Whether the session is waiting for a QR code scan.
90 91 92 |
# File 'lib/whatsapp/session.rb', line 90 def qr_pending? status == :qr_pending end |
#rate_limit_stats ⇒ Hash
Get rate limiting statistics.
232 233 234 |
# File 'lib/whatsapp/session.rb', line 232 def rate_limit_stats @client.rate_limit_stats end |
#read_messages(keys) ⇒ Hash
Mark messages as read.
123 124 125 |
# File 'lib/whatsapp/session.rb', line 123 def (keys) @client.(keys) end |
#safety_status ⇒ Hash
Get comprehensive safety status for monitoring.
239 240 241 |
# File 'lib/whatsapp/session.rb', line 239 def safety_status @client.safety_status end |
#send_message(to:, text: nil, media: nil, location: nil, contact: nil, react: nil, poll: nil, quoted_message_id: nil, unsafe: false) ⇒ Message
Send a message through this session.
98 99 100 |
# File 'lib/whatsapp/session.rb', line 98 def (to:, text: nil, media: nil, location: nil, contact: nil, react: nil, poll: nil, quoted_message_id: nil, unsafe: false) @client.(to: to, text: text, media: media, location: location, contact: contact, react: react, poll: poll, quoted_message_id: , unsafe: unsafe) end |
#send_presence(type, to: nil) ⇒ Hash
Send a presence update through this session.
107 108 109 |
# File 'lib/whatsapp/session.rb', line 107 def send_presence(type, to: nil) @client.send_presence(type, to: to) end |
#subscribe_presence(jid) ⇒ Hash
Subscribe to presence updates for a JID.
115 116 117 |
# File 'lib/whatsapp/session.rb', line 115 def subscribe_presence(jid) @client.subscribe_presence(jid) end |
#to_h ⇒ Hash
Session info as a hash.
253 254 255 256 257 258 259 260 261 |
# File 'lib/whatsapp/session.rb', line 253 def to_h { name: name, status: status, qr_pending: qr_pending?, rate_limits: rate_limit_stats, heartbeat: @heartbeat.to_h } end |