Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

入館申請のQRコードの送信先を選択できるように修正しました #214

Merged
merged 9 commits into from
Jun 11, 2024
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
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
Loading