From 7a5afa2f9cf9f3f2b3346b2d176960ab9af8e65e Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Sun, 14 Jul 2019 02:31:37 -0500 Subject: [PATCH] work in progress --- bot_config.yaml.example | 8 +++++++- commands.rb | 19 ++++++++++++++++++- run.rb | 24 +++++++++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/bot_config.yaml.example b/bot_config.yaml.example index 93ff3b4..6f394cd 100644 --- a/bot_config.yaml.example +++ b/bot_config.yaml.example @@ -9,4 +9,10 @@ allowed_sources: - 'private' - 'group' - 'supergroup' - +probes: + srv1: + loc: 'Office' + srv2: + loc: 'Area 50.1' + srv3: + loc: 'Dungeon' diff --git a/commands.rb b/commands.rb index 4d83a4f..2a76bdd 100644 --- a/commands.rb +++ b/commands.rb @@ -36,7 +36,24 @@ end def process_command_check(message, command, adm) if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) - reply = `cat fakeoutput.txt` + #reply = `scripts/tempf.py` + #some interactive code to select a probe + select_loc = "Aisle 12-3" + #end interactive code + #probe = @probes[0] #set manually for now + reply = "" + 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 + puts reply else reply = "I am not authorized to provide this information here." end diff --git a/run.rb b/run.rb index 2c487cb..57a64af 100755 --- a/run.rb +++ b/run.rb @@ -20,6 +20,7 @@ token = @conf['token'] admin = @conf['admin'] @auth_chat = @conf['authorized_chats'] @allowed_sources = @conf['allowed_sources'] +@probes = @conf['probes'] ### Begin sanity check ### STDOUT.sync = true @@ -104,6 +105,14 @@ if errcount > 0 else print "Environment appears sane.\n\n".green.bold end +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" +else + print "OK\n".green.bold +end + STDOUT.sync = false ### End sanity check ### puts "Starting [#{@botname}]...\n\n" @@ -114,6 +123,13 @@ puts "Temporary direcotry: #{@tmpdir}" puts "Start time: " + timestamp + "\n\n\n\n" STDOUT.flush +def host_lookup(select_loc) + #@probes.select { |k,v| v['loc'] == select_loc }.each do |host,loc| + # return host + #end + return @probes.select { |k,v| v['loc'] == select_loc } +end + def handle_message(message) if ! message.reply_to_message.nil? then #drop message. Someone's replying to a message @@ -186,7 +202,7 @@ def handle_message(message) reply = "Sorry, #{command} is not a valid command." end # Verbose output: - puts timestamp + ": Sending #{reply.inspect}\n\n" + #print timestamp + ": Sending #{reply.inspect} ..... " STDOUT.flush return reply end @@ -198,8 +214,14 @@ Telegram::Bot::Client.run(token) do |bot| #puts "DEBUG: #{validation}" if validation reply = handle_message(message) + print timestamp + ": Sending ..... " if ! message.text.nil? bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") + print "OK\n".green.bold + puts "Sent: #{reply.inspect}\n\n" + else + print 'FAIL'.red.bold + print " (Message is NULL)\n\n" end STDOUT.flush else