Class: API::Messages::Service
- Inherits:
-
Object
- Object
- API::Messages::Service
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/api/messages/service.rb
Constant Summary collapse
- MESSAGING_PRODUCT =
::T.let("whatsapp", ::String)
- RECIPIENT_TYPE =
::T.let("individual", ::String)
Instance Method Summary collapse
- #download_media(media_url:) ⇒ Object
- #fetch_media_url(media_id:) ⇒ Object
-
#initialize(config:) ⇒ Service
constructor
A new instance of Service.
- #send_audio(recipient:, link:, reply_message_id: nil) ⇒ Object
- #send_contact(recipient:, contacts:, reply_message_id: nil) ⇒ Object
- #send_document(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object
- #send_image(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object
- #send_location(recipient:, longitude:, latitude:, name:, address:, reply_message_id: nil) ⇒ Object
- #send_reaction(recipient:, emoji:, reply_message_id: nil) ⇒ Object
- #send_template(recipient:, template_name:, template_lang:, components:) ⇒ Object
- #send_text(recipient:, body:, reply_message_id: nil) ⇒ Object
- #send_video(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object
Constructor Details
#initialize(config:) ⇒ Service
Returns a new instance of Service.
13 14 15 |
# File 'lib/api/messages/service.rb', line 13 def initialize(config:) @config = config end |
Instance Method Details
#download_media(media_url:) ⇒ Object
257 258 259 260 261 |
# File 'lib/api/messages/service.rb', line 257 def download_media(media_url:) client = ::CloudWaba::HttpClient.new(base_url: media_url, auth_token: @config.access_token) response = client.get build_file(response: response) end |
#fetch_media_url(media_id:) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/api/messages/service.rb', line 246 def fetch_media_url(media_id:) url = "#{@config.base_url}/#{@config.api_version}/#{media_id}" client = ::CloudWaba::HttpClient.new(base_url: url, auth_token: @config.access_token) response = with_error_handling { client.get } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Media::Response.parse(hash: parsed_response) end |
#send_audio(recipient:, link:, reply_message_id: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/api/messages/service.rb', line 68 def send_audio(recipient:, link:, reply_message_id: nil) audio_type = "audio" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": audio_type, } payload["audio"] = { link: link } payload["context"] = { "message_id": } unless .nil? response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_contact(recipient:, contacts:, reply_message_id: nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/api/messages/service.rb', line 157 def send_contact(recipient:, contacts:, reply_message_id: nil) contacts_type = "contacts" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": contacts_type, } payload["contacts"] = contacts.map(&:serialize) payload["context"] = { "message_id": } unless .nil? response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_document(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/api/messages/service.rb', line 110 def send_document(recipient:, caption:, link:, reply_message_id: nil) document_type = "document" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": document_type, } payload["document"] = { link: link, caption: } payload["context"] = { "message_id": } unless .nil? response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_image(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/api/messages/service.rb', line 47 def send_image(recipient:, caption:, link:, reply_message_id: nil) image_type = "image" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": image_type, } payload["image"] = { link: link, caption: } payload["context"] = { "message_id": } unless .nil? response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_location(recipient:, longitude:, latitude:, name:, address:, reply_message_id: nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/api/messages/service.rb', line 131 def send_location(recipient:, longitude:, latitude:, name:, address:, reply_message_id: nil) location_type = "location" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": location_type, } payload["location"] = { "longitude": longitude, "latitude": latitude, "name": name, "address": address } payload["context"] = { "message_id": } unless .nil? response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_reaction(recipient:, emoji:, reply_message_id: nil) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/api/messages/service.rb', line 192 def send_reaction(recipient:, emoji:, reply_message_id: nil) reaction_type = "reaction" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": reaction_type, } payload["reaction"] = { "message_id": , "emoji": emoji } response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_template(recipient:, template_name:, template_lang:, components:) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/api/messages/service.rb', line 221 def send_template(recipient:, template_name:, template_lang:, components:) template_type = "template" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": template_type, "template": { "name": template_name, "language": { "code": template_lang }, "components": [] } } response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_text(recipient:, body:, reply_message_id: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/api/messages/service.rb', line 21 def send_text(recipient:, body:, reply_message_id: nil) text_type = "text" enable_preview = false payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": text_type, "text": { "preview_url": enable_preview, "body": body } } payload["context"] = { "message_id": } unless .nil? response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |
#send_video(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/api/messages/service.rb', line 89 def send_video(recipient:, caption:, link:, reply_message_id: nil) video_type = "video" payload = { "messaging_product": MESSAGING_PRODUCT, "recipient_type": RECIPIENT_TYPE, "to": recipient, "type": video_type, } payload["video"] = { link: link, caption: } payload["context"] = { "message_id": } unless .nil? response = with_error_handling { http_client.post(body: payload, headers: {}) } parsed_response = JSON.parse(response.body.to_s) ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response) end |