Add possibility to handle specific actions

By defining specific actions, you can define the answer when a
validation error is found on a SMS.
master
Guillaume DOTT 2013-11-07 11:03:25 +01:00
parent 9ef1b9327d
commit ce8e53a4ed
5 changed files with 40 additions and 11 deletions

View File

@ -4,7 +4,9 @@ require 'smsd'
require 'net/http'
cli = SMSd::CLI.new(ARGV) do
machine = SMSd::AnsweringMachine.new(I18n.t(:default_answer))
machine = SMSd::AnsweringMachine.new('I did not understand.',
too_old: 'Your message is too old',
short_number: 'The phone number you are using is too short')
machine.add_action(/ping/i, 'PONG')
machine.add_action(/free/i) do
@ -17,4 +19,5 @@ cli = SMSd::CLI.new(ARGV) do
machine
end
cli.modem.prefered_storage('SM')
cli.run

View File

@ -1,16 +1,25 @@
module SMSd
class AnsweringMachine
attr_accessor :actions, :default_action
attr_accessor :actions, :default_action, :specific_actions
def initialize(default_answer = nil, &block)
def initialize(default_answer = nil, specific_actions = {}, &block)
self.actions = []
self.default_action = Action.new(nil, default_answer, &block)
self.specific_actions = {}
specific_actions.each do |key, value|
self.specific_actions[key] = Action.new(nil, value)
end
end
def add_action(regexp, answer = nil, &block)
actions << Action.new(regexp, answer, &block)
end
def execute_action(action, from, to, message)
specific_actions[action].answer(from, to, message) if specific_actions.key?(action)
end
def execute(from, to, message)
actions.each do |action|
return action.answer(from, to, message) if message =~ action.regexp

View File

@ -10,10 +10,10 @@ module SMSd
def answer(from, to, message)
case action
when String
action
when Proc
action.call(from, to, message)
else
action
end
end
end

View File

@ -68,7 +68,7 @@ module SMSd
if sms.valid?
send_answer(sms)
else
logger.warn "#{sms}: #{sms.errors.join(',')}"
send_errors(sms)
end
modem.delete(sms.id)
end
@ -77,12 +77,29 @@ module SMSd
to = (phone_numbers.empty? ? phone_numbers.first[:number] : nil)
message = machine.execute(sms.phone_number, to, sms.message)
send_sms(sms, message)
end
def send_errors(sms)
logger.warn "#{sms}: #{sms.errors.values.join(',')}"
to = (phone_numbers.empty? ? phone_numbers.first[:number] : nil)
errors = sms.errors.keys.map do |error|
machine.execute_action(error, sms.phone_number, to, sms.message)
end
message = errors.compact.join(' ')
send_sms(sms, message) unless errors.include?(false)
end
def send_sms(sms, message)
if message.nil? || message == ''
log = 'Empty answer'
else
modem.send(Biju::Sms.new(
phone_number: sms.phone_number, message: message))
log = message
phone_number: sms.phone_number, message: message[0, 160]))
log = "Sent answer \"#{message}\""
end
logger.info "#{sms}: #{log}"

View File

@ -2,10 +2,10 @@ class Biju::Sms
attr_reader :errors
def valid?
@errors = []
@errors = {}
@errors << 'Message too old' if datetime.to_time < Time.now - (5 * 60)
@errors << 'Phone number too short' if phone_number.length < 6
@errors[:too_old] = 'Message too old' if datetime.to_time < Time.now - (5 * 60)
@errors[:short_number] = 'Phone number too short' if phone_number.length < 6
@errors.empty?
end