Skip to content

Commit

Permalink
add worker & notification
Browse files Browse the repository at this point in the history
  • Loading branch information
yamashush committed Aug 15, 2019
1 parent f6691a0 commit b6d3a40
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -9,4 +9,5 @@ gem 'http'
gem 'mechanize'
gem 'sidekiq'
gem 'sinatra'
gem 'slack-ruby-client'
gem 'slack-ruby-client'
gem 'config'
36 changes: 36 additions & 0 deletions Gemfile.lock
Expand Up @@ -9,9 +9,44 @@ GEM
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
concurrent-ruby (1.1.5)
config (2.0.0)
activesupport (>= 4.2)
deep_merge (~> 1.2, >= 1.2.1)
dry-schema (~> 1.0)
connection_pool (2.2.2)
deep_merge (1.2.1)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dry-configurable (0.8.3)
concurrent-ruby (~> 1.0)
dry-core (~> 0.4, >= 0.4.7)
dry-container (0.7.2)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.4.9)
concurrent-ruby (~> 1.0)
dry-equalizer (0.2.2)
dry-inflector (0.1.2)
dry-initializer (3.0.1)
dry-logic (1.0.3)
concurrent-ruby (~> 1.0)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-schema (1.3.3)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.8, >= 0.8.3)
dry-core (~> 0.4)
dry-equalizer (~> 0.2)
dry-initializer (~> 3.0)
dry-logic (~> 1.0)
dry-types (~> 1.0)
dry-types (1.1.1)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.2, >= 0.2.2)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.0, >= 1.0.2)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.13.1)
Expand Down Expand Up @@ -89,6 +124,7 @@ PLATFORMS
ruby

DEPENDENCIES
config
http
mechanize
sidekiq
Expand Down
3 changes: 2 additions & 1 deletion Procfile
@@ -1 +1,2 @@
web: bundle exec rackup config.ru -p $PORT
web: bundle exec rackup config.ru -p $PORT
worker: bundle exec sidekiq -C ./config/sidekiq.yml -r ./app/workers/reception_worker.rb
18 changes: 18 additions & 0 deletions app/controllers/slack_interactive_message.rb
@@ -0,0 +1,18 @@
require 'json'
require_relative '../workers/reception_worker'

module SlackInteractiveMessage
def run(request)

req = URI.decode_www_form(request.body.read)
payload = req.assoc('payload').last
slack_dialog = JSON.parse(payload)

# Slack のレスポンス3秒以内ルールのため非同期処理
ReceptionWorker.perform_async slack_dialog

return
end

module_function :run
end
83 changes: 83 additions & 0 deletions app/controllers/slack_notification.rb
@@ -0,0 +1,83 @@
require 'json'
require 'slack-ruby-client'

module SlackNotification
def run(request)

json = JSON.parse(request.body.read)
mail_body = json['body']
mail_body = mail_body.gsub("\\n", "")

slack_id = mail_body.match(/(?:To:【)(.+)(?:】)/)[1]
recept_name = mail_body.match(/(?:】)(.+)(?:\s様)/)[1]
recept_date = mail_body.match(/\d{4}\/\d{2}\/\d{2}.+\d{2}:\d{2}/)[0]
recept_id = mail_body.match(/(?:ID:)(\d+)(?:\s+\*)/)[1]

day_of_the_week_eg2jp = {
'Sun' => '日',
'Mon' => '月',
'Tue' => '火',
'Wed' => '水',
'Thu' => '木',
'Fri' => '金',
'Sat' => '土',
}
recept_date = recept_date.gsub(/([a-zA-Z]{3})/, day_of_the_week_eg2jp)

client = Slack::Web::Client.new(
token: ENV['SLACK_TOKEN']
)

messages = open('./config/messages.yml', 'r') { |f| YAML.load(f) }

res = client.chat_postMessage(
icon_emoji: messages['notification']['icon'],
channel: ENV['SLACK_CHANNEL'],
text: "<@#{slack_id}> #{messages['notification']['text_notification']}",
attachments: [
{
color: "#36a64f",
fields: [
{
title: messages['notification']['recept_name'],
value: "#{recept_name} 様",
short: true
},
{
title: messages['notification']['recept_datetime'],
value: recept_date,
short: true
},
{
title: messages['notification']['recept_id'],
value: recept_id,
short: true
}
]
}
]
)

barcode_url = "https://barcode.tec-it.com/barcode.ashx?data=#{recept_id}&code=Code128"
text_guide = messages['notification']['text_guide']
text_guide.gsub!('RECEPT_DATE', "#{recept_date}")
text_guide.gsub!('RECEPT_ID', "#{recept_id}\n#{barcode_url}")

res = client.chat_postMessage(
icon_emoji: ':office:',
channel: res.channel,
text: text_guide,
attachments: [
{
title: "バーコード/Barcode",
image_url: barcode_url,
}
],
thread_ts: res.ts
)

return
end

module_function :run
end
77 changes: 77 additions & 0 deletions app/workers/reception_worker.rb
@@ -0,0 +1,77 @@
require 'mechanize'
require 'sidekiq'
require 'slack-ruby-client'

class ReceptionWorker
include Sidekiq::Worker
sidekiq_options queue: :default, retry: false

def perform(slack_dialog)

# slack dialog input
recept_date = slack_dialog['submission']['date']
recept_time = slack_dialog['submission']['time']
recept_company_name = slack_dialog['submission']['company_name']
recept_visitor_name = slack_dialog['submission']['name']
slack_id = slack_dialog['user']['id']
slack_channel = slack_dialog['channel']['id']

# srd-gate login
agent = Mechanize.new
page = agent.get("https://srd-gate.com/03/login.cgi")
agent.page.form.field_with(name: 'userid').value = ENV['SRD_GATE_USERNAME']
agent.page.form.field_with(name: 'passwd').value = ENV['SRD_GATE_PASSWORD']
page = agent.page.form.submit

# srd-gate regist page
page = agent.get("https://srd-gate.com/03/sinsei.cgi")

# regist parameter
agent.page.form.field_with(name: 'raihoubi').value = recept_date
agent.page.form.field_with(name: 'raihoujikan').value = recept_time
agent.page.form.checkbox_with(name: 'seminarcheck').check # 団体受付可能フラグ
# slack id を会社名につけて登録結果メールに情報を引回す
# メール受信 webhook を受け取って、登録者にメンションするため
# 半角記号は使えない仕様なので削除する
agent.page.form.field_with(name: 'kaisha[]').value = '【' + slack_id + '】' + recept_company_name.gsub(/[[:punct:]]/, "")
agent.page.form.field_with(name: 'mei[]').value = recept_visitor_name.gsub(/[[:punct:]]/, "")
agent.page.form.field_with(name: 'kana[]').value = "カナ"
agent.page.form.field_with(name: 'mail[]').value = ENV['MAIL_ADDRESS_WEBHOOK']
agent.page.form.field_with(name: 'sinseiemail').value = ENV['MAIL_ADDRESS_HOST']
agent.page.form.field_with(name: 'sinseitel').value = ENV['COMPANY_TEL']

# regist
page = agent.page.form.submit

client = Slack::Web::Client.new(
token: ENV['SLACK_TOKEN']
)

messages = open('./config/messages.yml', 'r') { |f| YAML.load(f) }

client.chat_postEphemeral(
icon_emoji: messages['intarctive']['icon'],
channel: slack_channel,
user: slack_id,
text: messages['intarctive']['text_notification'],
attachments: [
{
color: "#439FE0",
fields: [
{
title: messages['intarctive']['recept_name'],
value: "#{recept_company_name} #{recept_visitor_name} 様",
short: true
},
{
title: messages['intarctive']['recept_datetime'],
value: "#{recept_date} #{recept_time}" ,
short: true
},
]
}
]
)

end
end
7 changes: 7 additions & 0 deletions config/initializers/sidekiq.rb
@@ -0,0 +1,7 @@
Sidekiq.configure_server do |config|
config.redis = { url: ENV['REDIS_PROVIDER'] }
end

Sidekiq.configure_client do |config|
config.redis = { url: ENV['REDIS_PROVIDER'] }
end
51 changes: 24 additions & 27 deletions config/messages.yml
@@ -1,45 +1,42 @@
common: &common
recept_date: 来訪日
recept_time: 訪問時間
recept_datetime: 訪問日時
recept_company: 来訪者会社名
recept_name: 来訪者名
user: dbuser
password: |-
recept_id: 入館ID
icon: ":office:"
dialog:
tilte: 入館受付申請
submit: 送信
recept_company_placeholder: 会社名がない場合は「面接」「なし」等
recept_name_placeholder: 「様」をつけると入館証が「様様」になるよ!
<<: *common
intarctive:
text_notification: "以下の内容で受け付けました。受け付け完了までしばらくお待ちください :pray:"
<<: *common
notification:
text_notification: "入館受付が完了しました :tada:"
text_guide: |- # RECEPT_DATE, RECEPT_ID は置換部分
```
◆日時
2019/08/21(水) 19:00
RECEPT_DATE
◆場所
〒106-6239
東京都港区六本木3-2-1
住友不動産六本木グランドタワー 39F
https://office.sumitomo-rd.co.jp/63/access.html
南北線「六本木一丁目駅」西改札直結
日比谷線・大江戸線「六本木駅」5番出口より徒歩5分
〒123-4567
東京都港区○○ 1-2-3
◆入館ID
https://barcode.tec-it.com/barcode.ashx?data=13110365176&code=Code128
RECEPT_ID
(URLクリックでバーコード画像が表示されます)
◆ビル入館方法
(1Fまたは4F)セキュリティゲート前の受付機にバーコードをかざしていただく、もしくは入館IDを入力して下さい。
①セキュリティゲート前の受付機にバーコードをかざしていただく、もしくは入館IDを入力して下さい。
②受付機より「入館証」が発券されます。
(人数分の発券が必要になります)
③入館証のQRコードをゲートにかざし入館します。
④ゲート右手のシャトルエレベータで29Fへ。
⑤29Fからは36-40F停車のエレベーターにて39Fへお越しください。
⑥SmartHR受付のiPadより担当者をお呼び出しください。
◆SmartHR新オフィスの行き方
https://shanaiho.smarthr.co.jp/n/n901fe6fc0e28
※注意点
「入館証」はご退館時まで必要です。大切にお持ち下さい。
dialog:

tilte: 入館受付申請
submit: 送信
recept_company_placeholder: 会社名がない場合は「面接」「なし」等
recept_name_placeholder: 「様」をつけると入館証が「様様」になるよ!
<<: *common

#data = open('./config/message.yml', 'r') { |f| YAML.load(f) }
```
<<: *common
3 changes: 3 additions & 0 deletions config/sidekiq.yml
@@ -0,0 +1,3 @@
:concurrency: 5
:queues:
- default

0 comments on commit b6d3a40

Please sign in to comment.