Skip to content

Instantly share code, notes, and snippets.

@jonsgreen
Created April 17, 2017 23:38
Show Gist options
  • Save jonsgreen/5806f6c56a68fc71d9ed756f62dba131 to your computer and use it in GitHub Desktop.
Save jonsgreen/5806f6c56a68fc71d9ed756f62dba131 to your computer and use it in GitHub Desktop.
require ::File.expand_path('../config/environment', __FILE__)
require ::File.expand_path('../lib/slack_bot', __FILE__)
Thread.abort_on_exception = true
Apartment.tenant_names.each do |schema|
if token = ENV["#{schema.upcase}_SLACK_API_TOKEN"]
Thread.new do
SlackBot.new(schema, token).bot.run
end
end
end
run Rails.application
#file is in config/puma/
workers Integer(ENV["WEB_PROCESSES"] || 2)
threads 8, Integer(ENV["WEB_MAX_THREADS"] || 16)
preload_app!
environment ENV.fetch("RACK_ENV")
# Cloud66 wants this.
bind "unix:///tmp/web_server.sock"
daemonize
pidfile "/tmp/web_server.pid"
stdout_redirect "#{ENV.fetch("RAILS_BASE_PATH")}/shared/log/puma.stdout.log",
"#{ENV.fetch("RAILS_BASE_PATH")}/shared/log/puma.stderr.log",
true # append to logfiles.
quiet # no request logs.
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
#file is in lib/
class SlackBot < SlackRubyBot::Bot
attr_accessor :bot
def initialize(schema, token)
@bot = generate_new_class
@bot.schema = schema
@bot.token = token
@bot.define_matchers
end
def generate_new_class
Class.new(SlackRubyBot::Bot) do
cattr_accessor :schema, :token
def self.hashtag
movement.hashtag
end
def self.movement
Movement.find_by(schema: schema)
end
def self.instance
@server ||= SlackRubyBot::Server.new(token: token)
end
command 'ping' do |client, data, match|
client.say(text: 'pong', channel: data.channel)
end
def self.define_matchers
match %r(#{hashtag}) do |client, data, match|
Apartment::Tenant.switch(schema) do
update = SlackUpdate.new(
message: data.text,
channel: client.channels[data.channel].name,
user: client.users[data.user].name,
team: client.teams[data.team].name,
projects: updated_projects(data.text),
posted_at: Time.at(data.ts.to_i))
if update.save
client.web_client
.chat_postMessage(text: "@#{update.user} I got your update!",
channel: data.user, as_user: true)
end
end
end
end
def self.updated_projects(message)
message.scan(/#[[:alnum:]-]+/).map {|h| h.gsub(hashtag, '')}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment