Class: Direct7::WHATSAPP
- Inherits:
-
Object
- Object
- Direct7::WHATSAPP
- Defined in:
- lib/direct7/whatsapp.rb
Instance Method Summary collapse
- #get_status(request_id) ⇒ Object
-
#initialize(client) ⇒ WHATSAPP
constructor
A new instance of WHATSAPP.
- #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
- #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
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 (originator, recipient, , 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) = { 'originator' => originator, 'recipients' => [{'recipient' => recipient}], 'content' => { 'message_type' => } } case when 'CONTACTS' ['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' ['content']['location'] = { 'latitude' => latitude, 'longitude' => longitude, 'name' => name, 'address' => address, } when 'ATTACHMENT' ['content']['attachment'] = { 'type' => type, 'url' => url, 'caption' => caption, } when 'TEXT' ['content']['text'] = { 'body' => body, } end response = @client.post(@client.host, '/whatsapp/v2/send', params: { 'messages' => [] }) @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 (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) = { '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' ['content']['template']['media'] = { 'media_type' => 'location', 'location' => { 'latitude' => latitude, 'longitude' => longitude, 'name' => location_name, 'address' => location_address } } else ['content']['template']['media'] = { 'media_type' => media_type, 'media_url' => media_url } end end if lto_expiration_time_ms ['content']['template']['limited_time_offer'] = { 'expiration_time_ms' => 'lto_expiration_time_ms', } end if coupon_code ['content']['template']['buttons'] = { 'coupon_code' => [ { "index"=> 0, "type"=> "copy_code", "coupon_code"=> coupon_code } ] } end if quick_replies ['content']['template']['buttons'] = { 'quick_replies' => actquick_repliesions, } end if actions ['content']['template']['buttons'] = { 'actions' => actions, } end if carousel_cards ['content']['template']['carousel'] = { 'cards' => carousel_cards, } end response = @client.post(@client.host, '/whatsapp/v2/send', true, params: { 'messages' => [] }) @log.info('Message sent successfully.') response end |