require "option_parser" require "json" require "colorize" require "crest" # program options begin OptionParser.parse do |parser| parser.banner = "Make ONLINE on vk.com\n" parser.on "--version", "Show version" do print "vk0nline ".colorize(:green).mode(:bold) puts "2.0.1".colorize(:yellow) exit(0) end parser.on "--mkconfig", "Make new empty config" do new_config = {appID: nil, userIds: nil, accessToken: nil} File.open("config.json.new", "w") do |f| f.puts new_config.to_json.to_s end print "Config saved to " print "config.json.new".colorize(:yellow).mode(:bold) puts " file." exit(0) end parser.on "-h", "--help", "Show help" do puts parser exit(0) end end rescue ex puts ex.message, "" end begin # JSON::ParseException json = File.open("config.json") do |file| JSON.parse(file) end rescue puts "Parse config.json error".colorize(:red) exit end def get_access_token(app_id : Int32) puts "Open this page, and save token to config.json:".colorize(:yellow) token_url = "https://oauth.vk.com/authorize?client_id=" \ "#{app_id}&redirect_uri=vk.com&response_type=token" \ "&display=mobile&v=5.131&revoke=1&state=01010&scope=offline" puts token_url.colorize(:green).mode(:bold) end if json["accessToken"] == nil get_access_token(json["appID"].as_i) else response = Crest.get( "https://api.vk.com/method/account.setOnline", params: {:voip => "0", :v => "5.131", :access_token => json["accessToken"].as_s, :user_ids => json["userIds"].as_i} ) status = JSON.parse(response.body) begin # Missing hash key if status["error"] puts status["error"]["error_msg"].colorize(:yellow).mode(:bold) puts "Error code #{status["error"]["error_code"]}".colorize(:red) puts "---".colorize(:dark_gray) get_access_token(json["appID"].as_i) end rescue # execute if an exception is raised end begin # Status set if status["response"] == 1 puts "Complete!".colorize(:green).mode(:bold) end rescue # execute if an exception is raised end end