You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.8 KiB
73 lines
1.8 KiB
class MonitorInbox
|
|
include Interactor
|
|
|
|
AUTHORIZED_SENDERS = [
|
|
"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"]
|
|
|
|
fetch_emails_res = FetchEmails.call!(
|
|
email: email,
|
|
password: password,
|
|
# TODO: We need the process to be intelligent and know when was it's last execution
|
|
query_from: calculate_from_time
|
|
)
|
|
|
|
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)
|
|
emails_arr.each do |email|
|
|
puts "Analyzing email with subject: #{email.subject} From: #{email.from} On #{email.date}"
|
|
if is_action_email?(email)
|
|
ejecutar_accion(email)
|
|
end
|
|
end
|
|
end
|
|
|
|
def is_action_email?(email)
|
|
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
|
|
|