Class: Direct7::WHATSAPP

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ WHATSAPP

Returns a new instance of WHATSAPP.



8
9
10
11
# File 'lib/direct7/whatsapp.rb', line 8

def initialize(client)
  @client = client
  @log = Logger.new(STDOUT) # You can customize the log destination as needed
end

Instance Method Details

#get_status(request_id) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/direct7/whatsapp.rb', line 96

def get_status(request_id)
  response = @client.get(
    @client.host,
    "/whatsapp/v1/report/#{request_id}"
  )
  @log.info('Message status retrieved successfully.')
  response
end

#send_whatsapp_freeform_message(originator, recipient, message_type, message_text = nil, first_name = nil, last_name = nil, display_name = nil, phone = nil, email = nil, url = nil, latitude = nil, longitude = nil, location_name = nil, location_address = nil, attachment_type = nil, attachment_url = nil, attachment_caption = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/direct7/whatsapp.rb', line 13

def send_whatsapp_freeform_message(originator, recipient, message_type, message_text= nil, first_name= nil,
                                   last_name= nil, display_name= nil, phone= nil,
                                   email= nil, url= nil, latitude= nil, longitude= nil,
                                   location_name= nil, location_address= nil,
                                   attachment_type= nil, attachment_url= nil,
                                   attachment_caption= nil)
  message = {
    'originator' => originator,
    'recipients' => [{'recipient' => recipient}],
    'content' => {
      'message_type' => message_type
    }
  }

  case message_type
  when 'CONTACTS'
    message['content']['contact'] = {
      'first_name' => first_name,
      'last_name' => last_name,
      'display_name' => display_name,
      'phone' => phone,
      'email' => email,
      'url' => url
    }
  when 'LOCATION'
    message['content']['location'] = {
      'latitude' => latitude,
      'longitude' => longitude,
      'name' => location_name,
      'address' => location_address
    }
  when 'ATTACHMENT'
    message['content']['attachment'] = {
      'attachment_type' => attachment_type,
      'attachment_url' => attachment_url,
      'attachment_caption' => attachment_caption
    }
  when 'TEXT'
    message['content']['message_text'] = message_text
  end
  puts message
  response = @client.post(@client.host, '/whatsapp/v1/send', true, params= { 'messages' => [message] })
  @log.info('Message sent successfully.')
  response
end

#send_whatsapp_templated_message(originator, recipient, template_id, body_parameter_values, media_type = nil, media_url = nil, latitude = nil, longitude = nil, location_name = nil, location_address = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/direct7/whatsapp.rb', line 59

def send_whatsapp_templated_message(originator, recipient, template_id,
                                    body_parameter_values, media_type= nil, media_url= nil,
                                    latitude= nil, longitude= nil, location_name= nil,
                                    location_address= nil)
  message = {
    'originator' => originator,
    'recipients' => [{'recipient' => recipient}],
    'content' => {
      'message_type' => 'TEMPLATE',
      'template' => {
        'template_id' => template_id,
        'body_parameter_values' => body_parameter_values
      }
    }
  }

  if media_type
    if media_type == 'location'
      message['content']['template']['media'] = {
        'media_type' => 'location',
        'location' => {
          'latitude' => latitude,
          'longitude' => longitude,
          'name' => location_name,
          'address' => location_address
        }
      }
    else
      message['content']['template']['media'] = { 'media_type' => media_type, 'media_url' => media_url }
    end
  end
  
  response = @client.post(@client.host, '/whatsapp/v1/send', true, params= { 'messages' => [message] })
  @log.info('Message sent successfully.')
  response
end