Created
November 12, 2014 04:10
-
-
Save mitsuhirookuno/383f84ffaf114ab7278a to your computer and use it in GitHub Desktop.
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 'faraday' | |
require 'json' | |
require 'yaml' | |
require 'pry' | |
require 'pp' | |
Setting = YAML.load_file './main.yml' | |
Setting['ChatWork']['OrderTemplate'] = Setting['ChatWork']['OrderTemplate'] + Setting['ChatWork']['PairOrderTemplate'].combination(2).collect {|arr| arr.join(",") } | |
def main | |
order_hash = {} | |
user_hash = {} | |
warning_list = [] | |
conn = Faraday.new(url: Setting['ChatWork']['Url']) do |faraday| | |
faraday.response :logger # log requests to STDOUT | |
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP | |
end | |
# Get Tasl | |
response = conn.get do |r| | |
r.url '/v1/rooms/%s/tasks?status=open&account_id=%s' % [ Setting['ChatWork']['Room'], Setting['ChatWork']['AssignedByAccount'] ] # GET http://sushi.com/nigiri/sake.json | |
r.headers['X-ChatWorkToken'] = Setting['ChatWork']['Token'] | |
end | |
exit if response.body.size.zero? | |
task_list = JSON.parse(response.body) | |
pp task_list | |
# Validate Task | |
task_list.each do |task| | |
user_hash.store( task['assigned_by_account']['account_id'], task['assigned_by_account']['name'] ) | |
unless Setting['ChatWork']['OrderTemplate'].include?(task['body']) | |
warning_list << "[To:%s] %s さん 注文【%s】 を、私は解釈出来ません。力足らずごめんなさい。" % [ task['assigned_by_account']['account_id'], user_hash[task['assigned_by_account']['account_id']], task['body'] ] | |
next | |
end | |
if order_hash.key?(task['body']) | |
order_hash[task['body']] << task['assigned_by_account']['account_id'] | |
else | |
order_hash[task['body']] = [task['assigned_by_account']['account_id']] | |
end | |
end | |
# Show Status | |
message = '[info]' | |
order_hash.each do |menu,queue| | |
message << "[title]%s(%d)[/title] %s" % [ menu, queue.size, queue.map{|r| "[picon:#{r}]" }.join("") ] | |
end | |
message << '[/info]' | |
unless warning_list.size.zero? | |
message << "\n[hr]Warning[hr]\n" | |
message << warning_list.join("\n") | |
message << "\n[info][title]オーダー可能なメニュー(こちらをコピーしてください)[/title]%s[/info]" % Setting['ChatWork']['OrderTemplate'].join("\n") | |
end | |
print message | |
response = conn.post do |r| | |
r.url '/v1/rooms/%s/messages' % Setting['ChatWork']['Room'] # GET http://sushi.com/nigiri/sake.json | |
r.headers['X-ChatWorkToken'] = Setting['ChatWork']['Token'] | |
r.params[:body] = '[info]%s[/info]' % message | |
end | |
end | |
main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment