Module: UnifonicSms

Defined in:
lib/unifonic_sms.rb,
lib/unifonic_sms/version.rb,
lib/unifonic_sms/error_code.rb,
lib/unifonic_sms/normalizer.rb,
lib/unifonic_sms/configuration.rb

Defined Under Namespace

Modules: ErrorCode, Normalizer Classes: Configuration

Constant Summary collapse

VERSION =

Gem Version

"1.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationQiwaSms::Configuration

Get the Configurations or Reset.

Returns:

  • (QiwaSms::Configuration)

    Configuration.



15
16
17
# File 'lib/unifonic_sms.rb', line 15

def configuration
  @configuration
end

Class Method Details

.api_keyString

Get the api key from the configurations.

Returns:

  • (String)

    Api Key.



32
33
34
# File 'lib/unifonic_sms.rb', line 32

def api_key
  configuration.api_key
end

.balanceString

Get Account’s Current Balance.

Returns:

  • (String)

    balance of the Account.

  • (String)

    shared balance with sub accounts.

  • (String)

    currency code used with cost.

  • (String)

    Code status of the response if 0 its a success else its an error.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/unifonic_sms.rb', line 57

def balance
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Account/GetBalance")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"

  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["data"].nil? 
    return { balance: response_body["data"]["Balance"],
             currency_code: response_body["data"]["CurrencyCode"],
             shared_balance: response_body["data"]["SharedBalance"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end         
end

.base_path(method_url) ⇒ String

Get the base url for api call.

Parameters:

  • method_url (String)

    the api method url.

Returns:

  • (String)

    Url for Api Call.



47
48
49
# File 'lib/unifonic_sms.rb', line 47

def base_path (method_url)
  "/rest/#{method_url}"
end

.configure {|configuration| ... } ⇒ Object

Holds the configurtion block.

Yields:



25
26
27
# File 'lib/unifonic_sms.rb', line 25

def configure
  yield(configuration)
end

.inbox(number, keyword = nil, date_from = nil, date_to = nil) ⇒ String, Array<Hash>

The Keyword method enables you to manage your numbers, create auto replies to incoming messages or set a webhook directly from your API, Check Keywords Management to view and edit your keywords.

Parameters:

  • number (String)

    to manage.

  • keyword (String, nil) (defaults to: nil)

    to use.

  • date_from (String, nil) (defaults to: nil)

    The start date for the report time interval, date format should be yyyy-mm-dd.

  • date_to (String, nil) (defaults to: nil)

    The end date for the report time interval, date format should be yyyy-mm-dd.

Returns:

  • (String)

    Number of messages.

  • (String)

    message from Recipient number.

  • (Array<Hash>)

    received message.

  • (String)

    Code status of the response if 0 its a success else its an error.

See Also:



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/unifonic_sms.rb', line 459

def inbox (number, keyword = nil, date_from = nil, date_to = nil)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/Inbox")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"
  body += "&Number=#{number}"
  body += "&Keyword=#{keyword}" unless keyword.nil?
  body += "&FromDate=#{date_from}" unless date_from.nil?
  body += "&ToDate=#{date_to}" unless date_to.nil?

  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["data"].nil? 
    return { number_of_messages: response_body["data"]["NumberOfMessages"],
             message_from: response_body["data"]["MessageFrom"],
             message: response_body["data"]["Message"],
             date_recieved: response_body["data"]["DateReceived"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.keyword(number, keyword, rule, message = nil, webhook_url = nil, message_parameter = nil, recipient_parameter = nil, request_type = nil, resource_number = nil) ⇒ String

The Keyword method enables you to manage your numbers, create auto replies to incoming messages or set a webhook directly from your API, Check Keywords Management to view and edit your keywords.

Parameters:

  • number (String)

    to manage.

  • keyword (String)

    to use.

  • rule (String)

    for the keyword.

  • message (String, nil) (defaults to: nil)

    Set an auto reply to send back to the user (Ex: You have been successfully registered ).

  • webhook_url (String, nil) (defaults to: nil)

    Defines the source that you want to make the callback to for example “www.google.com”.

  • message_parameter (String, nil) (defaults to: nil)

    Set the parameters that the source takes for example www.google.jo/search?q=hello&oq=hello then your parameters that you have to set are q and oq.

  • recipient_parameter (String, nil) (defaults to: nil)

    Set the parameters that the source takes for example www.google.jo/search?q=hello&oq=hello then your parameters that you have to set are q and oq.

  • request_type (String, nil) (defaults to: nil)

    Defines the http callback methods , it can be either [Post: Requests data from a specified resource] or [Get: Submits data to be processed to a specified resource].

  • resource_number (String, nil) (defaults to: nil)

    your inbound number for example 70001.

Returns:

  • (String)

    Success True, to indicate successfully create of a new keyword.

  • (String)

    Code status of the response if 0 its a success else its an error.

See Also:



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/unifonic_sms.rb', line 411

def keyword (number, keyword, rule, message = nil, webhook_url = nil, message_parameter = nil, recipient_parameter = nil, request_type = nil, resource_number = nil)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/Keyword")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"
  body += "&Number=#{number}"
  body += "&Keyword=#{keyword}"
  body += "&Rule=#{rule}"
  body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
  body += "&Message=#{message}" unless message.nil?
  body += "&WebhookURL=#{webhook_url}" unless webhook_url.nil?
  body += "&MessageParameter=#{message_parameter}" unless message_parameter.nil?
  body += "&RecipientParameter=#{recipient_parameter}" unless recipient_parameter.nil?
  body += "&RequestType =#{request_type}" unless request_type.nil?
  body += "&ResourceNumber  =#{resource_number}" unless resource_number.nil?

  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 
    return { success: response_body["Success"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.message_status(message_id) ⇒ String

Get Message Status.

Parameters:

  • message_id (String)

    The ID of the message.

Returns:

  • (String)

    Status of message possible values are “Queued” , “Sent”, “Failed” and “Rejected”.

  • (String)

    DLR Message delivery status returned by networks, the possible values are “Delivered” or “Undeliverable”, and are available for advanced plans.

  • (String)

    Code status of the response if 0 its a success else its an error.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/unifonic_sms.rb', line 207

def message_status (message_id)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/GetMessageIDStatus")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}&MessageID=#{message_id}"
  
  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["Status"].nil? 
    return { status: response_body["Status"], code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.messages_details(message_id = nil, date_from = nil, date_to = nil, status = nil, dlr = nil, country = nil, limit = nil) ⇒ String, Array<Hash>

Get latest 10,000 messages details if no message id was provided.

Parameters:

  • message_id (String, nil) (defaults to: nil)

    The ID of the message.

  • date_from (String, nil) (defaults to: nil)

    The start date for the report time interval, date format should be yyyy-mm-dd.

  • date_to (String, nil) (defaults to: nil)

    The end date for the report time interval, date format should be yyyy-mm-dd.

  • status (String, nil) (defaults to: nil)

    Filter messages report according to a specific message status, “Sent”, “Queued”, “Rejected” or “Failed”.

  • dlr (String, nil) (defaults to: nil)

    Message delivery status returned by networks, the possible values are “Delivered” or “Undeliverable”, and are available for advanced plans.

  • country (String, nil) (defaults to: nil)

    Filter messages report according to a specific destination country.

  • limit (String, nil) (defaults to: nil)

    Number of messages to return in the report, where the limit maximum is 10,000 and messages are sorted by sending date.

Returns:

  • (String)

    Number of messages.

  • (String)

    Page number.

  • (Array<Hash>)

    Messages.

  • (String)

    Code status of the response if 0 its a success else its an error.



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/unifonic_sms.rb', line 289

def messages_details (message_id = nil, date_from = nil, date_to = nil, status = nil, dlr = nil, country = nil, limit = nil)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/GetMessagesDetails")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"
  body += "&MessageID=#{message_id}" unless message_id.nil?
  body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
  body += "&DateFrom=#{date_from}" unless date_from.nil?
  body += "&DateTo=#{date_to}" unless date_to.nil?
  body += "&Status=#{status}" unless status.nil?
  body += "&DLR=#{dlr}" unless dlr.nil?
  body += "&Country=#{country}" unless country.nil?
  body += "&Limit=#{limit}" unless limit.nil?

  
  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["data"].nil? 
    return { total_text_messages: response_body["data"]["TotalTextMessages"],
             page: response_body["data"]["Page"], 
             messages: response_body["data"]["messages"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.messages_report(date_from = nil, date_to = nil, status = nil, dlr = nil, country = nil) ⇒ String

To get a summarized report for sent messages within a specific timer interval

Parameters:

  • date_from (String, nil) (defaults to: nil)

    The start date for the report time interval, date format should be yyyy-mm-dd.

  • date_to (String, nil) (defaults to: nil)

    The end date for the report time interval, date format should be yyyy-mm-dd.

  • status (String, nil) (defaults to: nil)

    Filter messages report according to a specific message status, “Sent”, “Queued”, “Rejected” or “Failed”.

  • dlr (String, nil) (defaults to: nil)

    Message delivery status returned by networks, the possible values are “Delivered” or “Undeliverable”, and are available for advanced plans.

  • country (String, nil) (defaults to: nil)

    Filter messages report according to a specific destination country.

Returns:

  • (String)

    Number of messages.

  • (String)

    Number of unit in a message.

  • (String)

    Price of a message total units.

  • (String)

    The currency code used eith cost.

  • (String)

    Code status of the response if 0 its a success else its an error.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/unifonic_sms.rb', line 242

def messages_report (date_from = nil, date_to = nil, status = nil, dlr = nil, country = nil)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/GetMessagesReport")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"
  body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
  body += "&DateFrom=#{date_from}" unless date_from.nil?
  body += "&DateTo=#{date_to}" unless date_to.nil?
  body += "&Status=#{status}" unless status.nil?
  body += "&DLR=#{dlr}" unless dlr.nil?
  body += "&Country=#{country}" unless country.nil?

  
  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["TotalTextMessages"].nil? 
    return { total_text_messages: response_body["TotalTextMessages"],
             number_of_units: response_body["NumberOfUnits"], 
             cost: response_body["Cost"],
             currency_code: response_body["CurrencyCode"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.pricing(country_code = nil) ⇒ Hash, String

The Keyword method enables you to manage your numbers, create auto replies to incoming messages or set a webhook directly from your API, Check Keywords Management to view and edit your keywords.

Parameters:

  • country_code (String, nil) (defaults to: nil)

    The Country code to check its prices.

Returns:

  • (Hash)

    Country Data.

  • (String)

    Country name.

  • (String)

    Code status of the response if 0 its a success else its an error.

See Also:



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/unifonic_sms.rb', line 500

def pricing (country_code = nil)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/Pricing")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"
  body += "&CountryCode=#{country_code}" unless country_code.nil?

  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["data"].nil? 
    return { country: response_body["data"]["CountryCode"],
             country_name: response_body["data"]["CountryName"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.resetObject

Reset Configuration to nil.



20
21
22
# File 'lib/unifonic_sms.rb', line 20

def reset
  @configuration = Configuration.new
end

.schedualed_messages(message_id = nil) ⇒ String

Get a summarized report for scheduled sent messages.

Parameters:

  • message_id (String, nil) (defaults to: nil)

    The ID of the message.

Returns:

  • (String)

    Message ID.

  • (String)

    Message Body.

  • (String)

    Sender ID.

  • (String)

    Recipient.

  • (String)

    Time Schedualed.

  • (String)

    Status.

  • (String)

    Code status of the response if 0 its a success else its an error.



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/unifonic_sms.rb', line 334

def schedualed_messages (message_id = nil)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/GetScheduled")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"
  body += "&MessageID=#{message_id}" unless message_id.nil?

  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 
    return { message_id: response_body["MessageID"],
             message_body: response_body["MessageBody"], 
             sender_id: response_body["SenderID"],
             recipient: response_body["Recipient"],
             time_schedualed: response_body["TimeScheduled"],
             status: response_body["Status"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.send_bulk(phones, message, time_schedualed = nil) ⇒ Array<Hash>, String

Send Bulk SMS messages Max is 1,000 recipents per request.

Parameters:

  • phones (String)

    The Recipients phone numbers seperated by comma.

  • message (String)

    Body of the message to be sent.

  • time_schedualed (String, nil) (defaults to: nil)

    Schedualed time to send the message.

Returns:

  • (Array<Hash>)

    Messages.

  • (String)

    Status of message possible values are “Queued” , “Sent”, “Failed” and “Rejected”.

  • (String)

    Number of unit in a message.

  • (String)

    Price of a message total units.

  • (String)

    The currency code used eith cost.

  • (String)

    Current balance of your account.

  • (String)

    The mobile numbers the message was sent to.

  • (String)

    Date a message was created in, in the following format “yyyy-mm-dd hh:mm:ss”.

  • (String)

    Code status of the response if 0 its a success else its an error.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
# File 'lib/unifonic_sms.rb', line 150

def send_bulk (phones, message, time_schedualed = nil)
  # Adjust Parameters
  recipients = []
  phone_numbers = phones.split(",")
  phone_numbers.each do |phone|
    recipients.push(Normalizer.normalize_number(phone))
  end
  message = Normalizer.normalize_message(message)

  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/SendBulk")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}&Recipient="

  # Add Recipents to body attributes
  recipients.each_with_index do |phone, index|
    body += phone
    if index != (recipients.count - 1)
      body += ","
    end
  end

  body += "&Body=#{message}"
  body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
  body += '&TimeScheduled=#{time_schedualed}' unless time_schedualed.nil?
  
  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["data"].nil? 
    return { messages: response_body["data"]["Messages"], 
              status: response_body["data"]["Status"], 
              number_of_units: response_body["data"]["NumberOfUnits"],
              cost: response_body["data"]["Cost"],
              currency_code: response_body["data"]["CurrencyCode"],
              balance: response_body["data"]["Balance"],
              recipient: response_body["data"]["Recipient"],
              time_created: response_body["data"]["TimeCreated"],
              code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.send_message(phone, message, time_schedualed = nil) ⇒ String

Send SMS message.

Parameters:

  • phone (String)

    The Recipient phone number.

  • message (String)

    Body of the message to be sent.

  • time_schedualed (String, nil) (defaults to: nil)

    Schedualed time to send the message.

Returns:

  • (String)

    Message ID.

  • (String)

    Status of message possible values are “Queued” , “Sent”, “Failed” and “Rejected”.

  • (String)

    Number of unit in a message.

  • (String)

    Price of a message total units.

  • (String)

    The currency code used eith cost.

  • (String)

    Current balance of your account.

  • (String)

    The mobile number the message was sent to.

  • (String)

    Date a message was created in, in the following format “yyyy-mm-dd hh:mm:ss”.

  • (String)

    Code status of the response if 0 its a success else its an error.



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
# File 'lib/unifonic_sms.rb', line 97

def send_message (phone, message, time_schedualed = nil)
  # Adjust Parameters
  recipient = Normalizer.normalize_number(phone)
  message = Normalizer.normalize_message(message)

  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/Send")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}&Recipient=#{recipient}&Body=#{message}"
  body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
  body += '&Priority=High'
  body += '&TimeScheduled=#{time_schedualed}' unless time_schedualed.nil?
  
  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200 && !response_body["data"].nil? 
    return { message_id: response_body["data"]["MessageID"], 
             status: response_body["data"]["Status"], 
             number_of_units: response_body["data"]["NumberOfUnits"],
             cost: response_body["data"]["Cost"],
             currency_code: response_body["data"]["CurrencyCode"],
             balance: response_body["data"]["Balance"],
             recipient: response_body["data"]["Recipient"],
             time_created: response_body["data"]["TimeCreated"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end

.sender_phoneString

Get the phone number of the sender from the configurations.

Returns:

  • (String)

    Phone Number.



39
40
41
# File 'lib/unifonic_sms.rb', line 39

def sender_phone
  configuration.sender_phone
end

.stop_schedualed_messages(message_id) ⇒ String

Get a summarized report for scheduled sent messages.

Parameters:

  • message_id (String)

    The ID of the message.

Returns:

  • (String)

    Success if stopped will return “true” else will return empty “”.

  • (String)

    Code status of the response if 0 its a success else its an error.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/unifonic_sms.rb', line 369

def stop_schedualed_messages (message_id)
  # Initialize Request
  http = Net::HTTP.new('api.unifonic.com', 80)
  path = base_path("Messages/StopScheduled")
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

  # Add Body Parameters to request
  body = "AppSid=#{api_key}"
  body += "&MessageID=#{message_id}"

  # Send Call Request
  response = http.post(path, body, headers)
  response_body = JSON.parse(response.body)

  if response.code.to_i == 200
    return { success: response_body["Success"],
             code: 0 }
  else
    result = ErrorCode.get_error_code(response_body["errorCode"]) 

    return result   
  end   
end