Skip to content

Commit

Permalink
Merge pull request #214 from shigerix/change_send_to
Browse files Browse the repository at this point in the history
入館申請のQRコードの送信先を選択できるように修正しました
  • Loading branch information
shigerix authored Jun 11, 2024
2 parents 3d156c5 + d7edb43 commit a973da3
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 6 deletions.
23 changes: 22 additions & 1 deletion app/models/admission_code_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ def initialize(email)
end

def post
ChatMessageSender.new.post_public_message(api_post_body)
chat_message_sender = ChatMessageSender.new
res = chat_message_sender.post_public_message(api_post_body) if send_to_channel?
res = chat_message_sender.post_public_message(api_post_body_direct_message) if send_to_direct_message?
res
end

private

def send_to_channel?
%w[CHANNEL BOTH].include?(ENV.fetch("SEND_MODE"))
end

def send_to_direct_message?
%w[DM BOTH].include?(ENV.fetch("SEND_MODE"))
end

# @return [Hash] postMessage API post body
# @see https://api.slack.com/methods/chat.postMessage
# @see https://github.com/slack-ruby/slack-ruby-client/blob/master/lib/slack/web/api/endpoints/chat.rb
Expand All @@ -31,6 +42,16 @@ def api_post_body
] }
end

def api_post_body_direct_message
{ icon_emoji: MESSAGES["notification"]["icon"],
channel: @email.slack_id,
text: "<@#{@email.slack_id}> #{MESSAGES['notification']['text_notification']}",
attachments: [
color: "good",
fields: attachment_fields
] }
end

# @return [Array] attachment_field array
# @private
def attachment_fields
Expand Down
4 changes: 4 additions & 0 deletions app/models/chat_message_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def post_public_message(post_body)
@slack_api_client.chat_postMessage(post_body)
end

def post_direct_message(post_body)
@slack_api_client.chat_postMessage(post_body)
end

# 特定のユーザーだけに見える形で投稿
# @param post_body [Hash]
# @see https://api.slack.com/methods/chat.postEphemeral
Expand Down
16 changes: 15 additions & 1 deletion app/models/slack_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@ def received_message_post_body
{ icon_emoji: MESSAGES["intarctive"]["icon"],
channel: @dialog_submission.slack_channel_id,
user: @dialog_submission.slack_user_id,
text: MESSAGES["intarctive"]["text_notification"],
text:,
attachments: [attachment(fields: received_message_attachment_fields)] }
end

def text
if send_to_direct_message?
MESSAGES["intarctive"]["dm_text_notification"]
else
MESSAGES["intarctive"]["text_notification"]
end
end

# @private
# @return [Boolean]
def send_to_direct_message?
%w[DM BOTH].include?(ENV.fetch("SEND_MODE"))
end

# @return [Array] attachment_field array
# @private
def received_message_attachment_fields
Expand Down
1 change: 1 addition & 0 deletions config/messages.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dialog:
<<: *common
intarctive:
text_notification: "以下の内容で受け付けました。受け付け完了までしばらくお待ちください :pray:"
dm_text_notification: "以下の内容で受け付けました。受け付け完了までしばらくお待ちください :pray: \n受付が完了すると入館IDとバーコードがslackbotで届きます:mailbox_with_mail:"
<<: *common
notification:
text_notification: "入館受付が完了しました :tada:"
Expand Down
1 change: 1 addition & 0 deletions config/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dialog:
<<: *common
intarctive:
text_notification: "以下の内容で受け付けました。受け付け完了までしばらくお待ちください :pray:"
dm_text_notification: "以下の内容で受け付けました。受け付け完了までしばらくお待ちください :pray: \n受付が完了すると入館IDとバーコードがslackbotで届きます:mailbox_with_mail:"
<<: *common
notification:
text_notification: "入館受付が完了しました :tada:"
Expand Down
6 changes: 4 additions & 2 deletions sample.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Slack App OAuth Access Token
SLACK_TOKEN='xoxp-'
# Slack への通知チャンネル (only public)
# Slack への通知チャンネル
SLACK_CHANNEL='#reception'
# srd-gate.com の認証ユーザー
SRD_GATE_USERNAME=""
Expand All @@ -11,4 +11,6 @@ MAIL_ADDRESS_HOST=""
# 申請結果を受け取る Email Webhook 用のアドレス(ゲストに届くメール)
MAIL_ADDRESS_WEBHOOK=""
# 会社の電話番号(申請に必要)
COMPANY_TEL="03-1234-5678"
COMPANY_TEL="03-1234-5678"
# 通知の送信先(CHANNEL or DM or BOTH)
SEND_MODE=""
45 changes: 45 additions & 0 deletions spec/app/models/admission_code_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "spec_helper"
require_relative "../../../app/models/email"
require_relative "../../../app/models/admission_code_message"
require_relative "../../../app/models/chat_message_sender"

describe AdmissionCodeMessage do
let(:instance) { described_class.new(email) }
Expand Down Expand Up @@ -30,6 +31,50 @@
EMAIL_BODY
end

describe "#post" do
context "SEND_MODE が CHANNEL の場合" do
it "post_public_message が1回呼ばれること" do
allow(ENV).to receive(:fetch).with("SEND_MODE").and_return("CHANNEL")
allow(ENV).to receive(:fetch).with("SLACK_CHANNEL").and_return("CH15TJXEX")
chat_message_sender = instance_double(ChatMessageSender)
allow(ChatMessageSender).to receive(:new).and_return(chat_message_sender)
allow(chat_message_sender).to receive(:post_public_message)

instance.post

expect(chat_message_sender).to have_received(:post_public_message).once
end
end

context "SEND_MODE が DM の場合" do
it "post_direct_message が1回呼ばれること" do
allow(ENV).to receive(:fetch).with("SEND_MODE").and_return("DM")
allow(ENV).to receive(:fetch).with("SLACK_CHANNEL").and_return("CH15TJXEX")
chat_message_sender = instance_double(ChatMessageSender)
allow(ChatMessageSender).to receive(:new).and_return(chat_message_sender)
allow(chat_message_sender).to receive(:post_public_message)

instance.post

expect(chat_message_sender).to have_received(:post_public_message).once
end
end

context "SEND_MODE が BOTH の場合" do
it "post_public_message が2回呼ばれること" do
allow(ENV).to receive(:fetch).with("SEND_MODE").and_return("BOTH")
allow(ENV).to receive(:fetch).with("SLACK_CHANNEL").and_return("CH15TJXEX")
chat_message_sender = instance_double(ChatMessageSender)
allow(ChatMessageSender).to receive(:new).and_return(chat_message_sender)
allow(chat_message_sender).to receive(:post_public_message)

instance.post

expect(chat_message_sender).to have_received(:post_public_message).twice
end
end
end

describe "#api_post_body" do
before do
allow(ENV).to receive(:fetch).with("SLACK_CHANNEL").and_return("CH15TJXEX")
Expand Down
31 changes: 29 additions & 2 deletions spec/app/models/slack_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
let(:instance) { described_class.new }

describe "#received_message_post_body" do
context "ok" do
it do
context "チャンネルに通知する場合" do
it "適切なメッセージが通知されること" do
allow(ENV).to receive(:fetch).with("SEND_MODE").and_return("CHANNEL")
modal_submit_fixture = { type: "dialog_submission",
user: { id: "UCKTXCBRB" },
channel: { id: "CH15TJXEX" },
Expand All @@ -35,6 +36,32 @@
end
end
end
context "DMに通知する場合" do
it "適切なメッセージが通知されること" do
allow(ENV).to receive(:fetch).with("SEND_MODE").and_return("DM")
modal_submit_fixture = { type: "dialog_submission",
user: { id: "UCKTXCBRB" },
channel: { id: "CH15TJXEX" },
submission: { date: "2023/01/01",
time: "08:00",
company_name: "SmartHR",
name: "須磨 英知" } }
dialog_submission = SlackDialogSubmission.new(modal_submit_fixture)
instance = described_class.new(dialog_submission:)

expected = {
channel: "CH15TJXEX",
icon_emoji: ":office:",
text: "以下の内容で受け付けました。受け付け完了までしばらくお待ちください :pray: \n受付が完了すると入館IDとバーコードがslackbotで届きます:mailbox_with_mail:",
user: "UCKTXCBRB",
attachments: [{
color: "good",
fields: instance.send(:received_message_attachment_fields)
}]
}
expect(instance.send(:received_message_post_body)).to eq expected
end
end
describe "#received_message_attachment_fields" do
context "ok" do
it do
Expand Down

0 comments on commit a973da3

Please sign in to comment.