Skip to content

Commit

Permalink
0.19.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-exz committed Jan 6, 2025
1 parent 9263536 commit b647f7d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Changelog
## 0.19.4
### Bugfixes
- Fixing hide reason feature
- Code cleanup
- Fixing answer at non-working hours and working hours
- Fixing problems actions with nil values

## 0.19.3
### Bugfixes
- Deleted not used code
Expand Down
78 changes: 51 additions & 27 deletions bot/commands/other.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ class Other
def self.call(client:, data:, match:)
log_event("Incoming data: #{data}")
message_processor = MessageProcessor.new
time = DateTime.strptime(data.ts, '%s')

# skip processing events and data without client_msg_id
return if (data.respond_to?(:client_msg_id) == false) && (data.respond_to?(:files) == false)

begin
channel = Channel.where(slack_channel_id: data.channel).first
return if channel.nil?

duty = Duty.where(channel_id: data.channel, enabled: true).first
reason = determine_reason(duty)

# store messages where reminder needed
if channel.reminder_enabled == true
Expand All @@ -29,7 +30,7 @@ def self.call(client:, data:, match:)

# Answer if it is known problem
if !data.nil? && !match.nil?
Action.where(channel: data.channel).each do |action|
Action.where(channel: data.channel).where.not(problem: nil).each do |action|
/#{action.problem}/i.match(data.text) do |_|
reply_to_known_problem(client: client, problem: action.problem, data: data, action: action.action)
end
Expand All @@ -39,38 +40,31 @@ def self.call(client:, data:, match:)
# check if message written in channel
if data.respond_to?(:thread_ts) == false
message_processor.collectUserInfo(data: data)
reason = self.answer(time, duty)
auto_answer_at_working_time(client, channel, data)
reply_in_not_working_time(client, reason, data, channel) unless reason.nil?
send_tagged_message(client, channel, data)
return
end

# check if message written in thread without answer from bot
message = Message.where('ts=? OR thread_ts=?', data.thread_ts, data.thread_ts).where(reply_counter: 1)
if message.blank?
reason = self.answer(time, duty)
auto_answer_at_working_time(client, channel, data)
reply_in_not_working_time(client, reason, data, channel) unless reason.nil?
send_tagged_message(client, channel, data)
handle_message(client, channel, data, reason)
else
# check if message written in thread without answer from bot
message = Message.where('ts=? OR thread_ts=?', data.thread_ts, data.thread_ts).where(reply_counter: 1)
handle_message(client, channel, data, reason) if message.blank?
end
rescue StandardError => e
print e
end
end

def self.reply_in_not_working_time(client, reason, data, channel)
def self.reply_in_not_working_time(client, reason, data)
answer = Answer.where(channel_id: data.channel, answer_type: 'non_working_time').first

if answer.nil?
text = I18n.t('reply.non-working-time.text', name: client.self.name)
else
text = answer.body
reason = '' if answer.hide_reason == 1
if answer.hide_reason
reason[:text] = ''
end
end

client.web_client.chat_postMessage(
text: '%s' % reason,
text: '%s' % reason[:text],
channel: data.channel,
attachments: [
{
Expand Down Expand Up @@ -105,21 +99,31 @@ def self.reply_to_known_problem(client:, problem:, data:, action:)
message_processor.save_message(data: data)
end

def self.answer(time, duty)
reason = nil
def self.determine_reason(duty)
reason = {}
current_time = Time.now

if (time.utc.strftime('%H%M%S%N') < duty.duty_from.utc.strftime('%H%M%S%N')) || (time.utc.strftime('%H%M%S%N') > duty.duty_to.utc.strftime('%H%M%S%N'))
if working_time?(current_time, duty)
reason[:type] = 'working_hours'
reason[:text] = ''
else
from_time = duty.duty_from.utc.strftime('%H:%M').to_s
to_time = duty.duty_to.utc.strftime('%H:%M').to_s
current_time = time.utc.strftime('%H:%M').to_s
reason = I18n.t('reply.reason.non-working-hours.text', fT: from_time, tT: to_time, cT: current_time)
reason[:type] = 'non_working_hours'
reason[:text] =
I18n.t('reply.reason.non-working-hours.text', fT: from_time, tT: to_time,
cT: current_time.utc.strftime('%H:%M').to_s)
end

unless duty.duty_days.split(',').include?(time.utc.strftime('%u'))
reason = I18n.t('reply.reason.non-working-day.text')
unless duty.duty_days.split(',').include?(current_time.utc.strftime('%u'))
reason[:type] = 'non_working_day'
reason[:text] = I18n.t('reply.reason.non-working-day.text')
end

reason = I18n.t('commands.user.status.enabled.text', status: duty.user.status) unless duty.user.status.nil?
if duty.user&.status
reason[:type] = 'user_status'
reason[:text] = I18n.t('commands.user.status.enabled.text', status: duty.user.status)
end

reason
end
Expand Down Expand Up @@ -152,10 +156,30 @@ def self.auto_answer_at_working_time(client, channel, data)
end
end
end

def self.log_event(message)
logger = Logger.new(STDOUT)
logger.info(message)
end

def self.working_time?(current_time, duty)
# Check if the current time is within the working hours
if current_time.utc.strftime('%H%M%S%N') < duty.duty_from.utc.strftime('%H%M%S%N') || current_time.utc.strftime('%H%M%S%N') > duty.duty_to.utc.strftime('%H%M%S%N')
return false
end

true
end

def self.handle_message(client, channel, data, reason)
auto_answer_at_working_time(client, channel, data) if reason[:type] == 'working_hours'

if %w[non_working_hours non_working_day user_status].include?(reason[:type])
reply_in_not_working_time(client, reason, data)
end

send_tagged_message(client, channel, data)
end
end
end
end
15 changes: 3 additions & 12 deletions lib/message_processor/message_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@ def initialize
end

def collectUserInfo(data:)
user = User.where(slack_user_id: data.user).first
if user.blank?
user_info = @slack_web_client.users_info(user: data.user)

user = User.new
user.slack_user_id = user_info["user"]["id"]
user_info = @slack_web_client.users_info(user: data.user)
user = User.find_or_initialize_by(slack_user_id: user_info["user"]["id"])
if user.new_record? || user.updated_at < 1.day.ago
user.name = user_info["user"]["name"]
user.real_name = user_info["user"]["real_name"]
user.tz = user_info["user"]["tz"]
user.tz_offset = user_info["user"]["tz_offset"]
user.contacts = user_info["user"]["profile"]["email"]
user.save
elsif user.updated_at < 1.day.ago
user_info = @slack_web_client.users_info(user: data.user)
user.tz = user_info["user"]["tz"]
user.tz_offset = user_info["user"]["tz_offset"]
user.updated_at = 1.minutes.ago
user.save
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/tasks/reminder.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace :reminder do
channels.each do |channel|
begin
duty = Duty.where(channel_id: channel.slack_channel_id).where(enabled: true).take!
time = DateTime.now
reason = WhoIsOnDutyTodaySlackBotModule::Commands::Other.answer(time, duty)
reason = WhoIsOnDutyTodaySlackBotModule::Commands::Other.determine_reason(duty)
unless reason.nil?
p "Reason to skip reminder:#{reason}"
next
Expand Down

0 comments on commit b647f7d

Please sign in to comment.