Class: Whatsapp::AccountProxy
- Inherits:
-
Object
- Object
- Whatsapp::AccountProxy
- Includes:
- Concerns::GroupMethods, Concerns::MessageSending
- Defined in:
- lib/whatsapp/account_proxy.rb
Overview
Proxy for a model with its own WhatsApp account (multi-session).
Created by the +whatsapp+ model macro with +account: true+. Each AccountProxy wraps a Session identified by a unique key.
Constant Summary collapse
- EVENT_CALLBACKS =
Event callbacks delegated to the session.
%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
-
#session_key ⇒ String
readonly
Unique session key for this account.
Instance Method Summary collapse
-
#business_profile(phone) ⇒ Hash
Get a contact's business profile.
-
#connect ⇒ self
Connect this account's WhatsApp session.
-
#connected? ⇒ Boolean
Whether the session is connected.
-
#disconnect ⇒ self
Disconnect this account's WhatsApp session.
-
#fetch_status(phone) ⇒ Hash
Get a contact's status/about text.
-
#health ⇒ Hash
Health check status.
-
#healthy? ⇒ Boolean
Whether the bridge and WhatsApp connection are healthy.
-
#initialize(session_key:) ⇒ AccountProxy
constructor
A new instance of AccountProxy.
-
#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 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(&block) ⇒ 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_data ⇒ String?
Base64-encoded QR code data (when status is +:qr_pending+).
-
#qr_pending? ⇒ Boolean
Whether the session is waiting for a QR code scan.
-
#rate_limit_stats ⇒ Hash
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 from this account.
-
#send_presence(type, to: nil) ⇒ Hash
Send a presence update from this account.
-
#status ⇒ Symbol
Current session status.
-
#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(session_key:) ⇒ AccountProxy
Returns a new instance of AccountProxy.
29 30 31 |
# File 'lib/whatsapp/account_proxy.rb', line 29 def initialize(session_key:) @session_key = session_key end |
Instance Attribute Details
#session_key ⇒ String (readonly)
Returns unique session key for this account.
26 27 28 |
# File 'lib/whatsapp/account_proxy.rb', line 26 def session_key @session_key end |
Instance Method Details
#business_profile(phone) ⇒ Hash
Get a contact's business profile.
145 146 147 |
# File 'lib/whatsapp/account_proxy.rb', line 145 def business_profile(phone) session.business_profile(phone) end |
#connect ⇒ self
Connect this account's WhatsApp session.
36 37 38 39 |
# File 'lib/whatsapp/account_proxy.rb', line 36 def connect session.connect self end |
#connected? ⇒ Boolean
Whether the session is connected.
59 60 61 |
# File 'lib/whatsapp/account_proxy.rb', line 59 def connected? session.connected? end |
#disconnect ⇒ self
Disconnect this account's WhatsApp session.
44 45 46 47 |
# File 'lib/whatsapp/account_proxy.rb', line 44 def disconnect session.disconnect self end |
#fetch_status(phone) ⇒ Hash
Get a contact's status/about text.
137 138 139 |
# File 'lib/whatsapp/account_proxy.rb', line 137 def fetch_status(phone) session.fetch_status(phone) end |
#health ⇒ Hash
Health check status.
227 228 229 |
# File 'lib/whatsapp/account_proxy.rb', line 227 def health session.heartbeat.to_h end |
#healthy? ⇒ Boolean
Whether the bridge and WhatsApp connection are healthy.
220 221 222 |
# File 'lib/whatsapp/account_proxy.rb', line 220 def healthy? session.heartbeat.healthy? end |
#on(event_name) {|data| ... } ⇒ void
This method returns an undefined value.
Register a handler for a custom event.
213 214 215 |
# File 'lib/whatsapp/account_proxy.rb', line 213 def on(event_name, &block) session.on(event_name, &block) end |
#on_connected(&block) ⇒ void
This method returns an undefined value.
Register a handler for successful connection.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 182
|
#on_contacts_update(&block) ⇒ void
This method returns an undefined value.
Register a handler for contact list updates.
202 203 204 205 206 |
# File 'lib/whatsapp/account_proxy.rb', line 202 EVENT_CALLBACKS.each do |method_name| define_method(method_name) do |&block| session.send(method_name, &block) end end |
#on_message {|message| ... } ⇒ void
This method returns an undefined value.
Register a handler for incoming messages.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 154
|
#on_message_any {|message| ... } ⇒ void
This method returns an undefined value.
Register a handler for all messages (live + history sync).
|
|
# File 'lib/whatsapp/account_proxy.rb', line 160
|
#on_message_delivered(&block) ⇒ void
This method returns an undefined value.
Register a handler for "delivered" status.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 174
|
#on_message_read(&block) ⇒ void
This method returns an undefined value.
Register a handler for "read" status.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 178
|
#on_message_receipt(&block) ⇒ void
This method returns an undefined value.
Register a handler for message receipt updates.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 190
|
#on_message_sent(&block) ⇒ void
This method returns an undefined value.
Register a handler for "sent" status.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 170
|
#on_message_status(&block) ⇒ void
This method returns an undefined value.
Register a handler for message status changes.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 166
|
#on_poll_vote(&block) ⇒ void
This method returns an undefined value.
Register a handler for poll vote results.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 194
|
#on_presence_update(&block) ⇒ void
This method returns an undefined value.
Register a handler for presence updates.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 186
|
#on_qr {|qr| ... } ⇒ void
This method returns an undefined value.
Register a handler for QR code events.
|
|
# File 'lib/whatsapp/account_proxy.rb', line 149
|
#on_whatsapp?(phone) ⇒ Hash
Check if a phone number is registered on WhatsApp.
120 121 122 |
# File 'lib/whatsapp/account_proxy.rb', line 120 def on_whatsapp?(phone) session.on_whatsapp?(phone) end |
#profile_picture(phone, high_res: false) ⇒ Hash
Get a contact's profile picture URL.
129 130 131 |
# File 'lib/whatsapp/account_proxy.rb', line 129 def profile_picture(phone, high_res: false) session.profile_picture(phone, high_res: high_res) end |
#qr_data ⇒ String?
Base64-encoded QR code data (when status is +:qr_pending+).
73 74 75 |
# File 'lib/whatsapp/account_proxy.rb', line 73 def qr_data session.qr_data end |
#qr_pending? ⇒ Boolean
Whether the session is waiting for a QR code scan.
66 67 68 |
# File 'lib/whatsapp/account_proxy.rb', line 66 def qr_pending? session.qr_pending? end |
#rate_limit_stats ⇒ Hash
Rate limiting statistics.
234 235 236 |
# File 'lib/whatsapp/account_proxy.rb', line 234 def rate_limit_stats session.rate_limit_stats end |
#read_messages(keys) ⇒ Hash
Mark messages as read.
112 113 114 |
# File 'lib/whatsapp/account_proxy.rb', line 112 def (keys) session.(keys) end |
#safety_status ⇒ Hash
Get comprehensive safety status for monitoring.
241 242 243 |
# File 'lib/whatsapp/account_proxy.rb', line 241 def safety_status session.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 from this account.
87 88 89 |
# File 'lib/whatsapp/account_proxy.rb', line 87 def (to:, text: nil, media: nil, location: nil, contact: nil, react: nil, poll: nil, quoted_message_id: nil, unsafe: false) session.(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 from this account.
96 97 98 |
# File 'lib/whatsapp/account_proxy.rb', line 96 def send_presence(type, to: nil) session.send_presence(type, to: to) end |
#status ⇒ Symbol
Current session status.
52 53 54 |
# File 'lib/whatsapp/account_proxy.rb', line 52 def status session.status end |
#subscribe_presence(jid) ⇒ Hash
Subscribe to presence updates for a JID.
104 105 106 |
# File 'lib/whatsapp/account_proxy.rb', line 104 def subscribe_presence(jid) session.subscribe_presence(jid) end |
#to_h ⇒ Hash
Session info as a hash.
248 249 250 |
# File 'lib/whatsapp/account_proxy.rb', line 248 def to_h session.to_h end |