Added error checking to probe processing

pull/2/head
Aaron Johnson 6 years ago
parent 07b7f46dd7
commit ae76468203

@ -1,7 +1,9 @@
def process_callback_zone(message) def process_callback_zone(message)
host = message.data.split("|")[1] host = message.data.split("|")[1]
tdata = `ssh #{host} heatbot_gettemp`
zone = @probes[host].values.first zone = @probes[host].values.first
puts "Selected: #{zone}" puts "Selected: #{zone}"
send_message(message.message.chat.id, "#{zone}: #{tdata}°#{@tunit}") tdata = process_tdata(host)
#puts "Selected: #{zone} [#{tdata}]"
send_message(message.message.chat.id, "#{zone}: #{tdata}")
STDOUT.flush
end end

@ -28,7 +28,7 @@ end
def process_command_start(message, command, adm) def process_command_start(message, command, adm)
reply = "I am #{@botname}, and I am here to provide temperature information from various sensors. Currently I can retrieve information " + reply = "I am #{@botname}, and I am here to provide temperature information from various sensors. Currently I can retrieve information " +
"from temperature sensors as well as report when temperatures are out of a specified range.\n\n" + "from temperature sensors as well as report when temperatures are out of a specified range.\n\n" +
"Commands available:\n/start (Shows this message)\n/check (Show temperatures, currently non-functional)\n/whoami or /chatinfo (Provides IDs for internal use)\n/whereareyou or /location (Provides hostname for bot server)\n\n" + "Commands available:\n/start (Shows this message)\n/check or /c (Show temperatures)\n/report or /r (Show all temperatures)\n/whoami or /chatinfo (Provides IDs for internal use)\n/whereareyou or /location (Provides hostname for bot server)\n\n" +
"Check again later to see if any new functions have been added, or use /patchnotes to learn about recent updates.\n" + "Check again later to see if any new functions have been added, or use /patchnotes to learn about recent updates.\n" +
"You can also view the source code at the following location:\nhttps://git.skyfall.tech/skyfall/heatbot" "You can also view the source code at the following location:\nhttps://git.skyfall.tech/skyfall/heatbot"
if message_from_admin?(message, adm) if message_from_admin?(message, adm)
@ -87,18 +87,6 @@ def process_command_check(message, command, adm)
message_text = "Which area would you like to check?\n" message_text = "Which area would you like to check?\n"
send_question(message.chat.id, message_text, options) send_question(message.chat.id, message_text, options)
#end interactive code #end interactive code
#probe = @probes[0] #set manually for now
#hostdata = host_lookup(select_loc)
#puts "var:".blue+" hostdata".blue.bold+"::".bold + hostdata.to_s
#if ! hostdata.empty?
# hostdata.each do |k,v|
# puts "match:".blue+" host".blue.bold+"::".bold + k.to_s
# reply += k.to_s+"\n"
# end
#else
# reply = "No matching locations!"
# puts "hostdata is empty!"
#end
else else
send_message(message.chat.id,"I am not authorized to provide this information here.") send_message(message.chat.id,"I am not authorized to provide this information here.")
end end
@ -106,16 +94,23 @@ end
def process_command_report(message, command, adm) def process_command_report(message, command, adm)
if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm)
confirmation = send_message(message.chat.id,"Generating full report... Please wait.")
report = "Full Report:\n\n" report = "Full Report:\n\n"
@probes.each do |k,v| @probes.each do |k,v|
zone = v["loc"].to_s zone = v["loc"].to_s
host = k host = k
print "#{host}: #{zone} " #print "#{host}: #{zone} "
tdata = `ssh #{host} heatbot_gettemp` tdata = process_tdata(host)
print "[#{tdata}°#{@tunit}]\n" #tdata = `ssh #{host} heatbot_gettemp`
report = report + "#{zone}: #{tdata}°#{@tunit}\n" #print "[#{tdata}°#{@tunit}]\n"
#report = report + "#{zone}: #{tdata}°#{@tunit}\n"
#print "[#{tdata}]\n"
report = report + "#{zone}: #{tdata}\n"
STDOUT.flush
end end
send_message(message.chat.id,report) send_message(message.chat.id,report)
#delete_message(confirmation)
#delete_confirmation(confirmation)
else else
send_message(message.chat.id,"I am not authorized to provide this information here.") send_message(message.chat.id,"I am not authorized to provide this information here.")
end end

@ -105,7 +105,8 @@ print "Checking temperature units .............. "
if @tunit.nil? if @tunit.nil?
errcount += 1 errcount += 1
print "FAIL!\n\n".red.bold print "FAIL!\n\n".red.bold
puts "Error(#{errcount.to_s}): No temperature probes configured! Bot will serve no purpose. Continuing anyway....\n\n" puts "Error(#{errcount.to_s}): Temperature units not configured! Defaulting to Rankine. Continuing...\n\n"
@tunit = 'R'
else else
print "OK\n".green.bold print "OK\n".green.bold
end end
@ -113,7 +114,7 @@ print "Checking probes list .................... "
if @probes.nil? if @probes.nil?
errcount += 1 errcount += 1
print "FAIL!\n\n".red.bold print "FAIL!\n\n".red.bold
puts "Error(#{errcount.to_s}): No temperature probes configured! Bot will serve no purpose. Continuing anyway....\n\n" puts "Error(#{errcount.to_s}): No temperature probes configured! Bot will serve no purpose. Continuing anyway...\n\n"
else else
print "OK\n".green.bold print "OK\n".green.bold
end end
@ -157,6 +158,29 @@ def host_lookup(select_loc)
return @probes.select { |k,v| v['loc'] == select_loc } return @probes.select { |k,v| v['loc'] == select_loc }
end end
class String
def is_integer?
/\A[-+]?\d+\z/ === self
#!!(self =~ /\A[-+]?[0-9]+\z/)
end
end
def process_tdata(host)
print "Processing #{host}: "
if system("nmap #{host} -p 22 2>&1 | grep 22 | grep open >/dev/null")
tdata = `ssh -oBatchMode=yes #{host} heatbot_gettemp`
if tdata.is_integer?
print tdata + "°#{@tunit}\n"
return tdata + "°#{@tunit}"
else
puts "Unexpected output from [".red.bold + host.bold + "]: ".red.bold + tdata
return "CHECK PROBE"
end
else
print "OFFLINE\n"
return "OFFLINE"
end
end
def ack_callback(message, display_message = true) def ack_callback(message, display_message = true)
#Delete message and notify user that we got the request #Delete message and notify user that we got the request
begin begin
@ -186,15 +210,32 @@ def delete_message(message)
STDOUT.flush STDOUT.flush
end end
def delete_confirmation(message)
#Deletes a message referred to by message_id
begin
Telegram::Bot::Client.run(@token) do |bot|
bot.api.deleteMessage(chat_id: message.chat.id, message_id: message.message_id)
end
rescue
puts "Error deleting message. Error: " + $!.message
end
STDOUT.flush
end
def send_message(chatid, message_text, imageurl = nil) def send_message(chatid, message_text, imageurl = nil)
if imageurl != nil if imageurl != nil
#Send message with text as html link to image #Send message with text as html link to image
Telegram::Bot::Client.run(@token) {|bot| bot.api.send_message(chat_id: chatid, text: "#{message_text}<a href=\"#{imageurl}\">.</a>", parse_mode: "HTML") } message = Telegram::Bot::Client.run(@token) {|bot| message = bot.api.send_message(chat_id: chatid, text: "#{message_text}<a href=\"#{imageurl}\">.</a>", parse_mode: "HTML") }
puts timestamp + ": Sent: #{message_text.inspect}\n\n" #puts timestamp + ": Sent: #{message_text.inspect}\n\n"
return message
else else
#Send a plain-text message #Send a plain-text message
Telegram::Bot::Client.run(@token) {|bot| bot.api.send_message(chat_id: chatid, text: message_text) } message = Telegram::Bot::Client.run(@token) {|bot| bot.api.send_message(chat_id: chatid, text: message_text) }
puts timestamp + ": Sent: #{message_text.inspect}\n\n" puts timestamp + ": Sent: #{message_text.inspect}\n\n"
#puts message
#message = message["results"]
#puts message
return message
end end
STDOUT.flush STDOUT.flush
end end
@ -298,7 +339,7 @@ def handle_callback_query(message)
return return
end end
ack_callback(message) #Change the movie/show selection message to "Request received..." ack_callback(message) #Change the selection message to "Request received..."
case command case command
when "ZONE" #Get temp info from location when "ZONE" #Get temp info from location

Loading…
Cancel
Save