Create a gist now

Instantly share code, notes, and snippets.

@kinoppyd /Gemfile Secret
Last active Dec 7, 2016

What would you like to do?
Slack Watcher
source "https://rubygems.org"
gem "slack-api"
gem "slack-rtmapi"
gem "dotenv"
gem "activesupport"
require 'slack'
require 'slack-rtmapi'
require 'dotenv'
require 'active_support'
require 'active_support/core_ext'
POST_CHANNEL_NAME = 'bot_devastation'
Dotenv.load
class StandardError
def report
"#{self.class}: #{message}\n#{backtrace.join("\n")}"
end
end
class SlackChannelWatcher
def initialize(channel_name_for_post)
@channel_id_for_post = channel_name_to_id(channel_name_for_post)
end
def slack
@slack ||= Slack.client(token: ENV['SLACK_TOKEN'])
end
def rtm_client(opts = {})
url = SlackRTM.get_url(token: ENV['SLACK_TOKEN'])
client = SlackRTM::Client.new(websocket_url: url)
handle_message!(client)
client
end
def channels(opts = {})
opts{:clear_cache} ? (@channels = slack.channels_list['channels']) : (@channels ||= slack.channels_list['channels'])
end
def channel_name_to_id(name)
channels.find { |channel| channel['name'] == name }['id']
end
def resolve_channel_name(id, opts = {})
channel_info = channels.find { |channel| channel['id'] == id }
if opts[:light]
channel_info['name']
else
"<##{channel_info['id']}|#{channel_info['name']}>"
end
end
def post_message(channel = @channel_id_for_post, text)
slack.chat_postMessage(channel: channel, text: text, as_user: true)
end
def handle_message!(handled_rtm_client)
handled_rtm_client.on(:message) do |message|
begin
case message['type']
when 'channel_created'
channels('update_cache')
post_message("#{resolve_channel_name(message['channel']['id'])} っていうチャンネルができたみたいだけど……")
when 'channel_rename'
old_name = resolve_channel_name(message['channel']['id'], light: true)
channels(clear_cache: true)
new_name = resolve_channel_name(message['channel']['id'])
post_message("##{old_name}#{new_name}に変異したみたいだけどっ!")
when 'channel_archive'
channels(cleac_cache: true)
post_message("またひとつ、#{resolve_channel_name(message['channel'])} っていうチャンネルがアーカイブの世界に閉じ込められた")
when 'channel_deleted'
post_message("っ、#{resolve_channel_name(message['channel'])} っていうチャンネルの気配が消えた……")
when "bot_added"
post_message("なんだか、<@#{message['bot']['id']}> っていう新しい#{message['bot']['name']}の気配を感じる")
when "channel_unarchive"
channels(clear_chache: true)
post_message("墓地から#{resolve_channel_name(message['channel'])} っていうチャンネルを場に出す")
when "emoji_changed"
case message['subtype']
when "add"
post_message("うー、またわけのわからない :#{message['name']}: `#{message['name']}` っていう絵文字が増えたー")
when "remove"
names = message['names'].map { |n| ":#{n}:" }.join(' ')
post_message("#{names} っていう絵文字が消えたけど、別にいいよね")
end
end
rescue => e
post_message("なんかダメだった₍₍ (ง´・_・`)ว ⁾⁾\n#{message}\n#{e.report}")
end
end
end
end
while
channel_watcher = SlackChannelWatcher.new(POST_CHANNEL_NAME)
begin
channel_watcher.channels(clear_cache: true)
channel_watcher.post_message("おはよう")
channel_watcher.rtm_client.main_loop
rescue => e
puts e.report
channel_watcher.post_message("(๑°⌓°๑)")
end
channel_watcher.post_message("(๑˃̵ᴗ˂̵)و")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment