Create a gist now

Instantly share code, notes, and snippets.

はんこbot
# -*- mode:ruby; coding:utf-8 -*-
require 'slack'
require 'httpclient'
require 'gyazo'
channelid = ENV['SLACK_CHANNELID']
Slack.configure {|config| config.token = ENV['SLACK_TOKEN'] }
slack = Slack.realtime
gyazo = Gyazo::Client.new ENV['GYAZO_TOKEN']
slack.on :hello do
puts 'Successfully connected.'
end
slack.on :message do |data|
#if data['subtype'] != 'bot_message'
if data['channel'] == channelid && data['subtype'] != 'bot_message'
params = {
channel: data['channel'],
username: '羽入',
icon_url: 'http://url/to/icon/imgage'
}
case data['text']
when '葡萄酒', '唐辛子'
params[:text]='あうあう'
Slack.chat_postMessage params
when /^はんこ[ ]+([\S\s]+)/
puts $1;
Dir.chdir('/home/hoge/makehanko/'){
hikisuu = ['makehanko.rb',$1]
IO.popen(['ruby', *hikisuu], "r") { |io|
io.close
}
}
image = File.expand_path('../makehanko/hanko.png',__FILE__)
if File.exist?(image) then
res = gyazo.upload image
params[:text]= res['url']
Slack.chat_postMessage params
File.unlink(image)
else
params[:text]= '画像の生成に失敗したのです……'
Slack.chat_postMessage params
end
else
end
end
end
slack.start
# coding: utf-8
require 'rmagick'
#settings
fontpath=File.expand_path("../fonts/ipaexm_tate.ttf", __FILE__)
outputpath=File.expand_path("../hanko.png", __FILE__)
syuiro='#EF4136'
string=ARGV[0]
#making image
baseimg = Magick::ImageList.new
baseimg.new_image(260,260) { self.background_color='white' }
img=baseimg
drawrec = Magick::Draw.new
drawrec.fill='transparent'
drawrec.stroke=syuiro
drawrec.stroke_width=3
drawrec.roundrectangle(5,5,255,255,10,10)
drawrec.draw(img)
drawstr = Magick::Draw.new
strlen=string.size
jigen=Math.sqrt(strlen).ceil
charsize=240.to_f/jigen
for gyou in 0..jigen-1 do
for retsu in 0..jigen-1 do
if gyou*jigen+retsu >= string.size then
break
end
drawstr.annotate(img,0,0,10+(gyou*charsize).round,10+(retsu*charsize).round, string[gyou*jigen+retsu].gsub('@', '\@')) {
self.font=fontpath
self.fill=syuiro
self.stroke='transparent'
self.pointsize=charsize.round
self.gravity=Magick::NorthEastGravity
#puts string[gyou*jigen+retsu].gsub("\\", "\\\\").gsub('@', '\@')
}
end
end
img.write(outputpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment