Class: WhatsappSdk::Api::Messages

Inherits:
Request
  • Object
show all
Defined in:
lib/whatsapp_sdk/api/messages.rb

Constant Summary collapse

DEFAULT_HEADERS =
{ 'Content-Type' => 'application/json' }.freeze

Instance Method Summary collapse

Methods inherited from Request

#download_file, #initialize, #send_request

Constructor Details

This class inherits a constructor from WhatsappSdk::Api::Request

Instance Method Details

#read_message(sender_id:, message_id:) ⇒ Boolean

Mark a message as read.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • message_id (Integer)

    Message ID.

Returns:

  • (Boolean)

    Whether the message was marked as read.



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/whatsapp_sdk/api/messages.rb', line 327

def read_message(sender_id:, message_id:)
  params = {
    messaging_product: "whatsapp",
    status: "read",
    message_id: message_id
  }

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::SuccessResponse.success_response?(response: response)
end

#send_audio(sender_id:, recipient_number:, audio_id: nil, link: nil, message_id: nil) ⇒ MessageDataResponse

Send an audio.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • audio_id (String) (defaults to: nil)

    Audio ID.

  • link (String) (defaults to: nil)

    Audio link.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/whatsapp_sdk/api/messages.rb', line 116

def send_audio(sender_id:, recipient_number:, audio_id: nil, link: nil, message_id: nil)
  raise Resource::Errors::MissingArgumentError, "audio_id or link is required" if !audio_id && !link

  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "audio"
  }
  params[:audio] = link ? { link: link } : { id: audio_id }
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS,
    multipart: true
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_contacts(sender_id:, recipient_number:, contacts: nil, contacts_json: {}, message_id: nil) ⇒ MessageDataResponse

Send contacts. You can either send contacts objects or contacts as JSON.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • contacts (Array<Contact>) (defaults to: nil)

    Contacts.

  • contacts_json (Json) (defaults to: {})

    Contacts.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/whatsapp_sdk/api/messages.rb', line 252

def send_contacts(
  sender_id:, recipient_number:, contacts: nil, contacts_json: {}, message_id: nil
)
  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "contacts"
  }
  params[:contacts] = contacts ? contacts.map(&:to_h) : contacts_json
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_document(sender_id:, recipient_number:, document_id: nil, link: nil, caption: "", message_id: nil, filename: nil) ⇒ MessageDataResponse

Send a document.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • document_id (String) (defaults to: nil)

    document ID.

  • link (String) (defaults to: nil)

    Image link.

  • caption (String) (defaults to: "")

    Image caption.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/whatsapp_sdk/api/messages.rb', line 183

def send_document(
  sender_id:, recipient_number:, document_id: nil, link: nil, caption: "", message_id: nil, filename: nil
)
  if !document_id && !link
    raise Resource::Errors::MissingArgumentError,
          "document or link is required"
  end

  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "document"
  }
  params[:document] = if link
                        { link: link, caption: caption }
                      else
                        { id: document_id, caption: caption }
                      end
  params[:document] = params[:document].merge({ filename: filename }) if filename
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_image(sender_id:, recipient_number:, image_id: nil, link: nil, caption: "", message_id: nil) ⇒ MessageDataResponse

Send an image.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • image_id (String) (defaults to: nil)

    Image ID.

  • link (String) (defaults to: nil)

    Image link.

  • caption (String) (defaults to: "")

    Image caption.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/whatsapp_sdk/api/messages.rb', line 81

def send_image(
  sender_id:, recipient_number:, image_id: nil, link: nil, caption: "", message_id: nil
)
  raise Resource::Errors::MissingArgumentError, "image_id or link is required" if !image_id && !link

  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "image"
  }
  params[:image] = if link
                     { link: link, caption: caption }
                   else
                     { id: image_id, caption: caption }
                   end
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_interactive_message(sender_id:, recipient_number:, interactive: nil, interactive_json: nil, message_id: nil) ⇒ MessageDataResponse Also known as: send_interactive_reply_buttons, send_interactive_list_messages

Send interactive reply buttons. developers.facebook.com/docs/whatsapp/guides/interactive-messages#reply-buttons You can either send interactive object or as JSON.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • interactive (Interactive) (defaults to: nil)

    Interactive.

  • interactive_json (Json) (defaults to: nil)

    The interactive object as a Json. If you pass interactive_json, you can’t pass interactive.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/whatsapp_sdk/api/messages.rb', line 288

def send_interactive_message(
  sender_id:, recipient_number:, interactive: nil, interactive_json: nil, message_id: nil
)
  if !interactive && !interactive_json
    raise Resource::Errors::MissingArgumentError,
          "interactive or interactive_json is required"
  end

  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "interactive"
  }

  params[:interactive] = if interactive.nil?
                           interactive_json
                         else
                           interactive.to_json
                         end
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_location(sender_id:, recipient_number:, longitude:, latitude:, name:, address:, message_id: nil) ⇒ MessageDataResponse

Send location.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • longitude (Float)

    Location longitude.

  • latitude (Float)

    Location latitude.

  • name (String)

    Location name.

  • address (String)

    Location address.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/whatsapp_sdk/api/messages.rb', line 46

def send_location(
  sender_id:, recipient_number:, longitude:, latitude:, name:, address:, message_id: nil
)
  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "location",
    location: {
      longitude: longitude,
      latitude: latitude,
      name: name,
      address: address
    }
  }
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_reaction(sender_id:, recipient_number:, message_id:, emoji:) ⇒ MessageDataResponse

Send reaction

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • message_id (String)

    the id of the message to reaction.

  • emoji (String)

    unicode of the emoji to send.

Returns:

  • (MessageDataResponse)

    Response object.



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/whatsapp_sdk/api/messages.rb', line 393

def send_reaction(sender_id:, recipient_number:, message_id:, emoji:)
  params = {
    messaging_product: "whatsapp",
    recipient_type: "individual",
    to: recipient_number,
    type: "reaction",
    reaction: {
      message_id: message_id,
      emoji: emoji
    }
  }

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_sticker(sender_id:, recipient_number:, sticker_id: nil, link: nil, message_id: nil) ⇒ MessageDataResponse

Send a document.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • sticker_id (String) (defaults to: nil)

    The sticker ID.

  • link (String) (defaults to: nil)

    Image link.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.

Raises:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/whatsapp_sdk/api/messages.rb', line 222

def send_sticker(sender_id:, recipient_number:, sticker_id: nil, link: nil, message_id: nil)
  raise Resource::Errors::MissingArgumentError, "sticker or link is required" if !sticker_id && !link

  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: Resource::Media::Type::STICKER
  }
  params[:sticker] = link ? { link: link } : { id: sticker_id }
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_template(sender_id:, recipient_number:, name:, language:, components: nil, components_json: nil) ⇒ MessageDataResponse

Send template

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • name (String)

    the template’s name.

  • language (String)

    template language.

  • components (Component) (defaults to: nil)

    Component.

  • components_json (Json) (defaults to: nil)

    The component as a Json. If you pass components_json, you can’t pass components.

Returns:

  • (MessageDataResponse)

    Response object.



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/whatsapp_sdk/api/messages.rb', line 352

def send_template(
  sender_id:, recipient_number:, name:, language:, components: nil, components_json: nil
)
  if !components && !components_json
    raise Resource::Errors::MissingArgumentError,
          "components or components_json is required"
  end

  params = {
    messaging_product: "whatsapp",
    recipient_type: "individual",
    to: recipient_number,
    type: "template",
    template: {
      name: name
    }
  }

  params[:template][:language] = { code: language } if language
  params[:template][:components] = if components.nil?
                                     components_json
                                   else
                                     components.map(&:to_json)
                                   end

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_text(sender_id:, recipient_number:, message:, message_id: nil) ⇒ MessageDataResponse

Send a text message.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • message (String)

    Text to send.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/whatsapp_sdk/api/messages.rb', line 17

def send_text(sender_id:, recipient_number:, message:, message_id: nil)
  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "text",
    text: { body: message }
  }
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end

#send_video(sender_id:, recipient_number:, video_id: nil, link: nil, caption: "", message_id: nil) ⇒ MessageDataResponse

Send a video.

Parameters:

  • sender_id (Integer)

    Sender’ phone number.

  • recipient_number (Integer)

    Recipient’ Phone number.

  • video_id (String) (defaults to: nil)

    Video ID.

  • link (String) (defaults to: nil)

    Image link.

  • caption (String) (defaults to: "")

    Image caption.

  • message_id (String) (defaults to: nil)

    The id of the message to reply to.

Returns:

  • (MessageDataResponse)

    Response object.

Raises:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/whatsapp_sdk/api/messages.rb', line 147

def send_video(
  sender_id:, recipient_number:, video_id: nil, link: nil, caption: "", message_id: nil
)
  raise Resource::Errors::MissingArgumentError, "video_id or link is required" if !video_id && !link

  params = {
    messaging_product: "whatsapp",
    to: recipient_number,
    recipient_type: "individual",
    type: "video"
  }
  params[:video] = if link
                     { link: link, caption: caption }
                   else
                     { id: video_id, caption: caption }
                   end
  params[:context] = { message_id: message_id } if message_id

  response = send_request(
    endpoint: endpoint(sender_id),
    params: params,
    headers: DEFAULT_HEADERS
  )

  Api::Responses::MessageDataResponse.build_from_response(response: response)
end