mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
Improved specs and minor modifications to TwilioAgent
This commit is contained in:
parent
6bf9b05d59
commit
19defeac3f
3 changed files with 47 additions and 9 deletions
|
@ -7,23 +7,23 @@ module Agents
|
|||
default_schedule "every_10m"
|
||||
|
||||
description <<-MD
|
||||
The TwilioAgent receives and collects events and send them via text message to cellphone when scheduled. Right now, it is assumed that events have `:message` key, the value of which is sent as the content of the text message.
|
||||
Set `:receiverscell` to the number on which you would like to receive text messages.
|
||||
`:expected_receive_period_in_days` is maximum days that you would expect to pass between events being received by this agent.
|
||||
The TwilioAgent receives and collects events and send them via text message to cellphone when scheduled.It is assumed that events have `:message`,`:text` or `:sms` key, the value of which is sent as the content of the text message.
|
||||
Set `receiver_cell` to the number on which you would like to receive text messages.
|
||||
`expected_receive_period_in_days` is maximum days that you would expect to pass between events being received by this agent.
|
||||
MD
|
||||
|
||||
def default_options
|
||||
{
|
||||
:account_sid => "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
:auth_token => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
:senderscell => "xxxxxxxxxx",
|
||||
:receiverscell => "xxxxxxxxxx",
|
||||
:sender_cell => "xxxxxxxxxx",
|
||||
:receiver_cell => "xxxxxxxxxx",
|
||||
:expected_receive_period_in_days => "x"
|
||||
}
|
||||
end
|
||||
|
||||
def validate_options
|
||||
errors.add(:base, "account_sid,auth_token,senderscell,receiverscell,expected_receive_period_in_days are all required") unless options[:account_sid].present? && options[:auth_token].present? && options[:senderscell].present? && options[:receiverscell].present? && options[:expected_receive_period_in_days].present?
|
||||
errors.add(:base, "account_sid,auth_token,sender_cell,receiver_cell,expected_receive_period_in_days are all required") unless options[:account_sid].present? && options[:auth_token].present? && options[:sender_cell].present? && options[:receiver_cell].present? && options[:expected_receive_period_in_days].present?
|
||||
end
|
||||
|
||||
def receive(incoming_events)
|
||||
|
@ -37,11 +37,19 @@ module Agents
|
|||
last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
|
||||
end
|
||||
|
||||
def send_message(client,message)
|
||||
client.account.sms.messages.create(:from=>options[:sender_cell],:to=>options[:receiver_cell],:body=>message)
|
||||
end
|
||||
|
||||
def check
|
||||
if self.memory[:queue] && self.memory[:queue].length > 0
|
||||
@client = Twilio::REST::Client.new options[:account_sid],options[:auth_token]
|
||||
self.memory[:queue].each do |text|
|
||||
@client.account.sms.messages.create(:from=>options[:senderscell],:to=>options[:receiverscell],:body=>"#{text[:message]}")
|
||||
message = text[:message] || text[:text] || text[:sms]
|
||||
if message
|
||||
message.slice! 160, message.length
|
||||
send_message @client, message
|
||||
end
|
||||
end
|
||||
self.memory[:queue] = []
|
||||
end
|
||||
|
|
|
@ -51,7 +51,7 @@ describe Agents::TriggerAgent do
|
|||
|
||||
@checker.should_not be_working # no events have ever been received
|
||||
Agents::TriggerAgent.async_receive(@checker.id, [@event.id])
|
||||
@checker.reload.should be_working # no events have ever been received
|
||||
@checker.reload.should be_working # Events received
|
||||
three_days_from_now = 3.days.from_now
|
||||
stub(Time).now { three_days_from_now }
|
||||
@checker.reload.should_not be_working # too much time has passed
|
||||
|
|
|
@ -2,9 +2,16 @@ require 'spec_helper'
|
|||
|
||||
describe Agents::TwilioAgent do
|
||||
before do
|
||||
@checker = Agents::TwilioAgent.new(:name => "somename", :options => {:account_sid => "x",:auth_token => "x",:senderscell => "x", :receiverscell => "x", :expected_receive_period_in_days => "x"})
|
||||
@checker = Agents::TwilioAgent.new(:name => "somename", :options => {:account_sid => "x",:auth_token => "x",:sender_cell => "x", :receiver_cell => "x", :expected_receive_period_in_days => "1"})
|
||||
@checker.user = users(:bob)
|
||||
@checker.save!
|
||||
|
||||
@event = Event.new
|
||||
@event.agent = agents(:bob_weather_agent)
|
||||
@event.payload = {:message => "Gonna rain.."}
|
||||
@event.save!
|
||||
|
||||
stub.any_instance_of(Agents::TwilioAgent).send_message {}
|
||||
end
|
||||
|
||||
describe "#receive" do
|
||||
|
@ -23,4 +30,27 @@ describe Agents::TwilioAgent do
|
|||
@checker.reload.memory[:queue].should == ["Some payload", "More payload"]
|
||||
end
|
||||
end
|
||||
|
||||
describe "#working?" do
|
||||
it "checks if events have been received within expected receive period" do
|
||||
@checker.should_not be_working # No events received
|
||||
Agents::TwilioAgent.async_receive @checker.id, [@event.id]
|
||||
@checker.reload.should be_working # Just received events
|
||||
two_days_from_now = 2.days.from_now
|
||||
stub(Time).now { two_days_from_now }
|
||||
@checker.reload.should_not be_working # More time have passed than expected receive period without sign of any new event
|
||||
end
|
||||
end
|
||||
|
||||
describe "#check" do
|
||||
before do
|
||||
Agents::TwilioAgent.async_receive @checker.id, [@event.id]
|
||||
end
|
||||
it "should send text message and Memory should be empty after that" do
|
||||
@checker.reload.memory[:queue].should == [{:message => "Gonna rain.."}]
|
||||
Agents::TwilioAgent.async_check(@checker.id)
|
||||
@checker.reload.memory[:queue].should == []
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue