From ae764682030b39d52aad368120714bd13c8992d9 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Sun, 15 Sep 2019 16:28:33 -0500 Subject: [PATCH] Added error checking to probe processing --- callbacks.rb | 6 ++++-- commands.rb | 29 ++++++++++++---------------- run.rb | 53 ++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/callbacks.rb b/callbacks.rb index 00a2f82..6ff5854 100644 --- a/callbacks.rb +++ b/callbacks.rb @@ -1,7 +1,9 @@ def process_callback_zone(message) host = message.data.split("|")[1] - tdata = `ssh #{host} heatbot_gettemp` zone = @probes[host].values.first 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 diff --git a/commands.rb b/commands.rb index 18efa1b..4129ef8 100644 --- a/commands.rb +++ b/commands.rb @@ -28,7 +28,7 @@ end 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 " + "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" + "You can also view the source code at the following location:\nhttps://git.skyfall.tech/skyfall/heatbot" 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" send_question(message.chat.id, message_text, options) #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 send_message(message.chat.id,"I am not authorized to provide this information here.") end @@ -106,16 +94,23 @@ end def process_command_report(message, command, 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" @probes.each do |k,v| zone = v["loc"].to_s host = k - print "#{host}: #{zone} " - tdata = `ssh #{host} heatbot_gettemp` - print "[#{tdata}°#{@tunit}]\n" - report = report + "#{zone}: #{tdata}°#{@tunit}\n" + #print "#{host}: #{zone} " + tdata = process_tdata(host) + #tdata = `ssh #{host} heatbot_gettemp` + #print "[#{tdata}°#{@tunit}]\n" + #report = report + "#{zone}: #{tdata}°#{@tunit}\n" + #print "[#{tdata}]\n" + report = report + "#{zone}: #{tdata}\n" + STDOUT.flush end send_message(message.chat.id,report) + #delete_message(confirmation) + #delete_confirmation(confirmation) else send_message(message.chat.id,"I am not authorized to provide this information here.") end diff --git a/run.rb b/run.rb index ddfaa3f..283938e 100755 --- a/run.rb +++ b/run.rb @@ -105,7 +105,8 @@ print "Checking temperature units .............. " if @tunit.nil? errcount += 1 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 print "OK\n".green.bold end @@ -113,7 +114,7 @@ print "Checking probes list .................... " if @probes.nil? errcount += 1 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 print "OK\n".green.bold end @@ -157,6 +158,29 @@ def host_lookup(select_loc) return @probes.select { |k,v| v['loc'] == select_loc } 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) #Delete message and notify user that we got the request begin @@ -186,15 +210,32 @@ def delete_message(message) STDOUT.flush 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) if imageurl != nil #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}.", parse_mode: "HTML") } - puts timestamp + ": Sent: #{message_text.inspect}\n\n" + message = Telegram::Bot::Client.run(@token) {|bot| message = bot.api.send_message(chat_id: chatid, text: "#{message_text}.", parse_mode: "HTML") } + #puts timestamp + ": Sent: #{message_text.inspect}\n\n" + return message else #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 message + #message = message["results"] + #puts message + return message end STDOUT.flush end @@ -298,7 +339,7 @@ def handle_callback_query(message) return 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 when "ZONE" #Get temp info from location