Module: Whatsapp::Concerns::MessageSending

Included in:
AccountProxy, Session
Defined in:
lib/whatsapp/concerns/message_sending.rb

Overview

Shared media/rich message helpers.

The includer must define +send_message+ with the standard signature (+to:, text:, media:, location:, contact:, react:, quoted_message_id:+).

Instance Method Summary collapse

Instance Method Details

#react(to:, message_id:, emoji:) ⇒ Message

React to a message with an emoji.

Parameters:

  • to (String)

    recipient phone number

  • message_id (String)

    message ID to react to

  • emoji (String)

    reaction emoji

Returns:



85
86
87
# File 'lib/whatsapp/concerns/message_sending.rb', line 85

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

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

Reply to a message (quoted reply).

Parameters:

  • to (String)

    recipient phone number

  • quoted_message_id (String)

    message ID to quote

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

    reply text

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

    reply media

Returns:



96
97
98
# File 'lib/whatsapp/concerns/message_sending.rb', line 96

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

#send_audio(to:, file:) ⇒ Message

Send an audio file.

Parameters:

  • to (String)

    recipient phone number

  • file (String)

    file path or URL

Returns:



44
45
46
# File 'lib/whatsapp/concerns/message_sending.rb', line 44

def send_audio(to:, file:)
  send_message(to: to, media: {type: "audio", file: file})
end

#send_contact(to:, name:, phone:) ⇒ Message

Send a contact card.

Parameters:

  • to (String)

    recipient phone number

  • name (String)

    contact name

  • phone (String)

    contact phone

Returns:



75
76
77
# File 'lib/whatsapp/concerns/message_sending.rb', line 75

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

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

Send a document.

Parameters:

  • to (String)

    recipient phone number

  • file (String)

    file path or URL

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

    display filename

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

    document caption

Returns:



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

def send_document(to:, file:, filename: nil, caption: nil)
  send_message(to: to, media: {type: "document", file: file, filename: filename, caption: caption})
end

#send_image(to:, file:, caption: nil) ⇒ Message

Send an image.

Parameters:

  • to (String)

    recipient phone number

  • file (String)

    file path or URL

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

    image caption

Returns:



14
15
16
# File 'lib/whatsapp/concerns/message_sending.rb', line 14

def send_image(to:, file:, caption: nil)
  send_message(to: to, media: {type: "image", file: file, caption: caption})
end

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

Send a location.

Parameters:

  • to (String)

    recipient phone number

  • latitude (Float)

    latitude

  • longitude (Float)

    longitude

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

    location name

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

    address

Returns:



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

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

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

Send a poll (single or multi-choice).

Parameters:

  • to (String)

    recipient phone number

  • question (String)

    poll question

  • options (Array<String>)

    poll options (2-12)

  • selectable_count (Integer) (defaults to: 1)

    max selections (1 = single choice, 0 = unlimited)

Returns:



107
108
109
# File 'lib/whatsapp/concerns/message_sending.rb', line 107

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

#send_sticker(to:, file:) ⇒ Message

Send a sticker.

Parameters:

  • to (String)

    recipient phone number

  • file (String)

    file path or URL (.webp)

Returns:



53
54
55
# File 'lib/whatsapp/concerns/message_sending.rb', line 53

def send_sticker(to:, file:)
  send_message(to: to, media: {type: "sticker", file: file})
end

#send_video(to:, file:, caption: nil) ⇒ Message

Send a video.

Parameters:

  • to (String)

    recipient phone number

  • file (String)

    file path or URL

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

    video caption

Returns:



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

def send_video(to:, file:, caption: nil)
  send_message(to: to, media: {type: "video", file: file, caption: caption})
end