|
|
|
@ -2,9 +2,17 @@ class MonitorInbox |
|
|
|
include Interactor |
|
|
|
|
|
|
|
AUTHORIZED_SENDERS = [ |
|
|
|
"rodolfo.leyes@gmail.com" |
|
|
|
"rodolfo.leyes@gmail.com", |
|
|
|
"ing_rodolfo_utn@hotmail.com" |
|
|
|
] |
|
|
|
|
|
|
|
VALID_ACTION_TERMS = [ |
|
|
|
"encender", |
|
|
|
"poweron", |
|
|
|
] |
|
|
|
|
|
|
|
def call |
|
|
|
context.actions_executed_count=0 |
|
|
|
email = context.email || ENV["EMAIL"] |
|
|
|
password = context.password || ENV["PASSWORD"] |
|
|
|
|
|
|
|
@ -12,11 +20,22 @@ class MonitorInbox |
|
|
|
email: email, |
|
|
|
password: password, |
|
|
|
# TODO: We need the process to be intelligent and know when was it's last execution |
|
|
|
query_from: Time.now - 3.days |
|
|
|
query_from: calculate_from_time |
|
|
|
) |
|
|
|
|
|
|
|
context.emails_arr = fetch_emails_res.messages |
|
|
|
analyze_emails(context.emails_arr) |
|
|
|
emails_arr = fetch_emails_res.messages |
|
|
|
analyze_emails(emails_arr) |
|
|
|
context.analyzed_emails_count = emails_arr.count |
|
|
|
end |
|
|
|
|
|
|
|
def calculate_from_time |
|
|
|
latest_action_log = ActionLog.last |
|
|
|
time = if latest_action_log |
|
|
|
latest_action_log.created_at |
|
|
|
else |
|
|
|
Time.now |
|
|
|
end |
|
|
|
time |
|
|
|
end |
|
|
|
|
|
|
|
def analyze_emails(emails_arr) |
|
|
|
@ -29,11 +48,26 @@ class MonitorInbox |
|
|
|
end |
|
|
|
|
|
|
|
def is_action_email?(email) |
|
|
|
# For now we only recognize the encender action |
|
|
|
email.subject.downcase.include?("encender") && AUTHORIZED_SENDERS.include?(email.from.downcase) |
|
|
|
VALID_ACTION_TERMS.include?(email.subject.downcase) && AUTHORIZED_SENDERS.include?(email.from.downcase) |
|
|
|
end |
|
|
|
|
|
|
|
def ejecutar_accion(email) |
|
|
|
al=ActionLog.new( |
|
|
|
from: email.from, |
|
|
|
subject: email.subject, |
|
|
|
created_at: email.date |
|
|
|
) |
|
|
|
|
|
|
|
if al.valid? |
|
|
|
puts "Executing action..." |
|
|
|
command = ENV["COMANDO_ACCION"] |
|
|
|
system(command) |
|
|
|
context.actions_executed_count+=1 |
|
|
|
al.save! |
|
|
|
else |
|
|
|
# We're using the validate uniqueness of ActionLog to detect duplicate actions |
|
|
|
# we have already processed if we get this exception we should not execute it |
|
|
|
puts "We have already executed this action" |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|