Method: Direct7::WHATSAPP#send_whatsapp_templated_message

Defined in:
lib/direct7/whatsapp.rb

#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