Class: Cpaas::Conversation
- Inherits:
-
Object
- Object
- Cpaas::Conversation
- Defined in:
- lib/cpaas-sdk/resources/conversation.rb
Overview
CPaaS conversation.
Class Method Summary collapse
-
.create_message(params) ⇒ Object
Send a new outbound message.
-
.delete_message(params) ⇒ Object
Delete conversation message.
-
.get_messages(params) ⇒ Object
Gets all messages.
-
.get_messages_in_thread(params) ⇒ Object
Read all messages in a thread.
-
.get_status(params) ⇒ Object
Read a conversation message status.
-
.get_subscription(params) ⇒ Object
Read active subscription.
-
.get_subscriptions(params) ⇒ Object
Read all active subscriptions.
-
.subscribe(params) ⇒ Object
Create a new subscription.
-
.unsubscribe(params = {}) ⇒ Object
Unsubscription from conversation notification.
Class Method Details
.create_message(params) ⇒ Object
Send a new outbound message
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 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 18 def self.(params) if params[:type] == types[:SMS] address = (params[:destination_address].is_a? String) ? [ params[:destination_address] ] : params[:destination_address] = { body: { outboundSMSMessageRequest: { address: address, clientCorrelator: Cpaas.api.client_correlator, outboundSMSTextMessage: { message: params[:message] } } } } response = Cpaas.api.send_request("#{base_url}/outbound/#{params[:sender_address]}/requests", , :post) process_response(response) do |res| outboundSMS = res.dig(:outbound_sms_message_request) { message: outboundSMS.dig(:outbound_sms_text_message, :message), senderAddress: outboundSMS.dig(:sender_address), deliveryInfo: outboundSMS.dig(:delivery_info_list, :delivery_info) } end end end |
.delete_message(params) ⇒ Object
Delete conversation message
97 98 99 100 101 102 103 104 105 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 97 def self.(params) if params[:type] == types[:SMS] url = "#{base_url}/remoteAddresses/#{params[:remote_address]}/localAddresses/#{params[:local_address]}" url += "/messages/#{params[:message_id]}" if params[:message_id] Cpaas.api.send_request(url, {}, :delete) end end |
.get_messages(params) ⇒ Object
Gets all messages.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 64 def self.(params) if params[:type] == types[:SMS] = { query: params[:query] } url = "#{base_url}/remoteAddresses" url += "/#{params[:remote_address]}" if params[:remote_address] url += "/localAddresses/#{params[:local_address]}" if params[:local_address] response = Cpaas.api.send_request(url, ) process_response(response) do |res| if params[:local_address] res.dig(:sms_thread_list, :sms_thread) .map { |i| reject(l, :resource_url) } else = res.dig(:sms_thread) reject(, :resource_url) end end end end |
.get_messages_in_thread(params) ⇒ Object
Read all messages in a thread
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 119 def self.(params) if params[:type] == types[:SMS] = { query: params[:query] } response = Cpaas.api.send_request("#{base_url}/remoteAddresses/#{params[:remote_address]}/localAddresses/#{params[:local_address]}/messages", ) process_response(response) do |res| res.dig(:sms_message_list, :sms_message) .map { |m| reject(m, :resource_url) } end end end |
.get_status(params) ⇒ Object
Read a conversation message status
143 144 145 146 147 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 143 def self.get_status(params) if params[:type] == types[:SMS] Cpaas.api.send_request("#{base_url}/remoteAddresses/#{params[:remote_address]}/localAddresses/#{params[:local_address]}/messages/#{params[:message_id]}/status") end end |
.get_subscription(params) ⇒ Object
Read active subscription
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 179 def self.get_subscription(params) if params[:type] == types[:SMS] response = Cpaas.api.send_request("#{base_url}/inbound/subscriptions/#{params[:subscription_id]}") process_response(response) do |res| subscription = res.dig(:subscription) { notify_url: subscription.dig(:callback_reference, :notify_url), destination_address: subscription.dig(:destination_address), subscription_id: id_from(subscription.dig(:resource_url)) } end end end |
.get_subscriptions(params) ⇒ Object
Read all active subscriptions
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 155 def self.get_subscriptions(params) if params[:type] == types[:SMS] response = Cpaas.api.send_request("#{base_url}/inbound/subscriptions") process_response(response) do |res| res.dig(:subscription_list, :subscription) .map do |subscriptions| { notify_url: subscription.dig(:callback_reference, :notify_url), destination_address: subscription.dig(:destination_address), subscription_id: id_from(subscription.dig(:resource_url)) } end end end end |
.subscribe(params) ⇒ Object
Create a new subscription
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 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 204 def self.subscribe(params) if params[:type] == types[:SMS] channel = Cpaas::NotificationChannel.create_channel(webhook_url: params[:webhook_url]) return channel if !channel.dig(:exception_id).nil? = { body: { subscription: { callbackReference: { notifyURL: channel[:channel_id] }, clientCorrelator: Cpaas.api.client_correlator, destinationAddress: params[:destination_address] } } } response = Cpaas.api.send_request("#{base_url}/inbound/subscriptions", , :post) process_response(response) do |res| { webhook_url: params[:webhook_url], destination_address: response.dig(:subscription, :destination_address), subscription_id: id_from(response.dig(:subscription, :resource_url)) } end end end |
.unsubscribe(params = {}) ⇒ Object
Unsubscription from conversation notification
242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/cpaas-sdk/resources/conversation.rb', line 242 def self.unsubscribe(params = {}) if params[:type] == types[:SMS] response = Cpaas.api.send_request("#{base_url}/inbound/subscriptions/#{params[:subscription_id]}", {}, :delete) process_response(response) do |res| { subscription_id: params[:subscription_id], success: true, message: "Unsubscribed from #{params[:type]} conversation notification" } end end end |