Add possibility to handle specific actions

By defining specific actions, you can define the answer when a
validation error is found on a SMS.
This commit is contained in:
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' require 'net/http'
cli = SMSd::CLI.new(ARGV) do 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(/ping/i, 'PONG')
machine.add_action(/free/i) do machine.add_action(/free/i) do
@ -17,4 +19,5 @@ cli = SMSd::CLI.new(ARGV) do
machine machine
end end
cli.modem.prefered_storage('SM')
cli.run cli.run

View File

@ -1,16 +1,25 @@
module SMSd module SMSd
class AnsweringMachine 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.actions = []
self.default_action = Action.new(nil, default_answer, &block) 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 end
def add_action(regexp, answer = nil, &block) def add_action(regexp, answer = nil, &block)
actions << Action.new(regexp, answer, &block) actions << Action.new(regexp, answer, &block)
end 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) def execute(from, to, message)
actions.each do |action| actions.each do |action|
return action.answer(from, to, message) if message =~ action.regexp return action.answer(from, to, message) if message =~ action.regexp

View File

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

View File

@ -68,7 +68,7 @@ module SMSd
if sms.valid? if sms.valid?
send_answer(sms) send_answer(sms)
else else
logger.warn "#{sms}: #{sms.errors.join(',')}" send_errors(sms)
end end
modem.delete(sms.id) modem.delete(sms.id)
end end
@ -77,12 +77,29 @@ module SMSd
to = (phone_numbers.empty? ? phone_numbers.first[:number] : nil) to = (phone_numbers.empty? ? phone_numbers.first[:number] : nil)
message = machine.execute(sms.phone_number, to, sms.message) 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 == '' if message.nil? || message == ''
log = 'Empty answer' log = 'Empty answer'
else else
modem.send(Biju::Sms.new( modem.send(Biju::Sms.new(
phone_number: sms.phone_number, message: message)) phone_number: sms.phone_number, message: message[0, 160]))
log = message log = "Sent answer \"#{message}\""
end end
logger.info "#{sms}: #{log}" logger.info "#{sms}: #{log}"

View File

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