Class: Cpaas::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/cpaas-sdk/resources/conversation.rb

Overview

CPaaS conversation.

Class Method Summary collapse

Class Method Details

.create_message(params) ⇒ Object

Send a new outbound message

Parameters:

  • params (Hash)

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :sender_address (String)

    Sender address information, basically the from address. E164 formatted DID number passed as a value, which is owned by the user. If the user wants to let CPaaS uses the default assigned DID number, this field can either has “default” value or the same value as the userId.

  • :destination_address (Array[string]|String)

    Indicates which DID number(s) used as destination for this SMS.

  • :message (String)

    SMS text 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.create_message(params)
  if params[:type] == types[:SMS]
    address = (params[:destination_address].is_a? String) ? [ params[:destination_address] ] : params[:destination_address]

    options = {
      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", options, :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

Parameters:

  • params (Hash)

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :remote_address (String)

    Remote address information while retrieving the conversation history, basically the destination telephone number that user exchanged message before. E164 formatted DID number passed as a value.

  • :local_address (String)

    Local address information while retrieving the conversation history, basically the source telephone number that user exchanged message before.

  • :message_id (String)

    optional Identification of the message. If messeageId is not passsed then the conversation thread is deleted with all messages.



97
98
99
100
101
102
103
104
105
# File 'lib/cpaas-sdk/resources/conversation.rb', line 97

def self.delete_message(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.

Parameters:

  • params (Hash)

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :remote_address (String)

    optional Remote address information while retrieving the conversation history, basically the destination telephone number that user exchanged message before. E164 formatted DID number passed as a value.

  • :local_address (String)

    optional Local address information while retrieving the conversation history, basically the source telephone number that user exchanged message before.

  • :query[:name] (String)

    optional - Performs search operation on firstName and lastName fields.

  • :query[:first_name] (String)

    optional - Performs search for the first_name field of the directory items.

  • :query[:last_name] (String)

    optional - Performs search for the last_name field of the directory items.

  • :query[:user_name] (String)

    optional - Performs search for the user_name field of the directory items.

  • :query[:phone_number] (String)

    optional - Performs search for the fields containing a phone number, like businessPhoneNumber, homePhoneNumber, mobile, pager, fax.

  • :query[:order] (String)

    optional - Ordering the contact results based on the requested sortBy value, order query parameter should be accompanied by sortBy query parameter.

  • :query[:sort_by] (String)

    optional - sort_by value is used to detect sorting the contact results based on which attribute. If order is not provided with that, ascending order is used.

  • :query[:max] (Number)

    optional - Maximum number of contact results that has been requested from CPaaS for this query.

  • :query[:next] (String)

    optional - Pointer for the next chunk of contacts, should be gathered from the previous query results.



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.get_messages(params)
  if params[:type] == types[:SMS]
    options = {
      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, options)

    process_response(response) do |res|
      if params[:local_address]
        res.dig(:sms_thread_list, :sms_thread)
          .map { |i| reject(l, :resource_url) }
      else
        message = res.dig(:sms_thread)
        reject(message, :resource_url)
      end
    end
  end
end

.get_messages_in_thread(params) ⇒ Object

Read all messages in a thread

Parameters:

  • params (Hash)

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :remote_address (String)

    Remote address information while retrieving the conversation history, basically the destination telephone number that user exchanged message before. E164 formatted DID number passed as a value.

  • :local_address (String)

    Local address information while retrieving the conversation history, basically the source telephone number that user exchanged message before.

  • :query[:next] (String)

    optional - Pointer for the next page to retrieve for the messages, provided by CPaaS in previous GET response.

  • :query[:max] (String)

    optional - Number of messages that is requested from CPaaS.

  • :query[:new] (String)

    optional - Filters the messages or threads having messages that are not received by the user yet.

  • :query[:last_Message_Time] (String)

    optional - Filters the messages or threads having messages that are sent/received after provided Epoch time



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.get_messages_in_thread(params)
  if params[:type] == types[:SMS]
    options = {
      query: params[:query]
    }

    response = Cpaas.api.send_request("#{base_url}/remoteAddresses/#{params[:remote_address]}/localAddresses/#{params[:local_address]}/messages", options)

    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

Parameters:

  • params (Hash)

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :remote_address (String)

    Remote address information while retrieving the conversation history, basically the destination telephone number that user exchanged message before. E164 formatted DID number passed as a value.

  • :local_address (String)

    Local address information while retrieving the conversation history, basically the source telephone number that user exchanged message before.

  • :message_id (String)

    Identification of the message. If messeageId is not passsed then the conversation thread is deleted with all messages.



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

Parameters:

  • params (Hash)

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :subscription_id (String)

    Resource ID of the 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

Parameters:

  • params (Hash)

    a customizable set of options

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options



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

Parameters:

  • params (Hash)

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :webhook_url (String)

    HTTPS URL that is present in your application server which is accessible from the public web where the notifications should be sent to. Note: Should be a POST endpoint.

  • :destination_address (String)

    optional The address that incoming messages are received for this subscription. If does not exist, CPaaS uses the default assigned DID number to subscribe against. It is suggested to provide the intended E164 formatted DID number within this parameter.



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?

    options = {
      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", options, :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

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :type (String)

    Type of conversation. Possible value(s) - 'sms'. Check Conversation.types for more options

  • :subscription_id (String)

    Resource ID of the subscription.



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