Class: Whatsapp::RecordProxy

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

Overview

Proxy for sending messages to a specific phone number.

Created by the +whatsapp+ model macro in destination mode (default). All messages are sent to the phone number bound to this proxy.

Examples:

proxy = Whatsapp::RecordProxy.new("+33612345678")
proxy.send_message("Hello!")
proxy.send_image("photo.jpg", caption: "Look!")
proxy.send_location(latitude: 48.8566, longitude: 2.3522, name: "Paris")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone_number) ⇒ RecordProxy

Returns a new instance of RecordProxy.

Parameters:

  • phone_number (String)

    recipient phone number



18
19
20
# File 'lib/whatsapp/record_proxy.rb', line 18

def initialize(phone_number)
  @phone_number = phone_number
end

Instance Attribute Details

#phone_numberString (readonly)

Returns the phone number this proxy sends to.

Returns:

  • (String)

    the phone number this proxy sends to



15
16
17
# File 'lib/whatsapp/record_proxy.rb', line 15

def phone_number
  @phone_number
end

Instance Method Details

#mark_as_read(message_ids) ⇒ Hash

Mark messages from this contact as read.

Parameters:

  • message_ids (String, Array<String>)

    one or more message IDs

Returns:

  • (Hash)


156
157
158
159
160
# File 'lib/whatsapp/record_proxy.rb', line 156

def mark_as_read(message_ids)
  ids = Array(message_ids)
  keys = ids.map { |id| {remote_jid: phone_number, id: id} }
  client.read_messages(keys)
end

#react(message_id:, emoji:) ⇒ Message

React to a message with an emoji.

Parameters:

  • message_id (String)

    ID of the message to react to

  • emoji (String)

    reaction emoji

Returns:



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

def react(message_id:, emoji:)
  client.send_message(to: phone_number, react: {emoji: emoji, message_id: message_id})
end

#reply(quoted_message_id:, text: nil, media: nil) ⇒ Message

Reply to a message (quoted reply).

Parameters:

  • quoted_message_id (String)

    ID of the message to quote

  • text (String, nil) (defaults to: nil)

    reply text

  • media (Hash, nil) (defaults to: nil)

    reply media

Returns:



112
113
114
# File 'lib/whatsapp/record_proxy.rb', line 112

def reply(quoted_message_id:, text: nil, media: nil)
  client.send_message(to: phone_number, text: text, media: media, quoted_message_id: quoted_message_id)
end

#send_audio(file) ⇒ Message

Send an audio file.

Parameters:

  • file (String)

    file path or URL

Returns:



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

def send_audio(file)
  send_media(:audio, file)
end

#send_composingHash

Send a "composing" (typing) indicator to this contact.

Returns:

  • (Hash)


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

def send_composing
  client.send_presence("composing", to: phone_number)
end

#send_contact(name:, phone:) ⇒ Message

Send a contact card.

Parameters:

  • name (String)

    contact display name

  • phone (String)

    contact phone number

Returns:



93
94
95
# File 'lib/whatsapp/record_proxy.rb', line 93

def send_contact(name:, phone:)
  client.send_message(to: phone_number, contact: {name: name, phone: phone})
end

#send_document(file, filename: nil, caption: nil) ⇒ Message

Send a document.

Parameters:

  • file (String)

    file path or URL

  • filename (String, nil) (defaults to: nil)

    display filename

  • caption (String, nil) (defaults to: nil)

    document caption

Returns:



45
46
47
# File 'lib/whatsapp/record_proxy.rb', line 45

def send_document(file, filename: nil, caption: nil)
  send_media(:document, file, filename: filename, caption: caption)
end

#send_document_later(file, filename: nil, caption: nil) ⇒ void

This method returns an undefined value.

Send a document asynchronously via ActiveJob.

Parameters:

  • file (String)

    file path or URL

  • filename (String, nil) (defaults to: nil)

    display filename

  • caption (String, nil) (defaults to: nil)

    document caption

Raises:

  • (Error)

    if WhatsappSendJob is not defined



193
194
195
196
197
198
199
# File 'lib/whatsapp/record_proxy.rb', line 193

def send_document_later(file, filename: nil, caption: nil)
  ensure_active_job!
  WhatsappSendJob.perform_later(
    to: phone_number,
    media: {type: "document", file: file, filename: filename, caption: caption}
  )
end

#send_image(file, caption: nil) ⇒ Message

Send an image.

Parameters:

  • file (String)

    file path or URL

  • caption (String, nil) (defaults to: nil)

    image caption

Returns:



35
36
37
# File 'lib/whatsapp/record_proxy.rb', line 35

def send_image(file, caption: nil)
  send_media(:image, file, caption: caption)
end

#send_image_later(file, caption: nil) ⇒ void

This method returns an undefined value.

Send an image asynchronously via ActiveJob.

Parameters:

  • file (String)

    file path or URL

  • caption (String, nil) (defaults to: nil)

    image caption

Raises:

  • (Error)

    if WhatsappSendJob is not defined



178
179
180
181
182
183
184
# File 'lib/whatsapp/record_proxy.rb', line 178

def send_image_later(file, caption: nil)
  ensure_active_job!
  WhatsappSendJob.perform_later(
    to: phone_number,
    media: {type: "image", file: file, caption: caption}
  )
end

#send_later(text) ⇒ void

This method returns an undefined value.

Send a text message asynchronously via ActiveJob.

Parameters:

  • text (String)

    message text

Raises:

  • (Error)

    if WhatsappSendJob is not defined



167
168
169
170
# File 'lib/whatsapp/record_proxy.rb', line 167

def send_later(text)
  ensure_active_job!
  WhatsappSendJob.perform_later(to: phone_number, text: text)
end

#send_location(latitude:, longitude:, name: nil, address: nil) ⇒ Message

Send a location.

Parameters:

  • latitude (Float)

    latitude

  • longitude (Float)

    longitude

  • name (String, nil) (defaults to: nil)

    location name

  • address (String, nil) (defaults to: nil)

    location address

Returns:



81
82
83
84
85
86
# File 'lib/whatsapp/record_proxy.rb', line 81

def send_location(latitude:, longitude:, name: nil, address: nil)
  client.send_message(
    to: phone_number,
    location: {latitude: latitude, longitude: longitude, name: name, address: address}
  )
end

#send_message(text) ⇒ Message

Send a text message.

Parameters:

  • text (String)

    message text

Returns:



26
27
28
# File 'lib/whatsapp/record_proxy.rb', line 26

def send_message(text)
  client.send_message(to: phone_number, text: text)
end

#send_pausedHash

Clear the typing/recording indicator for this contact.

Returns:

  • (Hash)


148
149
150
# File 'lib/whatsapp/record_proxy.rb', line 148

def send_paused
  client.send_presence("paused", to: phone_number)
end

#send_poll(question:, options:, selectable_count: 1) ⇒ Message

Send a poll to this contact.

Parameters:

  • question (String)

    poll question

  • options (Array<String>)

    poll options

  • selectable_count (Integer) (defaults to: 1)

    max selections (1 = single, 0 = unlimited)

Returns:



122
123
124
125
126
127
# File 'lib/whatsapp/record_proxy.rb', line 122

def send_poll(question:, options:, selectable_count: 1)
  client.send_message(
    to: phone_number,
    poll: {question: question, options: options, selectable_count: selectable_count}
  )
end

#send_recordingHash

Send a "recording" (audio) indicator to this contact.

Returns:

  • (Hash)


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

def send_recording
  client.send_presence("recording", to: phone_number)
end

#send_sticker(file) ⇒ Message

Send a sticker.

Parameters:

  • file (String)

    file path or URL (.webp)

Returns:



70
71
72
# File 'lib/whatsapp/record_proxy.rb', line 70

def send_sticker(file)
  send_media(:sticker, file)
end

#send_video(file, caption: nil) ⇒ Message

Send a video.

Parameters:

  • file (String)

    file path or URL

  • caption (String, nil) (defaults to: nil)

    video caption

Returns:



54
55
56
# File 'lib/whatsapp/record_proxy.rb', line 54

def send_video(file, caption: nil)
  send_media(:video, file, caption: caption)
end