Last active
June 15, 2019 00:29
-
-
Save Tosainu/6b9eb76e56bdceaacb15 to your computer and use it in GitHub Desktop.
Slack Wandbox Bot https://twitter.com/myon___/status/690754708630204416
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'slack-ruby-client' | |
require 'wandbox' | |
require './config.rb' | |
Slack.configure do |conf| | |
conf.token = TOKEN | |
conf.logger = Logger.new(STDOUT) | |
conf.logger.level = Logger::WARN | |
end | |
client = Slack::RealTime::Client.new | |
client.on :hello do | |
puts "Successfully connected, welcome '#{client.self.name}' to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com." | |
end | |
client.on :message do |data| | |
case data.text | |
when "<@#{client.self.id}> help" then | |
client.message channel: data.channel, text: <<-EOS | |
<@#{data.user}> (✘╹◡╹)ノ できること | |
*プログラムの実行* | |
• `run code` : codeをC++として実行 | |
• `run:lang code` : codeをlangとして実行 | |
EOS | |
when /^run(?::([[:graph:]]+))?\s(.+)$/m then | |
client.typing channel: data.channel | |
code = Slack::Messages::Formatting.unescape(Regexp.last_match(2)) | |
lang = Regexp.last_match(1).nil? ? 'C++' : Regexp.last_match(1) | |
compiler = Wandbox.lang2compiler(lang) | |
if compiler.nil? | |
client.message channel: data.channel, | |
text: "`#{lang}` は対応してないです... (。・_・。✘)" | |
else | |
result = Wandbox.run compiler['name'], code | |
messages = [] | |
messages << if result['status'] == '0' | |
':tada: ∩(>◡<✘)∩' | |
else | |
':sweat_drops:ヾ(。>﹏<。✘)ノ゙' | |
end | |
messages << "lang: `#{lang}` compiler: `#{compiler['name']}`" | |
messages << "```\n#{code}\n```" | |
if !result['compiler_error'].nil? | |
messages << 'compiler_error:' | |
messages << ">>> #{result['compiler_error']}" | |
elsif !result['program_message'].nil? | |
messages << 'program_message:' | |
messages << ">>> #{result['program_message']}" | |
end | |
client.message channel: data.channel, text: messages.join("\n") | |
end | |
end | |
end | |
client.on :close do |_data| | |
puts 'Connection closed, exiting.' | |
end | |
client.start! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ruby:2.6-alpine | |
ADD bot.rb config.rb Gemfile /usr/src/app/ | |
WORKDIR /usr/src/app/ | |
RUN \ | |
apk upgrade --no-cache && \ | |
apk add --no-cache --virtual gem-dependencies \ | |
libgcc \ | |
libstdc++ \ | |
openssl-dev && \ | |
apk add --no-cache --virtual build-dependencies \ | |
build-base && \ | |
bundle install && \ | |
apk del build-dependencies | |
CMD ["bundle", "exec", "ruby", "bot.rb"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
# Slack | |
gem 'slack-ruby-client', '~> 0.11.0' | |
gem 'eventmachine' | |
gem 'faye-websocket' | |
gem 'wandbox' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment