Class: Direct7::WHATSAPP
- Inherits:
-
Object
- Object
- Direct7::WHATSAPP
- Defined in:
- lib/direct7/whatsapp.rb
Instance Method Summary collapse
- #download_media(media_id) ⇒ Object
- #get_status(request_id) ⇒ Object
-
#initialize(client) ⇒ WHATSAPP
constructor
A new instance of WHATSAPP.
- #read_receipt(message_id) ⇒ Object
- #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
- #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
- #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, button_flow = 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
#download_media(media_id) ⇒ Object
260 261 262 263 264 265 266 267 |
# File 'lib/direct7/whatsapp.rb', line 260 def download_media(media_id) response = @client.get( @client.host, "/whatsapp/v2/download/#{media_id}" ) @log.info('Successfully downloaded media.') response end |
#get_status(request_id) ⇒ Object
242 243 244 245 246 247 248 249 |
# File 'lib/direct7/whatsapp.rb', line 242 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
251 252 253 254 255 256 257 258 |
# File 'lib/direct7/whatsapp.rb', line 251 def read_receipt() 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 (originator, recipient, , 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, =nil, emoji=nil, contact_addresses=nil) = { 'originator' => originator, 'recipients' => [{'recipient' => recipient}], 'content' => { 'message_type' => } } if if == 'CONTACTS' ['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 == 'LOCATION' ['content']['location'] = { 'latitude' => latitude, 'longitude' => longitude, 'name' => name, 'address' => address, } end if == 'ATTACHMENT' if type if type == "document" ['content']['attachment'] = { 'type' => type, 'url' => url, 'caption' => caption, 'filename' => filename } else ['content']['attachment'] = { 'type' => type, 'url' => url, 'caption' => caption } end end end if == 'TEXT' ['content']['text'] = { 'body' => body, } end if == 'REACTION' ['content']['reaction'] = { 'message_id' => , 'emoji' => emoji } end end response = @client.post(@client.host, '/whatsapp/v2/send', true, params= {'messages' => []}) @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
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 233 234 235 236 237 238 239 240 |
# File 'lib/direct7/whatsapp.rb', line 174 def (originator, recipient, interactive_type, header_type=nil, header_text=nil, header_link=nil, header_file_name=nil, body_text=nil, =nil, parameters=nil, sections=nil, =nil,=nil) = { 'originator' => originator, 'recipients' => [{'recipient' => recipient}], 'content' => { 'message_type' => 'INTERACTIVE', 'interactive' => { 'type' => interactive_type, 'header' => { 'type' => header_type, }, 'body' => { 'text' => body_text, }, 'footer' => { 'text' => , } } } } if header_type if header_type == 'text' ['content']['interactive']['header']["text"] = header_text elsif header_type == 'image' || header_type == 'video' || header_type == 'document' ['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' ['content']['interactive']['action'] = { 'parameters' => parameters, } elsif interactive_type == 'button' ['content']['interactive']['action'] = { 'buttons' => , } elsif interactive_type == 'list' ['content']['interactive']['action'] = { 'sections' => sections, 'button' => } elsif interactive_type == 'location_request_message' ['content']['interactive']['action'] = { 'name' => "send_location" } elsif interactive_type == 'address_message' ['content']['interactive']['action'] = { 'parameters' => parameters, } elsif interactive_type == 'flow' ['content']['interactive']['action'] = parameters end end response = @client.post(@client.host, '/whatsapp/v2/send', true, params= { 'messages' => [] }) @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, button_flow = 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 167 168 169 170 171 172 |
# File 'lib/direct7/whatsapp.rb', line 86 def (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,=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 } = { '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' ['content']['template']['media'] = { 'media_type' => media_type, 'location' => { 'latitude' => latitude, 'longitude' => longitude, 'name' => name, 'address' => address } } elsif media_type == 'text' ['content']['template']['media'] = { 'media_type' => media_type, 'text_header_title' => text_header_title } 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 ['content']['template']['button'] = { "button_flow"=>, } 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' => quick_replies, } 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 |