Пример работы с SMS шлюзом на языке Ruby
Отправка смс
Получения статуса отправленной смс
Получение цены отправки
Получение баланса
Отправка смс:
require 'uri'
require 'net/http'
require 'rexml/document'
require 'active_support/all'
#
# Epochta REST API Ruby class
#
# Documentation
# /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 = 'http'
@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 == 'http'
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..", ["380933630000"])
#puts "\n"
puts epochta_api2.get_price("TeSender", "bla bla bla..", ["380933630000"])
puts "\n"
По запросу предоставляется необходимое количество дополнительных смс для тестирования сервиса массовых смс-уведомлений ePochta SMS.
Есть вопрос?
- 8 (800) 555-09-63
- Бесплатно по России
Новости:
- 2020-11-27
- Внимание! С 1 декабря 2020 года меняется цена на отправку СМС в сети Билайн
- 2020-11-23
- ePochta объявляет старт Черной Пятницы: Спеццена на рассылку в WhatsApp
- 2020-11-18
- С 1 декабря 2020 года оператор МТС повышает стоимость СМС
- 2020-05-04
- Вебинар: СМС маркетинг. Что писать в тексте СМС рассылок?
- 2020-04-28
- Операторы повышают цены СМС рассылки по прямому каналу в РФ


7 (495) 649-62-48
Украинский
Казахский
English
Italiano
