Commit 48ad2927 authored by Andi Saurer's avatar Andi Saurer

deliver message

parent eac083f3
class ApplicationMailer < ActionMailer::Base
default from: ShortMessage.config.default_mail_sender
end
module ShortMessage
class Mailer < ApplicationMailer
def error_notification message, response
mail to: ShortMessage.config.admin_notification_email, subject: "Error delivering SMS to #{message.recipient}", body: "SMS from #{message.sender} to #{message.recipient} could not be sent!\r\n\r\nResponse: #{response.to_yaml}"
end
def payment_required_notification
mail to: ShortMessage.config.reload_notification_email, subject: "SMS Gateway requires payment", body: "Your SMS Gateway API #{ShortMessage.config.gateway_server} requires payment!"
end
end
end
module ShortMessage
class Message < ActiveRecord::Base
def deliver
self.sender = ShortMessage.config.default_sms_sender if self.sender.blank?
unless self.recipient.blank? and self.text.blank?
http = Net::HTTP.new(ShortMessage.config.gateway_server, ShortMessage.config.gateway_port)
if ShortMessage.config.gateway_port == Net::HTTP.https_default_port()
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
response, data = http.post(ShortMessage.config.send_file_path, build_deliver_params_string)
if response.code == "200" or response.code == "402"
if response.code == "402" and not ShortMessage.config.reload_notification_email.blank?
ShortMessage::Mailer.payment_required_notification(self, response).deliver_now
end
result_set = response.body.gsub("(","").gsub(")","").split(" ")
self.message_key = result_set[2] unless result_set[2].blank?
return self.save
else
ShortMessage::Mailer.error_notification(self, response).deliver_now unless ShortMessage.config.admin_notification_email.blank?
end
else
return false
end
end
def recharge
raise "NOT YET IMPLEMENTED"
end
def status_text
I18n.t("short_message.status.code_#{self.status_code}")
end
private
def build_deliver_params_string
params = []
params << "user=#{ShortMessage.config.user}"
params << "pass=#{ShortMessage.config.password}"
params << "from=#{CGI.escape(self.sender)}"
params << "to=#{CGI.escape(self.recipient)}"
params << "text=#{CGI.escape(self.text)}"
params.join("&")
end
def build_recharge_params_string amount
raise "NOT YET IMPLEMENTED"
# params = []
# params << "user=#{ShortMessage.config.user}"
# params << "password=#{ShortMessage.config.password}"
# params << "amount=#{amount}"
# params.join("&")
end
end
end
class CreateShortMessageMessages < ActiveRecord::Migration
def change
create_table :short_message_messages do |t|
t.string :message_key
t.string :sender
t.string :recipient
t.integer :status_code
t.text :text
t.timestamps null: false
end
end
end
......@@ -3,7 +3,7 @@ ShortMessage.configure do |config|
config.gateway_server = "http.gtx-messaging.net"
config.gateway_port = 443
config.send_file_path = "smsc.php"
config.send_file_path = "/smsc.php"
# config.account_functions_path = ""
# enter your details received from provider here
......@@ -15,7 +15,7 @@ ShortMessage.configure do |config|
config.default_mail_sender = "webmaster@your-domain.com"
config.reload_notification_email = "webmaster@your-domain.com" # send a message if payment is required
config.admin_notification_email = "webmaster@your-domain.com" # set nil to deactivate error mailing
config.voucher_notification_email = "give-me-money@your-domain.com" # set nil to disable voucher mailing
# config.voucher_notification_email = "give-me-money@your-domain.com" # set nil to disable voucher mailing
# set a default sms sender (used if no sender is present)
# config.default_sms_sender = ""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment