#!/usr/bin/env ruby #$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'chatterbot' require 'chatterbot/skeleton' require 'optparse' include Chatterbot::Helpers include Chatterbot::Config include Chatterbot::Logging Chatterbot::from_helper = true @force = false @skeleton = true # # pull in any arguments and set them as env variables # opts = OptionParser.new opts.banner = <<-EOS Register a bot with Twitter, and generate a script to run it. This script will walk you through the whole process. Usage: #{File.basename($0)} [options] EOS opts.separator "" opts.separator "Options:" opts.on('-d', '--db [ARG]', "Specify a DB connection URI") { |d| ENV["chatterbot_db"] = d } opts.on('-c', '--config [ARG]', "Specify a config file to load") { |c| ENV["chatterbot_config"] = c } opts.on('-b', '--bot [ARG]', "Name of the bot you want to register. If you don't specify this, it will be grabbed from twitter when you authenticate") { |b| @botname = b } opts.on('-f', '--force', "Overwrite existing config values") { @force = true } opts.on('-s', '--skip-skeleton', "Don't write out a skeleton bot script") { @skeleton = false } opts.on_tail("-h", "--help", "Show this message") do puts opts exit end extra = opts.parse!(ARGV) if @botname.nil? @botname = "tmp_bot_name" end @bot = Chatterbot::Bot.new(:name => @botname) if ! @bot.needs_auth_token? && @force == false puts "Looks like your bot has already been authorized." puts "If something went wrong, run #{File.basename($0)} again with the '-f' flag." end @bot.config.delete(:token) @bot.require_login(false) unless @bot.screen_name.nil? if @botname == "tmp_bot_name" puts "change botname to #{@bot.screen_name}" @bot.botname = @bot.screen_name end puts "Storing config info" puts @bot.config.inspect config_src = File.join(Dir.getwd, "tmp_bot_name.yml") config_dest = File.join(Dir.getwd, "#{@bot.botname}.yml") File.rename(config_src, config_dest) if @skeleton skel_dest = File.join(Dir.getwd, "#{@bot.botname}.rb") if File.exist?(skel_dest) puts "#{skel_dest} exists already, not doing anything" else puts "writing skeleton to #{skel_dest}" f = File.new(skel_dest, "w") script = Chatterbot::Skeleton.generate(@bot) f.write(script) f.close File.chmod(0740, skel_dest) end end else puts "Looks like something went wrong, better luck next time!" end