Class: WhatsAppNotifier::Providers::WebAutomation

Inherits:
Base
  • Object
show all
Defined in:
lib/whatsapp_notifier/providers/web_automation.rb

Instance Attribute Summary

Attributes inherited from Base

#configuration

Instance Method Summary collapse

Methods inherited from Base

#session_ready?

Constructor Details

#initialize(configuration:) ⇒ WebAutomation

Returns a new instance of WebAutomation.



4
5
6
7
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 4

def initialize(configuration:)
  super
  @store = Session::Store.new(path: configuration.web_session_path)
end

Instance Method Details

#connection_status(metadata: {}) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 45

def connection_status(metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter must be configured for web_automation provider" unless adapter.respond_to?(:connection_status)

  adapter.connection_status(metadata: )
end

#delete_media(message_id:, metadata: {}) ⇒ Object

Raises:



79
80
81
82
83
84
85
86
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 79

def delete_media(message_id:, metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter does not support media deletion (upgrade to a delete_media-capable adapter)" unless adapter.respond_to?(:delete_media)

  adapter.delete_media(message_id: message_id, metadata: )
end

#deliver(payload) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 9

def deliver(payload)
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter must be configured for web_automation provider" unless adapter.respond_to?(:send_message)

  warn_risk_once
  session = session_for(payload.fetch(:metadata, {}))
  response = adapter.send_message(payload: payload, session: session)
  persist_session(response.fetch(:session, {}), payload.fetch(:metadata, {}))

  success = response.fetch(:success)
  Result.new(
    success: success,
    provider: :web_automation,
    message_id: response[:message_id],
    error_code: error_code_for(success, response),
    error_message: response[:error_message],
    wait_seconds: response[:wait_seconds],
    metadata: response.fetch(:metadata, {})
  )
rescue StandardError => e
  Result.new(success: false, provider: :web_automation,
             error_code: ErrorCode.from_exception(e), error_message: e.message)
end

#fetch_history(chat_id:, limit: 50, metadata: {}) ⇒ Object

Raises:



112
113
114
115
116
117
118
119
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 112

def fetch_history(chat_id:, limit: 50, metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter does not support history replay (upgrade to a fetch_history-capable adapter)" unless adapter.respond_to?(:fetch_history)

  adapter.fetch_history(chat_id: chat_id, limit: limit, metadata: )
end

#fetch_inbound(metadata: {}) ⇒ Object

Pull pending inbound messages for the user. fetch_inbound is an optional adapter capability (added in v0.4.0) — older adapters that predate inbound support raise a clear ConfigurationError rather than NoMethodError, and Configuration#validate! does not require it.

Raises:



58
59
60
61
62
63
64
65
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 58

def fetch_inbound(metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter does not support inbound capture (upgrade to a fetch_inbound-capable adapter)" unless adapter.respond_to?(:fetch_inbound)

  adapter.fetch_inbound(metadata: )
end

#fetch_media(message_id:, metadata: {}) ⇒ Object

Media helpers are optional adapter capabilities (added in v0.7.0) — same guard idiom as fetch_inbound so older adapters fail with a clear ConfigurationError rather than NoMethodError.

Raises:



70
71
72
73
74
75
76
77
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 70

def fetch_media(message_id:, metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter does not support media fetch (upgrade to a fetch_media-capable adapter)" unless adapter.respond_to?(:fetch_media)

  adapter.fetch_media(message_id: message_id, metadata: )
end

#list_chats(metadata: {}) ⇒ Object

Chat-history helpers are optional adapter capabilities (added in v0.8.0) — same guard idiom as fetch_media so older adapters fail with a clear ConfigurationError rather than NoMethodError.

Raises:



103
104
105
106
107
108
109
110
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 103

def list_chats(metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter does not support chat listing (upgrade to a list_chats-capable adapter)" unless adapter.respond_to?(:list_chats)

  adapter.list_chats(metadata: )
end

#logout(metadata: {}) ⇒ Object

Raises:



121
122
123
124
125
126
127
128
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 121

def logout(metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter must be configured for web_automation provider" unless adapter.respond_to?(:logout)

  adapter.logout(metadata: )
end

#refetch_media(message_id:, chat_id:, metadata: {}) ⇒ Object

On-demand media re-download (added in v0.8.0) — same guard idiom as fetch_media so older adapters fail with a clear ConfigurationError rather than NoMethodError.

Raises:



91
92
93
94
95
96
97
98
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 91

def refetch_media(message_id:, chat_id:, metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter does not support media refetch (upgrade to a refetch_media-capable adapter)" unless adapter.respond_to?(:refetch_media)

  adapter.refetch_media(message_id: message_id, chat_id: chat_id, metadata: )
end

#scan_qr(metadata: {}) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
# File 'lib/whatsapp_notifier/providers/web_automation.rb', line 36

def scan_qr(metadata: {})
  raise ConfigurationError, "web automation provider is disabled" unless configuration.web_automation_enabled

  adapter = configuration.web_adapter
  raise ConfigurationError, "web_adapter must be configured for web_automation provider" unless adapter.respond_to?(:fetch_qr_code)

  Session::QrService.new(store: @store, adapter: adapter).qr_code(metadata: )
end