Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-exz committed Oct 2, 2023
1 parent 99344f5 commit 5a8964d
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 87 deletions.
61 changes: 3 additions & 58 deletions bot/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,48 +120,6 @@ def self.answer_delete_custom_text(client:, data:)
)
end

def self.call_of_duty(client:, data:)
duty = Duty.where(channel_id: data.channel).where(enabled: true).take!
slack_web_client = Slack::Web::Client.new
client_info = slack_web_client.users_info(user: data.user)
options = {}
options[:message_ts] = data.thread_ts || data.ts
options[:channel] = data.channel
message_info = slack_web_client.chat_getPermalink(options)
notification = NotifyOpsgenie.new

recipient = {}
if !duty.opsgenie_escalation_name.nil?
recipient['name'] = duty.opsgenie_escalation_name
recipient['type'] = 'escalation'
recipient['field_name'] = 'name'
elsif !duty.opsgenie_schedule_name.nil?
recipient['name'] = duty.opsgenie_schedule_name
recipient['type'] = 'schedule'
recipient['field_name'] = 'name'
else
recipient['name'] = duty.user.contacts
recipient['type'] = 'user'
recipient['field_name'] = 'username'
end

response = notification.send(recipient, client_info, message_info)

json_response = JSON.parse(response.body)

if !json_response['result'].nil?
reply = I18n.t('reply.opsgenie.text')
elsif !json_response['message'].nil?
reply = I18n.t('reply.opsgenie.error', message: json_response['message'])
end

client.say(
channel: data.channel,
text: reply,
thread_ts: data.thread_ts || data.ts
)
end

def self.duty_create(client:, data:, match:)
message_processor = MessageProcessor.new
slack_web_client = Slack::Web::Client.new
Expand Down Expand Up @@ -349,15 +307,6 @@ def self.set_user_on_duty(data:, client:, slack_user_id:)
)
end

def self.set_user_status(data:, client:, status:)
User.where(slack_user_id: data.user).update_all(status: status)
client.say(
channel: data.channel,
text: I18n.t('commands.user.status.configured.text', status: status),
thread_ts: data.thread_ts || data.ts
)
end

def self.who_is_on_duty(data:, client:)
duty = Duty.where(channel_id: data.channel).where(enabled: true).take!
client.say(
Expand Down Expand Up @@ -449,7 +398,7 @@ def self.watch(client:, data:, match:)
end

# don't reply on duty person messages
return if data.user == duty.user.slack_user_id
#return if data.user == duty.user.slack_user_id

# Answer if it is known problem
if data != nil and match != nil
Expand Down Expand Up @@ -493,12 +442,8 @@ def self.answer(time, duty)
reason = I18n.t('reply.reason.non-working-day.text')
end

if duty.user.status == 'lunch'
reason = I18n.t('commands.user.status.enabled.lunch')
end

if duty.user.status == 'holidays'
reason = I18n.t('commands.user.status.enabled.holidays')
unless duty.user.status.nil?
reason = I18n.t('commands.user.status.enabled.text', status: duty.user.status)
end

reason
Expand Down
4 changes: 4 additions & 0 deletions bot/commands/allow_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
end
end
47 changes: 47 additions & 0 deletions bot/commands/call_duty_person.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class CallDutyPerson
def self.call(client:, data:)
duty = Duty.where(channel_id: data.channel).where(enabled: true).take!
slack_web_client = Slack::Web::Client.new
client_info = slack_web_client.users_info(user: data.user)
options = {}
options[:message_ts] = data.thread_ts || data.ts
options[:channel] = data.channel
message_info = slack_web_client.chat_getPermalink(options)
notification = NotifyOpsgenie.new

recipient = {}
if !duty.opsgenie_escalation_name.nil?
recipient['name'] = duty.opsgenie_escalation_name
recipient['type'] = 'escalation'
recipient['field_name'] = 'name'
elsif !duty.opsgenie_schedule_name.nil?
recipient['name'] = duty.opsgenie_schedule_name
recipient['type'] = 'schedule'
recipient['field_name'] = 'name'
else
recipient['name'] = duty.user.contacts
recipient['type'] = 'user'
recipient['field_name'] = 'username'
end

response = notification.send(recipient, client_info, message_info)

json_response = JSON.parse(response.body)

if !json_response['result'].nil?
reply = I18n.t('reply.opsgenie.text')
elsif !json_response['message'].nil?
reply = I18n.t('reply.opsgenie.error', message: json_response['message'])
end

client.say(
channel: data.channel,
text: reply,
thread_ts: data.thread_ts || data.ts
)
end
end
end
end
20 changes: 12 additions & 8 deletions bot/commands/help.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
class Help
def self.exec(client:,data:)
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.help.text', version: Whoisondutytoday::Application::VERSION),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
module WhoIsOnDutyTodaySlackBotModule
module Commands
class Help
def self.call(client:,data:)
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.help.text', version: Whoisondutytoday::Application::VERSION),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
9 changes: 9 additions & 0 deletions bot/commands/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
end
end

require_relative 'help'
require_relative 'allow_list'
require_relative 'my_status'
require_relative 'call_duty_person'
19 changes: 19 additions & 0 deletions bot/commands/my_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class MyStatus
def self.call(data:, client:, match:)
status = match['expression']
if status.nil?
User.where(slack_user_id: data.user).delete_all
else
User.where(slack_user_id: data.user).update_all(status: status)
end
client.say(
channel: data.channel,
text: I18n.t('commands.user.status.configured.text', status: status),
thread_ts: data.thread_ts || data.ts
)
end
end
end
end
21 changes: 6 additions & 15 deletions bot/whoisondutytodayslackbot.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
require 'date'
require_relative 'commands'
require_relative 'commands/help'
require_relative 'commands/main'

class Router < SlackRubyBot::Bot
class WhoIsOnDutyTodaySlackBot < SlackRubyBot::Bot
SlackRubyBot::Client.logger.level = Logger::WARN

command 'help' do |client, data|
Help.exec(client: client, data: data)
WhoIsOnDutyTodaySlackBotModule::Commands::Help.call(client: client, data: data)
end

command 'call duty person' do |client, data|
Commands.call_of_duty(client: client, data: data)
WhoIsOnDutyTodaySlackBotModule::Commands::CallDutyPerson.call(client: client, data: data)
end

command 'my status lunch' do |client, data|
Commands.set_user_status(client: client, data: data, status: 'lunch')
end

command 'my status holidays' do |client, data|
Commands.set_user_status(client: client, data: data, status: 'holidays')
end

command 'my status work' do |client, data|
Commands.set_user_status(client: client, data: data, status: 'work')
command 'my status' do |client, data, match|
WhoIsOnDutyTodaySlackBotModule::Commands::MyStatus.call(client: client, data: data, match: match)
end

command 'i am on duty' do |client, data|
Expand Down
3 changes: 1 addition & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ en:
text: "User status configured `%{status}`"
enabled:
duty: "%{user} on duty today. Duty hours: from %{duty_from} to %{duty_to} (UTC)"
lunch: "Sorry, duty person on lunch."
holidays: "Sorry, duty person on holidays."
text: "Sorry, duty person on %{status}."
opsgenie-schedule-name:
text: "All duties in this channel synced with Opsgenie."
opsgenie-escalation-name:
Expand Down
3 changes: 1 addition & 2 deletions config/locales/hr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ hr:
text: "Status korisnika konfiguriran `%{status}`"
enabled:
duty: "%{user} danas na dužnosti."
lunch: "Oprosti, dežurna osoba na ručku."
holidays: "Oprosti, dežurna osoba u praznicima."
text: "Sorry, duty person on %{status}."
opsgenie-schedule-name:
text: ""
thread:
Expand Down
3 changes: 1 addition & 2 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ ru:
text: "Статус пользователя сконфигурирован: `%{status}`"
enabled:
duty: "%{user} сегодня дежурный."
lunch: "Извините, дежурный на обеде."
holidays: "Извините, дежурный на каникулах."
text: "Sorry, duty person on %{status}."
opsgenie-schedule-name:
text: ""
thread:
Expand Down

0 comments on commit 5a8964d

Please sign in to comment.