diff --git a/app/models/admission_code_message.rb b/app/models/admission_code_message.rb index 934042a..c5d9dbc 100644 --- a/app/models/admission_code_message.rb +++ b/app/models/admission_code_message.rb @@ -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 @@ -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 diff --git a/app/models/chat_message_sender.rb b/app/models/chat_message_sender.rb index 014beb0..fbe60cb 100644 --- a/app/models/chat_message_sender.rb +++ b/app/models/chat_message_sender.rb @@ -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 diff --git a/app/models/slack_message.rb b/app/models/slack_message.rb index 75e1a2a..bb8d103 100644 --- a/app/models/slack_message.rb +++ b/app/models/slack_message.rb @@ -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 diff --git a/config/messages.sample.yml b/config/messages.sample.yml index 70397e3..73a9de3 100644 --- a/config/messages.sample.yml +++ b/config/messages.sample.yml @@ -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:" diff --git a/config/messages.yml b/config/messages.yml index 5b4b27f..247dc8d 100644 --- a/config/messages.yml +++ b/config/messages.yml @@ -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:" diff --git a/sample.env b/sample.env index 4f1bd25..110bfe2 100644 --- a/sample.env +++ b/sample.env @@ -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="" @@ -11,4 +11,6 @@ MAIL_ADDRESS_HOST="" # 申請結果を受け取る Email Webhook 用のアドレス(ゲストに届くメール) MAIL_ADDRESS_WEBHOOK="" # 会社の電話番号(申請に必要) -COMPANY_TEL="03-1234-5678" \ No newline at end of file +COMPANY_TEL="03-1234-5678" +# 通知の送信先(CHANNEL or DM or BOTH) +SEND_MODE="" \ No newline at end of file diff --git a/spec/app/models/admission_code_message_spec.rb b/spec/app/models/admission_code_message_spec.rb index 4d69b84..8d71d81 100644 --- a/spec/app/models/admission_code_message_spec.rb +++ b/spec/app/models/admission_code_message_spec.rb @@ -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) } @@ -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") diff --git a/spec/app/models/slack_message_spec.rb b/spec/app/models/slack_message_spec.rb index a0c854f..0dd692a 100644 --- a/spec/app/models/slack_message_spec.rb +++ b/spec/app/models/slack_message_spec.rb @@ -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" }, @@ -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