はじめに
TwitterでPaizaさんが提供されてるソースコードを実行するものをMastodonでもやってみたくて試したものになります。
なお、C++のソースコードを実行したパターンしか試していないので他な言語の場合は想定していないです。
実際のコード
Gemfile
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'mastodon-api', git: 'https://github.com/tootsuite/mastodon-api.git', ref: '189deb8'
gem 'dotenv'
gem 'wandbox'
wandbox.rb
require 'bundler/setup'
Bundler.require(:default)
require 'open3'
require 'mastodon'
stream = Mastodon::Streaming::Client.new(base_url: <MastodonのURL>, bearer_token: <アクセストークン>)
client = Mastodon::REST::Client.new(base_url: <MastodonのURL>, bearer_token: <アクセストークン>)
URL = <MastodonのURL>
name = <MastodonのアカウントID>
stream.user() do |toot|
if toot.content.to_s =~ /wandbox/ then
str = toot.content.to_s
str.gsub!(/<\/p><p>/, '<br>')
str.gsub!(/<p>|<\/p>|wandbox|/, '')
str.gsub!(/<a href=\"<MastodonのURL>\/tags\/include\" class=\"mention hashtag\" rel=\"tag\">|<span>|<\/span>|<\/a>/, '')
str.gsub!(/<br>|<br \/>/, '\n')
str.gsub!(/</, '<')
str.gsub!(/>/, '>')
str.gsub!(/"/, '"')
str.gsub!(/&/, '&')
source = str.split('\n')
File.open("wandbox.cpp", "w+") do |file|
source.each do |line|
file.puts(line)
end
end
s = Open3.capture3("wandbox run wandbox.cpp --compiler=clang-head")
p s[0]
response = client.create_status("[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ\n#{s[0]}")
end
end
ソースコードはこれだけ。
やったことといえばTootの成型とOpen3.capture3
でwandboxでの実行結果を受け取ってるくらい。
おわりに
これうまいこと使えばソースコードを実行できるBotとか作れそうとか思った。
そこまでの気力はないので、だれか作らないかなぁ......|д゚)
やるならQiitadon辺りで実装すると面白いんではないかな?
参考資料
CLI から Wandbox を利用してソースファイルを実行する gem をつくった
Rubyで外部コマンドを実行して結果を受け取る方法あれこれ