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



131
132
133
134
135
136
137
138
# File 'lib/direct7/whatsapp.rb', line 131

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, body = nil, first_name = nil, last_name = nil, formatted_name = nil, phones = nil, emails = nil, urls = nil, latitude = nil, longitude = nil, name = nil, address = nil, type = nil, url = nil, 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
# 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, phones=nil, emails=nil, urls=nil, latitude=nil, longitude=nil, name=nil, address=nil, type=nil, url=nil, caption=nil)
  message = {
    'originator' => originator,
    'recipients' => [{'recipient' => recipient}],
    'content' => {
      'message_type' => message_type
    }
  }

  case message_type
  when 'CONTACTS'
    message['content']['contacts'] = [{
      'name' => {
        'first_name' => first_name,
        'last_name' => last_name,
        'formatted_name' => formatted_name,
      },
      'phones' => (phones.map { |phone| { 'phone' => phone } } if phones),
      'emails' => (emails.map { |email| { 'email' => email } } if emails),
      'urls' => (urls.map { |url| { 'url' => url } } if urls),
    }]
  when 'LOCATION'
    message['content']['location'] = {
      'latitude' => latitude,
      'longitude' => longitude,
      'name' => name,
      'address' => address,
    }
  when 'ATTACHMENT'
    message['content']['attachment'] = {
      'type' => type,
      'url' => url,
      'caption' => caption,
    }
  when 'TEXT'
    message['content']['text'] = {
      'body' => body,
    }
  end

  response = @client.post(@client.host, '/whatsapp/v2/send', 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, lto_expiration_time_ms = nil, coupon_code = nil, quick_replies = nil, actions = nil, carousel_cards = nil) ⇒ Object



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
85
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
# File 'lib/direct7/whatsapp.rb', line 58

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,lto_expiration_time_ms=nil, coupon_code=nil, quick_replies= nil, actions= nil, carousel_cards=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

  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' => actquick_repliesions,
    }
  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