#!/usr/bin/env ruby require 'rubygems' require 'yaml' require 'telegram/bot' require 'pp' require 'time' require_relative 'commands.rb' require_relative 'colors.rb' def timestamp Time.now.strftime("%F %H:%M:%S").yellow end #conf = YAML.load(File.read("bot_config.yaml")) @conf = YAML.load_file("bot_config.yaml") @botname = @conf['botname'] token = @conf['token'] telnet = @conf['telnet'] admin = @conf['admin'] @auth_chat = @conf['authorized_chats'] @allowed_sources = @conf['allowed_sources'] ### Begin sanity check ### STDOUT.sync = true errcount = 0 puts "Checking if environment is sane...\n\n" print "Checking bot token ...................... " if token.nil? print "FAIL!\n\n".red.bold puts "No bot token defined in bot_config.yaml!\nTHIS IS REQUIRED! Bot initialization failed; exiting..." exit(1) else print "OK\n".green.bold end print "Checking configured bot name ............ " if @botname.nil? errcount += 1 print "FAIL!\n\n".red.bold puts "Error(#{errcount.to_s}): No bot name defined. This is superficial. We'll call him Bob.\n\n" @botname = "Bob" else print "OK\n".green.bold end print "Checking telnet configuration ........... " if telnet.nil? errcount += 1 print "FAIL!\n\n".red.bold puts "Error(#{errcount.to_s}): No telnet information provided in bot_config.yaml.\nThis is required for nearly all Empyrion-related " + "functions.\nTHIS SHOULD BE ADDRESSED. Continuing. (some commands will return broken messages)\n\n" else print "OK\n".green.bold end print "Checking administrators ................. " if admin.nil? errcount += 1 print "FAIL!\n\n".red.bold puts "Error(#{errcount.to_s}): No admin Telegram IDs provided in bot_config.yaml.\nThis is required for many functions.\n" + "THIS SHOULD BE ADDRESSED. Continuing. (some commands will not be available)\n\n" admin = ["0"] else print "OK\n".green.bold end print "Checking authorized chats ............... " if @auth_chat.nil? errcount += 1 print "FAIL!\n\n".red.bold puts "Error(#{errcount.to_s}): No authorized Telegram group IDs provided in bot_config.yaml.\nThis is required for most Empyrion-related " + "functions.\nTHIS SHOULD BE ADDRESSED. Continuing. (some commands will not be available)\n\n" @auth_chat = ["0"] else print "OK\n".green.bold end puts "Errors found: #{errcount.to_s}\n\n" if errcount > 0 print "Environment is grinning and holding a spatula. Please review your configuration.\n\n".red.bold else print "Environment appears sane.\n\n".green.bold end STDOUT.sync = false ### End sanity check ### puts "Starting [#{@botname}]...\n\n" puts "Empyrion Host: #{telnet['host']}" puts "Empyrion Telnet Port: #{telnet['port']}" puts "Authorized administrator IDs: #{admin}" puts "Authorized chat IDs: #{@auth_chat}" puts "Bot token: #{token}" puts "Start time: " + timestamp + "\n\n\n\n" STDOUT.flush #def process_command_srvstart(message) # puts "Received command: srvstart" # reply = `./srvstart` #end # #def process_command_srvstop(message) # puts "Received command: srvstop" # reply = `./srvstop` #end def handle_message(message) if ! message.reply_to_message.nil? then #drop message. Someone's replying to a message #sent by our bot message.text = nil return end if message.text.nil? # Find out if user(s) joined the group. If so, welcome them #if ! message.new_chat_members.nil? # handle_user_join(message) #else # #Handle non-messages and non-joins here #end return #so that we don't try to process this as a command (below) end #Format sender name if ! message.from.username.nil? message.from.username = "@" + message.from.username elsif ! message.from.first_name.nil? message.from.username = message.from.first_name end #Format command command = message.text.split(" ")[0].split("@")[0].downcase #Strip command from arguments and @tags #command = message.text reply = 'Empty String' #conf = YAML.load_file("bot_config.yaml") telnet = @conf['telnet'] adm = @conf['admin'] puts Time.now.strftime("%F %H:%M:%S").yellow + ": Received command from " + "#{message.from.username}".cyan.bold + " [" + "#{message.from.id}".cyan + "]: " + "#{command}".magenta.bold case command when '/start' reply = "Introduction: I am #{@botname}, and I am here to make life easier for meatbag admins like you. Currently I can retrieve information " + "from an Empyrion server as well as kill the server process. I also have some pretty sweet dance moves.\n\n" + "Commands available:\n/start (Shows this message)\n/srvstart (Currently non-functional)\n/srvstop\n/status\n/whoami or /chatinfo\n/whereareyou or /location\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/empyrion-bot" if message_from_admin?(message, adm) msg_from_admin = true end if is_chat_authorized?(message, @auth_chat) chat_authorized = true end if ! msg_from_admin && ! chat_authorized if message.from.id == message.chat.id reply = reply + "\n\nWARNING: I am not authorized to work with you directly. My functionality is limited." else reply = reply + "\n\nWARNING: I am not authorized to participate with this group. My functionality is limited." end elsif msg_from_admin && ! chat_authorized reply = reply + "\n\nWARNING: Although you are my master, I have not been authorized to participate in this group. My functionality is limited." end when '/srvstart' reply = process_command_srvstart(message, command, adm) when '/srvstop' reply = process_command_srvstop(message, command, adm) when '/status' reply = process_command_srvstatus(message, command, adm) when '/patch', '/patchnotes' reply = process_command_patchnotes(message, command, adm) when '/location', '/whereareyou' if message_from_admin?(message, adm) || is_chat_authorized?(message, @auth_chat) reply = "I am currently located at:\n\nHost: #{`head -n1 /etc/hostname`}ExtIP: #{`curl icanhazip.com 2>/dev/null`}" else reply = "Refusal: I am not authorized to provide this information here." end when '/whoami', '/chatinfo' #reply = "Answer: You are a meatbag named #{message.from.username}\n\nUser ID: #{message.from.id}\n\nChat ID: #{message.chat.id}" reply = "User ID: #{message.from.id}\nChat ID: #{message.chat.id}" if command == '/whoami' reply = "Answer: You are a meatbag named #{message.from.username}\n\n" + reply end when '/pp', '/debug' pp message reply = "Confirmation: Message debug information sent to console." when '/dance' if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) reply = "dance" else reply = "Refusal: I am not authorized to bust a move in this location." end when '/flex', '/unclemike' if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) reply = "flex" else reply = "Refusal: I am not authorized to bring the gun show to this location." end else reply = "Mockery: My name is #{message.from.username}, I am a meatbag, and I think #{command} is a valid command." end # Verbose output: puts timestamp + ": Sending #{reply.inspect}\n\n" STDOUT.flush #puts "End of case" return reply end Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| #pp message validation = validate_incoming_data(message) #puts "DEBUG: #{validation}" if validation reply = handle_message(message) if reply == "dance" || reply == "flex" bot.api.sendVideo(chat_id: message.chat.id, video: "https://img.skyfalltech.net/togra/#{reply}.gif") elsif ! message.text.nil? bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") end STDOUT.flush else puts "Received bad data! [#{message.chat.type}]" puts validation STDOUT.flush end end end