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



234
235
236
237
238
239
240
241
# File 'lib/direct7/whatsapp.rb', line 234

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

#read_receipt(message_id) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/direct7/whatsapp.rb', line 243

def read_receipt(message_id)
  response = @client.post(
    @client.host,
    "/whatsapp/v2/read-receipt/#{message_id}"
  )
  @log.info('Message marked as read successfully.')
  response
end

#send_whatsapp_freeform_message(originator, recipient, message_type, body = nil, first_name = nil, last_name = nil, formatted_name = nil, middle_name = nil, suffix = nil, prefix = nil, birthday = nil, phones = nil, emails = nil, urls = nil, latitude = nil, longitude = nil, name = nil, address = nil, type = nil, url = nil, caption = nil, filename = nil, message_id = nil, emoji = nil, contact_addresses = 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
58
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
# File 'lib/direct7/whatsapp.rb', line 13

def send_whatsapp_freeform_message(originator, recipient, message_type, body=nil, first_name=nil, last_name=nil, formatted_name=nil, middle_name=nil, suffix=nil, prefix=nil, birthday=nil, phones=nil, emails=nil, urls=nil, latitude=nil, longitude=nil, name=nil, address=nil, type=nil, url=nil, caption=nil, filename=nil, message_id=nil, emoji=nil, contact_addresses=nil)
  message = {
    'originator' => originator,
    'recipients' => [{'recipient' => recipient}],
    'content' => {
      'message_type' => message_type
    }
  }

  if message_type
    if message_type == 'CONTACTS'
      message['content']['contacts'] = [{
        'name' => {
          'first_name' => first_name,
          'last_name' => last_name,
          'formatted_name' => formatted_name,
          'middle_name' => middle_name,
          'suffix' => suffix,
          'prefix' => prefix,
        },
        'addresses' => contact_addresses,
        'birthday' => birthday,
        'phones' => phones,
        'emails' => emails,
        'urls' => urls
      }]
    end

    if message_type == 'LOCATION'
      message['content']['location'] = {
        'latitude' => latitude,
        'longitude' => longitude,
        'name' => name,
        'address' => address,
      }
    end
    if message_type == 'ATTACHMENT'
      if type
        if type == "document"
          message['content']['attachment'] = {
            'type' => type,
            'url' => url,
            'caption' => caption,
            'filename' => filename
          }
        else
          message['content']['attachment'] = {
            'type' => type,
            'url' => url,
            'caption' => caption
          }
        end
      end
    end

    if message_type == 'TEXT'
      message['content']['text'] = {
        'body' => body,
      }
    end

    if message_type == 'REACTION'
      message['content']['reaction'] = {
        'message_id' => message_id,
        'emoji' => emoji
      }
    end
  end
  response = @client.post(@client.host, '/whatsapp/v2/send', true, params= {'messages' => [message]})
  @log.info('Message sent successfully.')
  response
end

#send_whatsapp_interactive_message(originator, recipient, interactive_type, header_type = nil, header_text = nil, header_link = nil, header_file_name = nil, body_text = nil, footer_text = nil, parameters = nil, sections = nil, buttons = nil, list_button_text = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/direct7/whatsapp.rb', line 168

def send_whatsapp_interactive_message(originator, recipient, interactive_type, header_type=nil,
  header_text=nil, header_link=nil, header_file_name=nil, body_text=nil,
  footer_text=nil, parameters=nil, sections=nil, buttons=nil,list_button_text=nil)

  message = {
    'originator' => originator,
    'recipients' => [{'recipient' => recipient}],
    'content' => {
      'message_type' => 'INTERACTIVE',
      'interactive' => {
        'type' => interactive_type,
        'header' => {
          'type' => header_type,
        },
        'body' => {
          'text' => body_text,
        },
        'footer' => {
          'text' => footer_text,
        }
      }
    }
  }

  if header_type
    if header_type == 'text'
      message['content']['interactive']['header']["text"] = header_text

    elsif header_type == 'image' || header_type == 'video' || header_type == 'document'
      message['content']['interactive']['header'][header_type] = {
            'filename' => (header_type == "document" ? header_file_name : nil),
            'link' => header_link
        }
    end
  end

  if interactive_type
    if interactive_type == 'cta_url'
      message['content']['interactive']['action'] = {
          'parameters' => parameters,
      }
    elsif interactive_type == 'button'
      message['content']['interactive']['action'] = {
          'buttons' => buttons,
      }
    elsif interactive_type == 'list'
      message['content']['interactive']['action'] = {
          'sections' => sections,
          'button' => list_button_text
      }
    elsif interactive_type == 'location_request_message'
      message['content']['interactive']['action'] = {
          'name' => "send_location"
      }
    elsif interactive_type == 'address_message'
      message['content']['interactive']['action'] = {
          'parameters' => parameters,
      }
    end
  end

  response = @client.post(@client.host, '/whatsapp/v2/send', true, params= { 'messages' => [message] })
  @log.info('Message sent successfully.')
  response
end

#send_whatsapp_templated_message(originator, recipient, template_id, language, body_parameter_values = nil, media_type = nil, text_header_title = nil, media_url = nil, latitude = nil, longitude = nil, name = nil, address = nil, lto_expiration_time_ms = nil, coupon_code = nil, quick_replies = nil, actions = nil, carousel_cards = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/direct7/whatsapp.rb', line 86

def send_whatsapp_templated_message(originator, recipient, template_id, language, body_parameter_values=nil, media_type=nil, text_header_title=nil, media_url=nil, latitude=nil, longitude=nil, name=nil, address=nil, lto_expiration_time_ms=nil, coupon_code=nil, quick_replies=nil, actions=nil, carousel_cards=nil)
  allowed_media_types = ['image', 'document', 'video', 'audio', 'text', 'location']
  template = {
      'template_id' => template_id,
      'language' => language,
      'body_parameter_values' => body_parameter_values ? body_parameter_values : {}
  }

  content = {
      'message_type' => 'TEMPLATE',
      'template' => template
  }

  message = {
    'originator' => originator,
    'recipients' => [{'recipient' => recipient}],
    'content' => content
  }

  if media_type
    unless allowed_media_types.include?(media_type)
      raise ArgumentError, "Invalid media_type: #{media_type}. Allowed values are: #{allowed_media_types.join(', ')}"
    end
    if media_type == 'location'
      message['content']['template']['media'] = {
        'media_type' => media_type,
        'location' => {
          'latitude' => latitude,
          'longitude' => longitude,
          'name' => name,
          'address' => address
        }
      }
    elsif media_type == 'text'
      message['content']['template']['media'] = {
                'media_type' => media_type, 
                'text_header_title' => text_header_title
              }
    else
      message['content']['template']['media'] = { 'media_type' => media_type, 'media_url' => media_url }
    end
  end

  if lto_expiration_time_ms
      message['content']['template']['limited_time_offer'] = {
        'expiration_time_ms' => lto_expiration_time_ms,
      }
  end

  if coupon_code
    message['content']['template']['buttons'] = {
      'coupon_code' => [
        {
            "index"=> 0,
            "type"=> "copy_code",
            "coupon_code"=> coupon_code
        }
      ]
    }
  end

  if quick_replies
    message['content']['template']['buttons'] = {
      'quick_replies' => quick_replies,
    }
  end

  if actions
    message['content']['template']['buttons'] = {
      'actions' => actions,
    }
  end
  if carousel_cards
    message['content']['template']['carousel'] = {
      'cards' => carousel_cards,
    }
  end
  response = @client.post(@client.host, '/whatsapp/v2/send', true, params= {'messages' => [message]})
  @log.info('Message sent successfully.')
  response
end