unifonic_sms 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/unifonic_sms.rb +525 -0
- data/lib/unifonic_sms/configuration.rb +11 -0
- data/lib/unifonic_sms/error_code.rb +96 -0
- data/lib/unifonic_sms/normalizer.rb +29 -0
- data/lib/unifonic_sms/version.rb +4 -0
- metadata +111 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 807e20ed74f946b8da4862674c01fbc4188f5053
|
|
4
|
+
data.tar.gz: 04623444a12689343843d03f5007507c961a2fb7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b3d769b5a51d5200a4a4efdcffd1bda457ab659a58f3a4f8319fe234b39f4510c08cb796de45ff52fe2d22d9a20bd674404659e6e71d85f6ae0fd8c41d327ebf
|
|
7
|
+
data.tar.gz: f90597cae15310bb2827963ad42f37347b82170aba1939d2ce118a61fb28593b1c71bdb757eb318ec8b830c1c5311c5d06defd927da39f1d57c1309955a8c654
|
data/lib/unifonic_sms.rb
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require "unifonic_sms/version"
|
|
3
|
+
require 'unifonic_sms/error_code'
|
|
4
|
+
require 'unifonic_sms/normalizer'
|
|
5
|
+
require 'unifonic_sms/configuration'
|
|
6
|
+
|
|
7
|
+
module UnifonicSms
|
|
8
|
+
class << self
|
|
9
|
+
|
|
10
|
+
attr_accessor :configuration
|
|
11
|
+
|
|
12
|
+
# Get the Configurations or Reset.
|
|
13
|
+
#
|
|
14
|
+
# @return [QiwaSms::Configuration] Configuration.
|
|
15
|
+
def configuration
|
|
16
|
+
@configuration ||= Configuration.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Reset Configuration to nil.
|
|
20
|
+
def reset
|
|
21
|
+
@configuration = Configuration.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Holds the configurtion block.
|
|
25
|
+
def configure
|
|
26
|
+
yield(configuration)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Get the api key from the configurations.
|
|
30
|
+
#
|
|
31
|
+
# @return [String] Api Key.
|
|
32
|
+
def api_key
|
|
33
|
+
configuration.api_key
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Get the phone number of the sender from the configurations.
|
|
37
|
+
#
|
|
38
|
+
# @return [String] Phone Number.
|
|
39
|
+
def sender_phone
|
|
40
|
+
configuration.sender_phone
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Get the base url for api call.
|
|
44
|
+
#
|
|
45
|
+
# @param method_url [String] the api method url.
|
|
46
|
+
# @return [String] Url for Api Call.
|
|
47
|
+
def base_path (method_url)
|
|
48
|
+
"/rest/#{method_url}"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Get Account's Current Balance.
|
|
52
|
+
#
|
|
53
|
+
# @return [String] balance of the Account.
|
|
54
|
+
# @return [String] shared balance with sub accounts.
|
|
55
|
+
# @return [String] currency code used with cost.
|
|
56
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
57
|
+
def balance
|
|
58
|
+
# Initialize Request
|
|
59
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
60
|
+
path = base_path("Account/GetBalance")
|
|
61
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
62
|
+
|
|
63
|
+
# Add Body Parameters to request
|
|
64
|
+
body = "AppSid=#{api_key}"
|
|
65
|
+
|
|
66
|
+
# Send Call Request
|
|
67
|
+
response = http.post(path, body, headers)
|
|
68
|
+
response_body = JSON.parse(response.body)
|
|
69
|
+
|
|
70
|
+
if response.code.to_i == 200 && !response_body["data"].nil?
|
|
71
|
+
return { balance: response_body["data"]["Balance"],
|
|
72
|
+
currency_code: response_body["data"]["CurrencyCode"],
|
|
73
|
+
shared_balance: response_body["data"]["SharedBalance"],
|
|
74
|
+
code: 0 }
|
|
75
|
+
else
|
|
76
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
77
|
+
|
|
78
|
+
return result
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Send SMS message.
|
|
83
|
+
#
|
|
84
|
+
# @param [String] phone The Recipient phone number.
|
|
85
|
+
# @param [String] message Body of the message to be sent.
|
|
86
|
+
# @param [String, nil] time_schedualed Schedualed time to send the message.
|
|
87
|
+
#
|
|
88
|
+
# @return [String] Message ID.
|
|
89
|
+
# @return [String] Status of message possible values are "Queued" , "Sent", "Failed" and "Rejected".
|
|
90
|
+
# @return [String] Number of unit in a message.
|
|
91
|
+
# @return [String] Price of a message total units.
|
|
92
|
+
# @return [String] The currency code used eith cost.
|
|
93
|
+
# @return [String] Current balance of your account.
|
|
94
|
+
# @return [String] The mobile number the message was sent to.
|
|
95
|
+
# @return [String] Date a message was created in, in the following format "yyyy-mm-dd hh:mm:ss".
|
|
96
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
97
|
+
def send_message (phone, message, time_schedualed = nil)
|
|
98
|
+
# Adjust Parameters
|
|
99
|
+
recipient = Normalizer.normalize_number(phone)
|
|
100
|
+
message = Normalizer.normalize_message(message)
|
|
101
|
+
|
|
102
|
+
# Initialize Request
|
|
103
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
104
|
+
path = base_path("Messages/Send")
|
|
105
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
106
|
+
|
|
107
|
+
# Add Body Parameters to request
|
|
108
|
+
body = "AppSid=#{api_key}&Recipient=#{recipient}&Body=#{message}"
|
|
109
|
+
body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
|
|
110
|
+
body += '&Priority=High'
|
|
111
|
+
body += '&TimeScheduled=#{time_schedualed}' unless time_schedualed.nil?
|
|
112
|
+
|
|
113
|
+
# Send Call Request
|
|
114
|
+
response = http.post(path, body, headers)
|
|
115
|
+
response_body = JSON.parse(response.body)
|
|
116
|
+
|
|
117
|
+
if response.code.to_i == 200 && !response_body["data"].nil?
|
|
118
|
+
return { message_id: response_body["data"]["MessageID"],
|
|
119
|
+
status: response_body["data"]["Status"],
|
|
120
|
+
number_of_units: response_body["data"]["NumberOfUnits"],
|
|
121
|
+
cost: response_body["data"]["Cost"],
|
|
122
|
+
currency_code: response_body["data"]["CurrencyCode"],
|
|
123
|
+
balance: response_body["data"]["Balance"],
|
|
124
|
+
recipient: response_body["data"]["Recipient"],
|
|
125
|
+
time_created: response_body["data"]["TimeCreated"],
|
|
126
|
+
code: 0 }
|
|
127
|
+
else
|
|
128
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
129
|
+
|
|
130
|
+
return result
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Send Bulk SMS messages
|
|
135
|
+
# Max is 1,000 recipents per request.
|
|
136
|
+
#
|
|
137
|
+
# @param [String] phones The Recipients phone numbers seperated by comma.
|
|
138
|
+
# @param [String] message Body of the message to be sent.
|
|
139
|
+
# @param [String, nil] time_schedualed Schedualed time to send the message.
|
|
140
|
+
#
|
|
141
|
+
# @return [Array<Hash>] Messages.
|
|
142
|
+
# @return [String] Status of message possible values are "Queued" , "Sent", "Failed" and "Rejected".
|
|
143
|
+
# @return [String] Number of unit in a message.
|
|
144
|
+
# @return [String] Price of a message total units.
|
|
145
|
+
# @return [String] The currency code used eith cost.
|
|
146
|
+
# @return [String] Current balance of your account.
|
|
147
|
+
# @return [String] The mobile numbers the message was sent to.
|
|
148
|
+
# @return [String] Date a message was created in, in the following format "yyyy-mm-dd hh:mm:ss".
|
|
149
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
150
|
+
def send_bulk (phones, message, time_schedualed = nil)
|
|
151
|
+
# Adjust Parameters
|
|
152
|
+
recipients = []
|
|
153
|
+
phone_numbers = phones.split(",")
|
|
154
|
+
phone_numbers.each do |phone|
|
|
155
|
+
recipients.push(Normalizer.normalize_number(phone))
|
|
156
|
+
end
|
|
157
|
+
message = Normalizer.normalize_message(message)
|
|
158
|
+
|
|
159
|
+
# Initialize Request
|
|
160
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
161
|
+
path = base_path("Messages/SendBulk")
|
|
162
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
163
|
+
|
|
164
|
+
# Add Body Parameters to request
|
|
165
|
+
body = "AppSid=#{api_key}&Recipient="
|
|
166
|
+
|
|
167
|
+
# Add Recipents to body attributes
|
|
168
|
+
recipients.each_with_index do |phone, index|
|
|
169
|
+
body += phone
|
|
170
|
+
if index != (recipients.count - 1)
|
|
171
|
+
body += ","
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
body += "&Body=#{message}"
|
|
176
|
+
body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
|
|
177
|
+
body += '&TimeScheduled=#{time_schedualed}' unless time_schedualed.nil?
|
|
178
|
+
|
|
179
|
+
# Send Call Request
|
|
180
|
+
response = http.post(path, body, headers)
|
|
181
|
+
response_body = JSON.parse(response.body)
|
|
182
|
+
|
|
183
|
+
if response.code.to_i == 200 && !response_body["data"].nil?
|
|
184
|
+
return { messages: response_body["data"]["Messages"],
|
|
185
|
+
status: response_body["data"]["Status"],
|
|
186
|
+
number_of_units: response_body["data"]["NumberOfUnits"],
|
|
187
|
+
cost: response_body["data"]["Cost"],
|
|
188
|
+
currency_code: response_body["data"]["CurrencyCode"],
|
|
189
|
+
balance: response_body["data"]["Balance"],
|
|
190
|
+
recipient: response_body["data"]["Recipient"],
|
|
191
|
+
time_created: response_body["data"]["TimeCreated"],
|
|
192
|
+
code: 0 }
|
|
193
|
+
else
|
|
194
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
195
|
+
|
|
196
|
+
return result
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Get Message Status.
|
|
201
|
+
#
|
|
202
|
+
# @param [String] message_id The ID of the message.
|
|
203
|
+
#
|
|
204
|
+
# @return [String] Status of message possible values are "Queued" , "Sent", "Failed" and "Rejected".
|
|
205
|
+
# @return [String] DLR Message delivery status returned by networks, the possible values are "Delivered" or "Undeliverable", and are available for advanced plans.
|
|
206
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
207
|
+
def message_status (message_id)
|
|
208
|
+
# Initialize Request
|
|
209
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
210
|
+
path = base_path("Messages/GetMessageIDStatus")
|
|
211
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
212
|
+
|
|
213
|
+
# Add Body Parameters to request
|
|
214
|
+
body = "AppSid=#{api_key}&MessageID=#{message_id}"
|
|
215
|
+
|
|
216
|
+
# Send Call Request
|
|
217
|
+
response = http.post(path, body, headers)
|
|
218
|
+
response_body = JSON.parse(response.body)
|
|
219
|
+
|
|
220
|
+
if response.code.to_i == 200 && !response_body["Status"].nil?
|
|
221
|
+
return { status: response_body["Status"], code: 0 }
|
|
222
|
+
else
|
|
223
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
224
|
+
|
|
225
|
+
return result
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# To get a summarized report for sent messages within a specific timer interval
|
|
230
|
+
#
|
|
231
|
+
# @param [String, nil] date_from The start date for the report time interval, date format should be yyyy-mm-dd.
|
|
232
|
+
# @param [String, nil] date_to The end date for the report time interval, date format should be yyyy-mm-dd.
|
|
233
|
+
# @param [String, nil] status Filter messages report according to a specific message status, "Sent", "Queued", "Rejected" or "Failed".
|
|
234
|
+
# @param [String, nil] dlr Message delivery status returned by networks, the possible values are "Delivered" or "Undeliverable", and are available for advanced plans.
|
|
235
|
+
# @param [String, nil] country Filter messages report according to a specific destination country.
|
|
236
|
+
#
|
|
237
|
+
# @return [String] Number of messages.
|
|
238
|
+
# @return [String] Number of unit in a message.
|
|
239
|
+
# @return [String] Price of a message total units.
|
|
240
|
+
# @return [String] The currency code used eith cost.
|
|
241
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
242
|
+
def messages_report (date_from = nil, date_to = nil, status = nil, dlr = nil, country = nil)
|
|
243
|
+
# Initialize Request
|
|
244
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
245
|
+
path = base_path("Messages/GetMessagesReport")
|
|
246
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
247
|
+
|
|
248
|
+
# Add Body Parameters to request
|
|
249
|
+
body = "AppSid=#{api_key}"
|
|
250
|
+
body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
|
|
251
|
+
body += "&DateFrom=#{date_from}" unless date_from.nil?
|
|
252
|
+
body += "&DateTo=#{date_to}" unless date_to.nil?
|
|
253
|
+
body += "&Status=#{status}" unless status.nil?
|
|
254
|
+
body += "&DLR=#{dlr}" unless dlr.nil?
|
|
255
|
+
body += "&Country=#{country}" unless country.nil?
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
# Send Call Request
|
|
259
|
+
response = http.post(path, body, headers)
|
|
260
|
+
response_body = JSON.parse(response.body)
|
|
261
|
+
|
|
262
|
+
if response.code.to_i == 200 && !response_body["TotalTextMessages"].nil?
|
|
263
|
+
return { total_text_messages: response_body["TotalTextMessages"],
|
|
264
|
+
number_of_units: response_body["NumberOfUnits"],
|
|
265
|
+
cost: response_body["Cost"],
|
|
266
|
+
currency_code: response_body["CurrencyCode"],
|
|
267
|
+
code: 0 }
|
|
268
|
+
else
|
|
269
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
270
|
+
|
|
271
|
+
return result
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Get latest 10,000 messages details if no message id was provided.
|
|
276
|
+
#
|
|
277
|
+
# @param [String, nil] message_id The ID of the message.
|
|
278
|
+
# @param [String, nil] date_from The start date for the report time interval, date format should be yyyy-mm-dd.
|
|
279
|
+
# @param [String, nil] date_to The end date for the report time interval, date format should be yyyy-mm-dd.
|
|
280
|
+
# @param [String, nil] status Filter messages report according to a specific message status, "Sent", "Queued", "Rejected" or "Failed".
|
|
281
|
+
# @param [String, nil] dlr Message delivery status returned by networks, the possible values are "Delivered" or "Undeliverable", and are available for advanced plans.
|
|
282
|
+
# @param [String, nil] country Filter messages report according to a specific destination country.
|
|
283
|
+
# @param [String, nil] limit Number of messages to return in the report, where the limit maximum is 10,000 and messages are sorted by sending date.
|
|
284
|
+
#
|
|
285
|
+
# @return [String] Number of messages.
|
|
286
|
+
# @return [String] Page number.
|
|
287
|
+
# @return [Array<Hash>] Messages.
|
|
288
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
289
|
+
def messages_details (message_id = nil, date_from = nil, date_to = nil, status = nil, dlr = nil, country = nil, limit = nil)
|
|
290
|
+
# Initialize Request
|
|
291
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
292
|
+
path = base_path("Messages/GetMessagesDetails")
|
|
293
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
294
|
+
|
|
295
|
+
# Add Body Parameters to request
|
|
296
|
+
body = "AppSid=#{api_key}"
|
|
297
|
+
body += "&MessageID=#{message_id}" unless message_id.nil?
|
|
298
|
+
body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
|
|
299
|
+
body += "&DateFrom=#{date_from}" unless date_from.nil?
|
|
300
|
+
body += "&DateTo=#{date_to}" unless date_to.nil?
|
|
301
|
+
body += "&Status=#{status}" unless status.nil?
|
|
302
|
+
body += "&DLR=#{dlr}" unless dlr.nil?
|
|
303
|
+
body += "&Country=#{country}" unless country.nil?
|
|
304
|
+
body += "&Limit=#{limit}" unless limit.nil?
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
# Send Call Request
|
|
308
|
+
response = http.post(path, body, headers)
|
|
309
|
+
response_body = JSON.parse(response.body)
|
|
310
|
+
|
|
311
|
+
if response.code.to_i == 200 && !response_body["data"].nil?
|
|
312
|
+
return { total_text_messages: response_body["data"]["TotalTextMessages"],
|
|
313
|
+
page: response_body["data"]["Page"],
|
|
314
|
+
messages: response_body["data"]["messages"],
|
|
315
|
+
code: 0 }
|
|
316
|
+
else
|
|
317
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
318
|
+
|
|
319
|
+
return result
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Get a summarized report for scheduled sent messages.
|
|
324
|
+
#
|
|
325
|
+
# @param [String, nil] message_id The ID of the message.
|
|
326
|
+
#
|
|
327
|
+
# @return [String] Message ID.
|
|
328
|
+
# @return [String] Message Body.
|
|
329
|
+
# @return [String] Sender ID.
|
|
330
|
+
# @return [String] Recipient.
|
|
331
|
+
# @return [String] Time Schedualed.
|
|
332
|
+
# @return [String] Status.
|
|
333
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
334
|
+
def schedualed_messages (message_id = nil)
|
|
335
|
+
# Initialize Request
|
|
336
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
337
|
+
path = base_path("Messages/GetScheduled")
|
|
338
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
339
|
+
|
|
340
|
+
# Add Body Parameters to request
|
|
341
|
+
body = "AppSid=#{api_key}"
|
|
342
|
+
body += "&MessageID=#{message_id}" unless message_id.nil?
|
|
343
|
+
|
|
344
|
+
# Send Call Request
|
|
345
|
+
response = http.post(path, body, headers)
|
|
346
|
+
response_body = JSON.parse(response.body)
|
|
347
|
+
|
|
348
|
+
if response.code.to_i == 200
|
|
349
|
+
return { message_id: response_body["MessageID"],
|
|
350
|
+
message_body: response_body["MessageBody"],
|
|
351
|
+
sender_id: response_body["SenderID"],
|
|
352
|
+
recipient: response_body["Recipient"],
|
|
353
|
+
time_schedualed: response_body["TimeScheduled"],
|
|
354
|
+
status: response_body["Status"],
|
|
355
|
+
code: 0 }
|
|
356
|
+
else
|
|
357
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
358
|
+
|
|
359
|
+
return result
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# Get a summarized report for scheduled sent messages.
|
|
364
|
+
#
|
|
365
|
+
# @param [String] message_id The ID of the message.
|
|
366
|
+
#
|
|
367
|
+
# @return [String] Success if stopped will return "true" else will return empty "".
|
|
368
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
369
|
+
def stop_schedualed_messages (message_id)
|
|
370
|
+
# Initialize Request
|
|
371
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
372
|
+
path = base_path("Messages/StopScheduled")
|
|
373
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
374
|
+
|
|
375
|
+
# Add Body Parameters to request
|
|
376
|
+
body = "AppSid=#{api_key}"
|
|
377
|
+
body += "&MessageID=#{message_id}"
|
|
378
|
+
|
|
379
|
+
# Send Call Request
|
|
380
|
+
response = http.post(path, body, headers)
|
|
381
|
+
response_body = JSON.parse(response.body)
|
|
382
|
+
|
|
383
|
+
if response.code.to_i == 200
|
|
384
|
+
return { success: response_body["Success"],
|
|
385
|
+
code: 0 }
|
|
386
|
+
else
|
|
387
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
388
|
+
|
|
389
|
+
return result
|
|
390
|
+
end
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
# The Keyword method enables you to manage your numbers,
|
|
394
|
+
# create auto replies to incoming messages or set a webhook directly from your API,
|
|
395
|
+
# Check Keywords Management to view and edit your keywords.
|
|
396
|
+
#
|
|
397
|
+
# @see http://software.unifonic.com/en/inbox/keywords?channel=SMS
|
|
398
|
+
#
|
|
399
|
+
# @param [String] number to manage.
|
|
400
|
+
# @param [String] keyword to use.
|
|
401
|
+
# @param [String] rule for the keyword.
|
|
402
|
+
# @param [String, nil] message Set an auto reply to send back to the user (Ex: You have been successfully registered ).
|
|
403
|
+
# @param [String, nil] webhook_url Defines the source that you want to make the callback to for example “www.google.com”.
|
|
404
|
+
# @param [String, nil] message_parameter Set the parameters that the source takes for example https://www.google.jo/search?q=hello&oq=hello then your parameters that you have to set are q and oq.
|
|
405
|
+
# @param [String, nil] recipient_parameter Set the parameters that the source takes for example https://www.google.jo/search?q=hello&oq=hello then your parameters that you have to set are q and oq.
|
|
406
|
+
# @param [String, nil] request_type 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].
|
|
407
|
+
# @param [String, nil] resource_number your inbound number for example 70001.
|
|
408
|
+
#
|
|
409
|
+
# @return [String] Success True, to indicate successfully create of a new keyword.
|
|
410
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
411
|
+
def keyword (number, keyword, rule, message = nil, webhook_url = nil, message_parameter = nil, recipient_parameter = nil, request_type = nil, resource_number = nil)
|
|
412
|
+
# Initialize Request
|
|
413
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
414
|
+
path = base_path("Messages/Keyword")
|
|
415
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
416
|
+
|
|
417
|
+
# Add Body Parameters to request
|
|
418
|
+
body = "AppSid=#{api_key}"
|
|
419
|
+
body += "&Number=#{number}"
|
|
420
|
+
body += "&Keyword=#{keyword}"
|
|
421
|
+
body += "&Rule=#{rule}"
|
|
422
|
+
body += "&SenderID=#{sender_phone}" unless sender_phone.nil?
|
|
423
|
+
body += "&Message=#{message}" unless message.nil?
|
|
424
|
+
body += "&WebhookURL=#{webhook_url}" unless webhook_url.nil?
|
|
425
|
+
body += "&MessageParameter=#{message_parameter}" unless message_parameter.nil?
|
|
426
|
+
body += "&RecipientParameter=#{recipient_parameter}" unless recipient_parameter.nil?
|
|
427
|
+
body += "&RequestType =#{request_type}" unless request_type.nil?
|
|
428
|
+
body += "&ResourceNumber =#{resource_number}" unless resource_number.nil?
|
|
429
|
+
|
|
430
|
+
# Send Call Request
|
|
431
|
+
response = http.post(path, body, headers)
|
|
432
|
+
response_body = JSON.parse(response.body)
|
|
433
|
+
|
|
434
|
+
if response.code.to_i == 200
|
|
435
|
+
return { success: response_body["Success"],
|
|
436
|
+
code: 0 }
|
|
437
|
+
else
|
|
438
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
439
|
+
|
|
440
|
+
return result
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
# The Keyword method enables you to manage your numbers,
|
|
445
|
+
# create auto replies to incoming messages or set a webhook directly from your API,
|
|
446
|
+
# Check Keywords Management to view and edit your keywords.
|
|
447
|
+
#
|
|
448
|
+
# @see http://software.unifonic.com/en/inbox/keywords?channel=SMS
|
|
449
|
+
#
|
|
450
|
+
# @param [String] number to manage.
|
|
451
|
+
# @param [String, nil] keyword to use.
|
|
452
|
+
# @param [String, nil] date_from The start date for the report time interval, date format should be yyyy-mm-dd.
|
|
453
|
+
# @param [String, nil] date_to The end date for the report time interval, date format should be yyyy-mm-dd.
|
|
454
|
+
#
|
|
455
|
+
# @return [String] Number of messages.
|
|
456
|
+
# @return [String] message from Recipient number.
|
|
457
|
+
# @return [Array<Hash>] received message.
|
|
458
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
459
|
+
def inbox (number, keyword = nil, date_from = nil, date_to = nil)
|
|
460
|
+
# Initialize Request
|
|
461
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
462
|
+
path = base_path("Messages/Inbox")
|
|
463
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
464
|
+
|
|
465
|
+
# Add Body Parameters to request
|
|
466
|
+
body = "AppSid=#{api_key}"
|
|
467
|
+
body += "&Number=#{number}"
|
|
468
|
+
body += "&Keyword=#{keyword}" unless keyword.nil?
|
|
469
|
+
body += "&FromDate=#{date_from}" unless date_from.nil?
|
|
470
|
+
body += "&ToDate=#{date_to}" unless date_to.nil?
|
|
471
|
+
|
|
472
|
+
# Send Call Request
|
|
473
|
+
response = http.post(path, body, headers)
|
|
474
|
+
response_body = JSON.parse(response.body)
|
|
475
|
+
|
|
476
|
+
if response.code.to_i == 200 && !response_body["data"].nil?
|
|
477
|
+
return { number_of_messages: response_body["data"]["NumberOfMessages"],
|
|
478
|
+
message_from: response_body["data"]["MessageFrom"],
|
|
479
|
+
message: response_body["data"]["Message"],
|
|
480
|
+
date_recieved: response_body["data"]["DateReceived"],
|
|
481
|
+
code: 0 }
|
|
482
|
+
else
|
|
483
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
484
|
+
|
|
485
|
+
return result
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
# The Keyword method enables you to manage your numbers,
|
|
490
|
+
# create auto replies to incoming messages or set a webhook directly from your API,
|
|
491
|
+
# Check Keywords Management to view and edit your keywords.
|
|
492
|
+
#
|
|
493
|
+
# @see http://software.unifonic.com/en/inbox/keywords?channel=SMS
|
|
494
|
+
#
|
|
495
|
+
# @param [String, nil] country_code The Country code to check its prices.
|
|
496
|
+
#
|
|
497
|
+
# @return [Hash] Country Data.
|
|
498
|
+
# @return [String] Country name.
|
|
499
|
+
# @return [String] Code status of the response if 0 its a success else its an error.
|
|
500
|
+
def pricing (country_code = nil)
|
|
501
|
+
# Initialize Request
|
|
502
|
+
http = Net::HTTP.new('api.unifonic.com', 80)
|
|
503
|
+
path = base_path("Messages/Pricing")
|
|
504
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
505
|
+
|
|
506
|
+
# Add Body Parameters to request
|
|
507
|
+
body = "AppSid=#{api_key}"
|
|
508
|
+
body += "&CountryCode=#{country_code}" unless country_code.nil?
|
|
509
|
+
|
|
510
|
+
# Send Call Request
|
|
511
|
+
response = http.post(path, body, headers)
|
|
512
|
+
response_body = JSON.parse(response.body)
|
|
513
|
+
|
|
514
|
+
if response.code.to_i == 200 && !response_body["data"].nil?
|
|
515
|
+
return { country: response_body["data"]["CountryCode"],
|
|
516
|
+
country_name: response_body["data"]["CountryName"],
|
|
517
|
+
code: 0 }
|
|
518
|
+
else
|
|
519
|
+
result = ErrorCode.get_error_code(response_body["errorCode"])
|
|
520
|
+
|
|
521
|
+
return result
|
|
522
|
+
end
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
module UnifonicSms
|
|
2
|
+
module ErrorCode
|
|
3
|
+
# Translates the error code returned by the Api call
|
|
4
|
+
# @see https://unifonic.docs.apiary.io/#reference/errors-list for references
|
|
5
|
+
#
|
|
6
|
+
# @param [String] error_code the error code recieved from response.
|
|
7
|
+
def self.get_error_code(error_code)
|
|
8
|
+
case error_code
|
|
9
|
+
when "ER-01"
|
|
10
|
+
{error: "Invalid AppSid", code: 1}
|
|
11
|
+
when "ER-02"
|
|
12
|
+
{error: "Missing parameter", code: 2}
|
|
13
|
+
when "ER-03"
|
|
14
|
+
{error: "Sender ID already exists", code: 3}
|
|
15
|
+
when "ER-04"
|
|
16
|
+
{error: "Wrong sender ID format, sender ID should not exceed 11 characters or 16 numbers, only English letters allowed with no special characters or spaces", code: 4}
|
|
17
|
+
when "ER-05"
|
|
18
|
+
{error: "Sender ID is blocked and can't be used", code: 5}
|
|
19
|
+
when "ER-06"
|
|
20
|
+
{error: "Sender ID doesn't exist", code: 6}
|
|
21
|
+
when "ER-07"
|
|
22
|
+
{error: "Default sender ID can't be deleted", code: 7}
|
|
23
|
+
when "ER-08"
|
|
24
|
+
{error: "Sender ID is not approved", code: 8}
|
|
25
|
+
when "ER-09"
|
|
26
|
+
{error: "No sufficient balance", code: 9}
|
|
27
|
+
when "ER-10"
|
|
28
|
+
{error: "Wrong number format , mobile numbers must be in international format without 00 or + Example: (4452023498)", code: 10}
|
|
29
|
+
when "ER-11"
|
|
30
|
+
{error: "Unsupported destination", code: 11}
|
|
31
|
+
when "ER-12"
|
|
32
|
+
{error: "Message body exceeded limit", code: 12}
|
|
33
|
+
when "ER-13"
|
|
34
|
+
{error: "Service not found", code: 13}
|
|
35
|
+
when "ER-14"
|
|
36
|
+
{error: "Sender ID is blocked on the required destination", code: 14}
|
|
37
|
+
when "ER-15"
|
|
38
|
+
{error: "User is Not active", code: 15}
|
|
39
|
+
when "ER-16"
|
|
40
|
+
{error: "Exceed throughput limit", code: 16}
|
|
41
|
+
when "ER-17"
|
|
42
|
+
{error: "Inappropriate content in message body", code: 17}
|
|
43
|
+
when "ER-18"
|
|
44
|
+
{error: "Invalid Message ID", code: 18}
|
|
45
|
+
when "ER-19"
|
|
46
|
+
{error: "Wrong date format, date format should be yyyy-mm-dd", code: 19}
|
|
47
|
+
when "ER-20"
|
|
48
|
+
{error: "Page limit Exceeds (10000)", code: 20}
|
|
49
|
+
when "ER-21"
|
|
50
|
+
{error: "Method not found.", code: 21}
|
|
51
|
+
when "ER-22"
|
|
52
|
+
{error: "Language not supported", code: 22}
|
|
53
|
+
when "ER-23"
|
|
54
|
+
{error: "You have exceeded your Sender ID's requests limit, delete one sender ID to request a new one", code: 23}
|
|
55
|
+
when "ER-24"
|
|
56
|
+
{error: "Wrong Status data , message status should be one of the following statuses \"Queued\" , \"Sent\" , \"Failed\" or \"Rejected\"", code: 24}
|
|
57
|
+
when "ER-25"
|
|
58
|
+
{error: "This request is not included in your account plan", code: 25}
|
|
59
|
+
when "ER-26"
|
|
60
|
+
{error: "Invalid Call ID", code: 26}
|
|
61
|
+
when "ER-27"
|
|
62
|
+
{error: "Wrong Status Data", code: 27}
|
|
63
|
+
when "ER-28"
|
|
64
|
+
{error: "Wrong Email Format", code: 28}
|
|
65
|
+
when "ER-29"
|
|
66
|
+
{error: "Invalid Email ID", code: 29}
|
|
67
|
+
when "ER-30"
|
|
68
|
+
{error: "Invalid Security Type", code: 30}
|
|
69
|
+
when "ER-31"
|
|
70
|
+
{error: "Wrong Passcode", code: 31}
|
|
71
|
+
when "ER-32"
|
|
72
|
+
{error: "Passcode expired", code: 32}
|
|
73
|
+
when "ER-33"
|
|
74
|
+
{error: "Wrong channel type, Channel value should be TextMessage, Call or Both", code: 33}
|
|
75
|
+
when "ER-34"
|
|
76
|
+
{error: "Wrong time to live value TTL, TTL should be between 1 and 60 minutes, and should not exceed the Expiry time", code: 34}
|
|
77
|
+
when "ER-35"
|
|
78
|
+
{error: "MessageID already sent", code: 35}
|
|
79
|
+
when "ER-36"
|
|
80
|
+
{error: "Wrong voice type, voice value should be Male or Female", code: 36}
|
|
81
|
+
when "ER-37"
|
|
82
|
+
{error: "", code: 37}
|
|
83
|
+
when "ER-38"
|
|
84
|
+
{error: "Invalid number; number is not available or had expired for the submitted AppSid", code: 38}
|
|
85
|
+
when "ER-39"
|
|
86
|
+
{error: "Invalid Rule ; rule should be: Is, StartsWith, Contains, Any. Only \"Is\" is available for a shared number", code: 39}
|
|
87
|
+
when "ER-40"
|
|
88
|
+
{error: "Keyword not defined", code: 40}
|
|
89
|
+
when "ER-41"
|
|
90
|
+
{error: "Invalid Country code", code: 41}
|
|
91
|
+
else
|
|
92
|
+
{error: "Unknown error code", code: error_code}
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module UnifonicSms
|
|
2
|
+
module Normalizer
|
|
3
|
+
# Normalize phone numbers to be used by Unifonic
|
|
4
|
+
# must remove any '+' or '0' at the start.
|
|
5
|
+
#
|
|
6
|
+
# @param [String] number the phone number to be normalized.
|
|
7
|
+
#
|
|
8
|
+
# @example Normalize a phone number
|
|
9
|
+
# "Normalizer.normalize_number('+01234667876')" #=> "1234667876"
|
|
10
|
+
def self.normalize_number(number)
|
|
11
|
+
n = number.dup
|
|
12
|
+
while n.start_with?('+') || n.start_with?('0')
|
|
13
|
+
n.slice!(0)
|
|
14
|
+
end
|
|
15
|
+
return n
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Normalize message to be used by Unifonic
|
|
19
|
+
# must be in UTF-8 Encoding.
|
|
20
|
+
#
|
|
21
|
+
# @param [String] message the message that will be sent.
|
|
22
|
+
#
|
|
23
|
+
# @example Normalize an SMS message
|
|
24
|
+
# "Normalizer.normalize_message('Test Message').encoding" #=> "UTF_8"
|
|
25
|
+
def self.normalize_message(message)
|
|
26
|
+
message.encode(Encoding::UTF_8)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: unifonic_sms
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Assen Deghady
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-01-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: webmock
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.1'
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: 3.1.1
|
|
65
|
+
type: :development
|
|
66
|
+
prerelease: false
|
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - "~>"
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '3.1'
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: 3.1.1
|
|
75
|
+
description:
|
|
76
|
+
email:
|
|
77
|
+
- assem.deghady@gmail.com
|
|
78
|
+
executables: []
|
|
79
|
+
extensions: []
|
|
80
|
+
extra_rdoc_files: []
|
|
81
|
+
files:
|
|
82
|
+
- lib/unifonic_sms.rb
|
|
83
|
+
- lib/unifonic_sms/configuration.rb
|
|
84
|
+
- lib/unifonic_sms/error_code.rb
|
|
85
|
+
- lib/unifonic_sms/normalizer.rb
|
|
86
|
+
- lib/unifonic_sms/version.rb
|
|
87
|
+
homepage: https://github.com/AssemDeghady/unifonic_sms
|
|
88
|
+
licenses:
|
|
89
|
+
- MIT
|
|
90
|
+
metadata: {}
|
|
91
|
+
post_install_message:
|
|
92
|
+
rdoc_options: []
|
|
93
|
+
require_paths:
|
|
94
|
+
- lib
|
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
|
+
requirements:
|
|
97
|
+
- - ">="
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: 2.3.0
|
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
|
+
requirements:
|
|
102
|
+
- - ">="
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
version: '0'
|
|
105
|
+
requirements: []
|
|
106
|
+
rubyforge_project:
|
|
107
|
+
rubygems_version: 2.6.13
|
|
108
|
+
signing_key:
|
|
109
|
+
specification_version: 4
|
|
110
|
+
summary: Send SMS messages using Unifonic Api.
|
|
111
|
+
test_files: []
|