Skip to content

Instantly share code, notes, and snippets.

@jfilip
Forked from bnmrrs/bot_template.rb
Last active May 13, 2016 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfilip/87062380a35ec70c29190f1e842251a7 to your computer and use it in GitHub Desktop.
Save jfilip/87062380a35ec70c29190f1e842251a7 to your computer and use it in GitHub Desktop.
# Bot Rails Template
#
# Use this script to help generate a Rails bot.
#
# rails new rails_app -m bot_template.rb
#
run "rm Gemfile"
run "touch Gemfile"
add_source 'http://rubygems.org'
gem 'rails', '4.2.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'bot'
gem_group :development do
gem 'quiet_assets'
end
gem_group :development, :test do
gem 'sqlite3'
gem 'dotenv-rails'
gem 'pry'
gem 'web-console', '~> 2.0'
gem 'spring'
end
gem_group :production do
gem 'pg'
gem 'puma'
gem 'rails_12factor'
end
run "bundle install --without production"
# Generate the required files for our Bot gem
run "rails generate bot:install"
# Add the Bot route so Kik can send notifications
route "mount_bot 'kik-notify'"
# Find out what our Bot details are
bot_name = ask "What is your bot username?"
bot_token = ask "What is your bot API key? (https://dev.kik.com/#/engine)"
host_url = ask "What is your host url? (eg: https://mybot.ngrok.io)"
use_mixpanel = yes?("Would you like to use Mixpanel? (Yes/No)")
run "touch .ruby-version"
run "touch .ruby-gemset"
append_file ".ruby-version", "2.2.2"
append_file ".ruby-gemset", bot_name
# Persist our Bot details using dotenv
file '.env', <<-CODE
KIK_BOT_USER=#{bot_name}
KIK_TOKEN=#{bot_token}
HOST_URL=#{host_url}
CALLBACK_URL=#{host_url}/kik-notify
REDIS_URL=redis://localhost:6379/15
CODE
if use_mixpanel
mixpanel_token = ask "What is your Mixpanel token?"
gem 'mixpanel-ruby'
append_file ".env", "MIXPANEL_TOKEN=#{mixpanel_token}"
initializer 'mixpanel.rb', <<-CODE
MIXPANEL = Mixpanel::Tracker.new(ENV['MIXPANEL_TOKEN'])
CODE
run 'rm app/bot/application_handler.rb'
file 'app/bot/application_handler.rb', <<-CODE
class ApplicationHandler < Bot::Handler
use Responders::Default
def user_for(message)
user = User.find_or_create_by(username: message["from"])
MIXPANEL.people.set(user.username, {
username: user.username
})
user
end
end
CODE
end
file 'config/puma.rb', <<-CODE
workers Integer(ENV['WEB_CONCURRENCY'] || 1)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
CODE
# Remove our default bot initializer and replace it with one that uses ENV
# to get the bot details
run "rm config/initializers/bot.rb"
initializer 'bot.rb', <<-CODE
Bot.configure do |config|
config.adapter = Bot::Adapter::Kik.new(bot_user: ENV['KIK_BOT_USER'], bot_token: ENV['KIK_TOKEN'])
config.redis = Redis.new
end
CODE
append_file ".gitignore", <<-CODE
.env
CODE
# Generate a User model
generate :model, "User", "username:string", "introduced:boolean"
rake "db:migrate"
# Tell Kik about our bot
rake "bot:kik:configure"
run "git init"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment