Пример работы с SMS шлюзом на языке Ruby

Отправка СМС:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
199
200
201
202
203
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
233
234
235
236
237
238
239
240
241
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
require 'uri'
require 'net/http'
require 'rexml/document'
require 'active_support/all'
 
#
# Epochta REST API Ruby class
#
# Documentation
# https://www.epochtasms.ru/api/v2.php
#
class EpochtaApi2
 
  #
  # Epochta API constructor
  #
  # @param [String] username
  # @param [String] password
  # @raise [Exception]
  #
  def initialize(username, password)
    raise 'Empty username or password' if username.to_s.empty? || password.to_s.empty?
 
    @username = username
    @password = password
    @protocol = 'https'
    @url = @protocol + "://api.myatompark.com/members/sms/xml.php"
 
  end
 
  #
  # Form and send request to API service
  #
  # @param [Hash] data
  # @return [Hash]
  # @raise [Exception]
  #
  def send_request(data = {})
    request_data = {}
    uri = URI.parse("#{@url}")
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if @protocol == 'https'
    request = Net::HTTP::Post.new(uri.request_uri)
    data[:XML] = '<?xml version="1.0" encoding="UTF-8"?>' + data[:XML].strip
    request.set_form_data(data)
     
    begin
        response = http.request(request)
        request_data[:data] = Hash.from_xml(response.body.to_s).to_json # JSON.parse(response.body)
        request_data[:http_code] = response.code
    rescue Exception => e
        puts "Exception \n  message: #{e.message} \n  backtrace: #{e.backtrace}"
    end
 
    handle_result(request_data)
  end
  private :send_request
 
  #
  # Process results
  #
  # @param [String] custom_message
  # @return [Hash]
  #
  def handle_error (custom_message = nil)
    data = { is_error: true }
    data[:message] = custom_message unless custom_message.nil?
    data
  end
  private :handle_error
 
  #
  # Process errors
  #
  # @param [Hash] data
  # @return [Hash]
  #
  def handle_result (data)
    unless data[:http_code].to_i == 200
      data[:is_error] = true
    end
    data
  end
  private :handle_result
 
  #
  # Send sms
  #
  # @param [String] book_name
  # @return [Hash]
  #
  def send_sms(sender, text, numbers_array)
    return handle_error('Empty sender or text or phones') if sender.empty? || text.empty? || numbers_array.empty?
 
    xml_source =<<END_OF_XML
    <SMS>
        <operations>
            <operation>SEND</operation>
        </operations>
        <authentification>
            <username></username>
            <password></password>
        </authentification>
        <message>
            <sender></sender>
            <text></text>
        </message>
        <numbers>
        </numbers>
    </SMS>
END_OF_XML
 
    doc_rexml = REXML::Document.new(xml_source)
 
    REXML::XPath.first(doc_rexml, "//username").text = @username
    REXML::XPath.first(doc_rexml, "//password").text = @password
 
    REXML::XPath.first(doc_rexml, "//sender").text = sender
    REXML::XPath.first(doc_rexml, "//text").text = text
 
    number_source =<<END_OF_XML
    <number></number>
END_OF_XML
 
    numbers_rexml = REXML::XPath.first(doc_rexml, "//numbers");
    numbers_array.each do |phone|
        number_rexml = REXML::Document.new(number_source)
        number_rexml.root.text = phone
        numbers_rexml.add(number_rexml)
    end
 
    data = { XML: doc_rexml.to_s }
    send_request(data)
  end
 
  #
  # Get price
  #
  # @param [Fixnum] id
  # @param [String] new_name
  # @return [Hash]
  #
  def get_price(sender, text, numbers_array)
    return handle_error('Empty sender or text or phones') if sender.empty? || text.empty? || numbers_array.empty?
 
    xml_source =<<END_OF_XML
    <SMS>
        <operations>
            <operation>GETPRICE</operation>
        </operations>
        <authentification>
            <username></username>
            <password></password>
        </authentification>
        <message>
            <sender></sender>
            <text></text>
        </message>
        <numbers>
        </numbers>
    </SMS>
END_OF_XML
 
    doc_rexml = REXML::Document.new(xml_source)
 
    REXML::XPath.first(doc_rexml, "//username").text = @username
    REXML::XPath.first(doc_rexml, "//password").text = @password
 
    REXML::XPath.first(doc_rexml, "//sender").text = sender
    REXML::XPath.first(doc_rexml, "//text").text = text
 
    number_source =<<END_OF_XML
    <number></number>
END_OF_XML
 
    numbers_rexml = REXML::XPath.first(doc_rexml, "//numbers");
    numbers_array.each do |phone|
        number_rexml = REXML::Document.new(number_source)
        number_rexml.root.text = phone
        numbers_rexml.add(number_rexml)
    end
 
    send_request({ XML: doc_rexml.to_s })
  end
 
  #
  # Get balance
  #
  # @param [Fixnum] id
  # @return [Hash]
  #
  def get_balance()
    xml_source =<<END_OF_XML
    <SMS>
        <operations>
            <operation>BALANCE</operation>
        </operations>
        <authentification>
            <username></username>
            <password></password>
        </authentification>
    </SMS>
END_OF_XML
 
    doc_rexml = REXML::Document.new(xml_source)
 
    REXML::XPath.first(doc_rexml, "//username").text = @username
    REXML::XPath.first(doc_rexml, "//password").text = @password
 
    send_request({ XML: doc_rexml.to_s })
  end
 
  #
  # Get credit price
  #
  # @param [Fixnum] limit
  # @param [Fixnum] offset
  # @return [Hash]
  #
  def get_credit_price()
    xml_source =<<END_OF_XML
    <SMS>
        <operations>
            <operation>CREDITPRICE</operation>
        </operations>
        <authentification>
            <username></username>
            <password></password>
        </authentification>
    </SMS>
END_OF_XML
 
    doc_rexml = REXML::Document.new(xml_source)
 
    REXML::XPath.first(doc_rexml, "//username").text = @username
    REXML::XPath.first(doc_rexml, "//password").text = @password
 
    send_request({ XML: doc_rexml.to_s })
  end
 
  #
  # Get status
  #
  # @param [Fixnum] id
  # @return [Hash]
  #
  def get_status(id_array)
    return handle_error('Empty list id') if id_array.empty?
 
    xml_source =<<END_OF_XML
    <SMS>
        <operations>
            <operation>GETSTATUS</operation>
        </operations>
        <authentification>
            <username></username>
            <password></password>
        </authentification>
        <statistics>
        </statistics>
    </SMS>
END_OF_XML
 
    doc_rexml = REXML::Document.new(xml_source)
 
    REXML::XPath.first(doc_rexml, "//username").text = @username
    REXML::XPath.first(doc_rexml, "//password").text = @password
     
    messageid_source =<<END_OF_XML
    <messageid></messageid>
END_OF_XML
 
    statistics_rexml = REXML::XPath.first(doc_rexml, "//statistics");
    id_array.each do |id|
        messageid_rexml = REXML::Document.new(messageid_source)
        messageid_rexml.root.text = id
        statistics_rexml.add(messageid_rexml)
    end
 
    send_request({ XML: doc_rexml.to_s })
  end
 
end
 
 
#usage
api_username = 'your api username'
api_password = 'your api password'
 
epochta_api2 = EpochtaApi2.new(api_username, api_password)
 
puts epochta_api2.get_balance
puts "\n"
 
puts epochta_api2.get_credit_price
puts "\n"
 
#puts epochta_api2.send_sms("TeSender", "bla bla bla..", ["380933637995"])
#puts "\n"
 
puts epochta_api2.get_price("TeSender", "bla bla bla..", ["380933637995"])
puts "\n"
vector
image

Нужна помощь?

Задайте вопрос специалисту технической поддержки