From abe9730dc713c24fd350827cefe6ec23ba1102d6 Mon Sep 17 00:00:00 2001 From: merefield Date: Thu, 4 Apr 2024 17:25:58 +0100 Subject: [PATCH 1/9] FEATURE: escalate breaches of quota to escalation group --- app/jobs/regular/chatbot_reply.rb | 34 +++++++++++++++++++++++++++++++ config/locales/server.en.yml | 5 +++++ config/settings.yml | 12 +++++++++++ plugin.rb | 3 +++ 4 files changed, 54 insertions(+) diff --git a/app/jobs/regular/chatbot_reply.rb b/app/jobs/regular/chatbot_reply.rb index 9a701ffc..897c5b28 100644 --- a/app/jobs/regular/chatbot_reply.rb +++ b/app/jobs/regular/chatbot_reply.rb @@ -46,6 +46,14 @@ def execute(opts) if over_quota reply_and_thoughts[:reply] = I18n.t('chatbot.errors.overquota') + + escalation_date = UserCustomField.find_by(name: ::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_FIELD, user_id: opts[:user_id]) + + if !SiteSetting.chatbot_quota_reach_escalation_cool_down_period.nil? && + escalation_date.value < SiteSetting.chatbot_quota_reach_escalation_cool_down_period.days.ago + escalate_quota_breach(opts[:user_id]) + end + elsif type == ::DiscourseChatbot::POST && post is_private_msg = post.topic.private_message? opts.merge!(is_private_msg: is_private_msg) @@ -149,4 +157,30 @@ def execute(opts) end reply_creator.create end + + def escalate_quota_breach(user_id) + user = User.find_by(id: user_id) + + target_group_names = [] + + Array(SiteSetting.chatbot_quota_reach_escalation_groups).each do |g| + target_group_names << Group.find(g.to_i).name + end + + target_group_names = target_group_names.join(",") + + default_opts = { + post_alert_options: { skip_send_email: true }, + raw: I18n.t("chatbot.quota_reached.escalation_message", user: user.username), + skip_validations: true, + title: I18n.t("chatbot.quota_reached.title"), + archetype: Archetype.private_message, + target_group_names: target_group_names + } + + post = PostCreator.create!(current_user, default_opts) + + user.custom_fields[::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_FIELD] = Date.now + user.save_custom_fields + end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index bc54070c..984da133 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -40,6 +40,8 @@ en: chatbot_quota_high_trust: "The allowed number of bot responses allowed by prompting user per week - high trust groups" chatbot_quota_medium_trust: "The allowed number of bot responses allowed by prompting user per week - medium trust groups" chatbot_quota_low_trust: "The allowed number of bot responses allowed by prompting user per week - low trust groups" + chatbot_quota_reach_escalation_group: "The group to which the bot will escalate when the quota is reached" + chatbot_quota_reach_escalation_cool_down_period: "The number of days the bot will wait before escalating again after reaching the quota" chatbot_high_trust_groups: "Groups considered high trust for bot interaction" chatbot_medium_trust_groups: "Groups considered medium trust for bot interaction" chatbot_low_trust_groups: "Groups considered low trust for bot interaction" @@ -73,6 +75,9 @@ en: pm_prefix: "A Discussion with Chatbot" quick_access_kick_off: announcement: "Hello, how can I help you?" + quota_reached: + escalation_title: "Chatbot Quota Reached: %{username}" + escalation_message: "I'm afraid %{username} has reached their weekly quota for bot interactions. I'm escalating this issue to staff for further assistance." prompt: system: basic: diff --git a/config/settings.yml b/config/settings.yml index 8632e9bc..1e07dca9 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -179,6 +179,18 @@ plugins: chatbot_quota_low_trust: default: 10 client: false + chatbot_quota_reach_escalation_groups: + client: false + type: group_list + list_type: compact + default: "" + allow_any: false + refresh: true + chatbot_quota_reach_escalation_cool_down_period: + client: false + default: 1 + min: 0 + max: 7 chatbot_max_look_behind: default: 5 max: 160 diff --git a/plugin.rb b/plugin.rb index a5a4cd6f..13215b7a 100644 --- a/plugin.rb +++ b/plugin.rb @@ -22,6 +22,7 @@ module ::DiscourseChatbot POST = "post" MESSAGE = "message" CHATBOT_QUERIES_CUSTOM_FIELD = "chatbot_queries" + CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_FIELD = "chatbot_queries_quota_reach_escalation_date" POST_TYPES_REGULAR_ONLY = [1] POST_TYPES_INC_WHISPERS = [1, 4] @@ -111,6 +112,8 @@ def progress_debug_message(message) register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_CUSTOM_FIELD, :integer) + register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_FIELD, :date) + add_to_serializer(:current_user, :chatbot_access) do !::DiscourseChatbot::EventEvaluation.new.trust_level(object.id).blank? From 01b82e84ca7a09a6152cdff06bb0cf65bfc9b6f0 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 17:18:46 +0100 Subject: [PATCH 2/9] improve --- .../discourse_chatbot/chatbot_controller.rb | 9 ++-- app/jobs/regular/chatbot_reply.rb | 33 --------------- app/jobs/scheduled/chatbot_quota_reset.rb | 4 ++ config/locales/server.en.yml | 5 ++- lib/discourse_chatbot/event_evaluation.rb | 42 ++++++++++++++++++- lib/discourse_chatbot/post/post_evaluation.rb | 2 +- plugin.rb | 4 +- 7 files changed, 57 insertions(+), 42 deletions(-) diff --git a/app/controllers/discourse_chatbot/chatbot_controller.rb b/app/controllers/discourse_chatbot/chatbot_controller.rb index e3af491a..6789a1e7 100644 --- a/app/controllers/discourse_chatbot/chatbot_controller.rb +++ b/app/controllers/discourse_chatbot/chatbot_controller.rb @@ -15,6 +15,9 @@ def start_bot_convo bot_user = ::User.find_by(username: bot_username) channel_type = SiteSetting.chatbot_quick_access_talk_button + evaluation = ::DiscourseChatbot::EventEvaluation.new + over_quota = evaluation.over_quota(current_user.id) + if channel_type == "chat" bot_author = ::User.find_by(username: SiteSetting.chatbot_bot_user) @@ -37,11 +40,11 @@ def start_bot_convo last_chat = ::Chat::Message.find_by(id: chat_channel.latest_not_deleted_message_id) - if (last_chat && last_chat.message != I18n.t("chatbot.quick_access_kick_off.announcement")) || last_chat.nil? + if (last_chat && (over_quota && last_chat.message != I18n.t('chatbot.errors.overquota') || !over_quota && last_chat.message != I18n.t("chatbot.quick_access_kick_off.announcement"))) || last_chat.nil? Chat::CreateMessage.call( chat_channel_id: chat_channel_id, guardian: guardian, - message: I18n.t("chatbot.quick_access_kick_off.announcement"), + message: over_quota ? I18n.t('chatbot.errors.overquota') : I18n.t("chatbot.quick_access_kick_off.announcement"), ) end @@ -50,7 +53,7 @@ def start_bot_convo elsif channel_type == "personal message" default_opts = { post_alert_options: { skip_send_email: true }, - raw: I18n.t("chatbot.quick_access_kick_off.announcement"), + raw: over_quota ? I18n.t('chatbot.errors.overquota') : I18n.t("chatbot.quick_access_kick_off.announcement"), skip_validations: true, title: I18n.t("chatbot.pm_prefix"), archetype: Archetype.private_message, diff --git a/app/jobs/regular/chatbot_reply.rb b/app/jobs/regular/chatbot_reply.rb index 897c5b28..466839f1 100644 --- a/app/jobs/regular/chatbot_reply.rb +++ b/app/jobs/regular/chatbot_reply.rb @@ -47,13 +47,6 @@ def execute(opts) if over_quota reply_and_thoughts[:reply] = I18n.t('chatbot.errors.overquota') - escalation_date = UserCustomField.find_by(name: ::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_FIELD, user_id: opts[:user_id]) - - if !SiteSetting.chatbot_quota_reach_escalation_cool_down_period.nil? && - escalation_date.value < SiteSetting.chatbot_quota_reach_escalation_cool_down_period.days.ago - escalate_quota_breach(opts[:user_id]) - end - elsif type == ::DiscourseChatbot::POST && post is_private_msg = post.topic.private_message? opts.merge!(is_private_msg: is_private_msg) @@ -157,30 +150,4 @@ def execute(opts) end reply_creator.create end - - def escalate_quota_breach(user_id) - user = User.find_by(id: user_id) - - target_group_names = [] - - Array(SiteSetting.chatbot_quota_reach_escalation_groups).each do |g| - target_group_names << Group.find(g.to_i).name - end - - target_group_names = target_group_names.join(",") - - default_opts = { - post_alert_options: { skip_send_email: true }, - raw: I18n.t("chatbot.quota_reached.escalation_message", user: user.username), - skip_validations: true, - title: I18n.t("chatbot.quota_reached.title"), - archetype: Archetype.private_message, - target_group_names: target_group_names - } - - post = PostCreator.create!(current_user, default_opts) - - user.custom_fields[::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_FIELD] = Date.now - user.save_custom_fields - end end diff --git a/app/jobs/scheduled/chatbot_quota_reset.rb b/app/jobs/scheduled/chatbot_quota_reset.rb index 6c128723..765db902 100644 --- a/app/jobs/scheduled/chatbot_quota_reset.rb +++ b/app/jobs/scheduled/chatbot_quota_reset.rb @@ -14,6 +14,10 @@ def execute(args) current_queries = "0" UserCustomField.create!(user_id: u.id, name: ::DiscourseChatbot::CHATBOT_QUERIES_CUSTOM_FIELD, value: current_queries) end + + if current_record = UserCustomField.find_by(user_id: u.id, name: ::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD) + current_record.delete + end end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 984da133..c6f87f37 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -76,8 +76,9 @@ en: quick_access_kick_off: announcement: "Hello, how can I help you?" quota_reached: - escalation_title: "Chatbot Quota Reached: %{username}" - escalation_message: "I'm afraid %{username} has reached their weekly quota for bot interactions. I'm escalating this issue to staff for further assistance." + escalation: + title: "Chatbot Quota Reached: %{username}" + message: "I'm afraid user '%{username}' has reached their weekly quota for Chatbot interactions. I'm escalating this issue to staff for further assistance." prompt: system: basic: diff --git a/lib/discourse_chatbot/event_evaluation.rb b/lib/discourse_chatbot/event_evaluation.rb index 852b5086..b3ce0f06 100644 --- a/lib/discourse_chatbot/event_evaluation.rb +++ b/lib/discourse_chatbot/event_evaluation.rb @@ -54,7 +54,47 @@ def over_quota(user_id) UserCustomField.create!(user_id: user_id, name: CHATBOT_QUERIES_CUSTOM_FIELD, value: current_queries) end - current_queries > max_quota + breach = current_queries > max_quota + escalate_as_required(user_id) if breach + breach + end + + def escalate_as_required(user_id) + escalation_date = UserCustomField.find_by(name: ::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD, user_id: user_id) + + if SiteSetting.chatbot_quota_reach_escalation_cool_down_period > 0 + if escalation_date.nil? || !SiteSetting.chatbot_quota_reach_escalation_cool_down_period.nil? && + escalation_date.value < SiteSetting.chatbot_quota_reach_escalation_cool_down_period.days.ago + escalate_quota_breach(user_id) + end + end + end + + def escalate_quota_breach(user_id) + user = User.find_by(id: user_id) + system_user = User.find_by(username_lower: "system") + + target_group_names = [] + + Array(SiteSetting.chatbot_quota_reach_escalation_groups).each do |g| + target_group_names << Group.find(g.to_i).name + end + + target_group_names = target_group_names.join(",") + + default_opts = { + post_alert_options: { skip_send_email: true }, + raw: I18n.t("chatbot.quota_reached.escalation.message", username: user.username), + skip_validations: true, + title: I18n.t("chatbot.quota_reached.escalation.title", username: user.username), + archetype: Archetype.private_message, + target_group_names: target_group_names + } + + post = PostCreator.create!(system_user, default_opts) + + user.custom_fields[::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD] = DateTime.now + user.save_custom_fields end private diff --git a/lib/discourse_chatbot/post/post_evaluation.rb b/lib/discourse_chatbot/post/post_evaluation.rb index 38386202..618633f9 100644 --- a/lib/discourse_chatbot/post/post_evaluation.rb +++ b/lib/discourse_chatbot/post/post_evaluation.rb @@ -57,7 +57,7 @@ def trigger_response(submission) ::DiscourseChatbot.progress_debug_message("humans found in this convo: #{human_participants_count}") - if bot_user && (user != bot_user) && (mentions_bot_name || explicit_reply_to_bot || (last_post_was_bot && human_participants_count == 1)) + if bot_user && (user.id > 0) && (mentions_bot_name || explicit_reply_to_bot || (last_post_was_bot && human_participants_count == 1)) opts = { type: POST, private: topic.archetype == Archetype.private_message, diff --git a/plugin.rb b/plugin.rb index 13215b7a..8f744c8c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -22,7 +22,7 @@ module ::DiscourseChatbot POST = "post" MESSAGE = "message" CHATBOT_QUERIES_CUSTOM_FIELD = "chatbot_queries" - CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_FIELD = "chatbot_queries_quota_reach_escalation_date" + CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD = "chatbot_queries_quota_reach_escalation_date" POST_TYPES_REGULAR_ONLY = [1] POST_TYPES_INC_WHISPERS = [1, 4] @@ -112,7 +112,7 @@ def progress_debug_message(message) register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_CUSTOM_FIELD, :integer) - register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_FIELD, :date) + register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD, :date) add_to_serializer(:current_user, :chatbot_access) do From 1226a788cc6dcad4349cc03b05432a942bdddd59 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 17:19:24 +0100 Subject: [PATCH 3/9] bump patch --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 8f744c8c..60b2a92b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-chatbot # about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages -# version: 0.9.7 +# version: 0.9.8 # authors: merefield # url: https://github.com/merefield/discourse-chatbot From f08e104cf63dc31df6adf1abfea065f11d472938 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 17:21:30 +0100 Subject: [PATCH 4/9] remove unnecessary trivial change --- app/jobs/regular/chatbot_reply.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/jobs/regular/chatbot_reply.rb b/app/jobs/regular/chatbot_reply.rb index 466839f1..9a701ffc 100644 --- a/app/jobs/regular/chatbot_reply.rb +++ b/app/jobs/regular/chatbot_reply.rb @@ -46,7 +46,6 @@ def execute(opts) if over_quota reply_and_thoughts[:reply] = I18n.t('chatbot.errors.overquota') - elsif type == ::DiscourseChatbot::POST && post is_private_msg = post.topic.private_message? opts.merge!(is_private_msg: is_private_msg) From e2244e6361b6b0443143760b0f12d8426104730d Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 17:25:58 +0100 Subject: [PATCH 5/9] update locale string --- config/locales/server.en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index c6f87f37..97194959 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -40,7 +40,7 @@ en: chatbot_quota_high_trust: "The allowed number of bot responses allowed by prompting user per week - high trust groups" chatbot_quota_medium_trust: "The allowed number of bot responses allowed by prompting user per week - medium trust groups" chatbot_quota_low_trust: "The allowed number of bot responses allowed by prompting user per week - low trust groups" - chatbot_quota_reach_escalation_group: "The group to which the bot will escalate when the quota is reached" + chatbot_quota_reach_escalation_groups: "The group(s) to which the bot will escalate when the quota is reached" chatbot_quota_reach_escalation_cool_down_period: "The number of days the bot will wait before escalating again after reaching the quota" chatbot_high_trust_groups: "Groups considered high trust for bot interaction" chatbot_medium_trust_groups: "Groups considered medium trust for bot interaction" From def25df0609eedd3e7707a5fd3026aeba76f6c73 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 18:18:12 +0100 Subject: [PATCH 6/9] extend test --- spec/lib/event_evaluation_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/lib/event_evaluation_spec.rb b/spec/lib/event_evaluation_spec.rb index 730c3d6e..a7853228 100644 --- a/spec/lib/event_evaluation_spec.rb +++ b/spec/lib/event_evaluation_spec.rb @@ -64,6 +64,7 @@ SiteSetting.chatbot_quota_low_trust = 1 event = ::DiscourseChatbot::EventEvaluation.new + expect { event.over_quota(high_trust_user.id) }.to change { Topic.where(archetype: "private_message").count }.by(1) expect(event.over_quota(high_trust_user.id)).to equal(true) end From 31c654cad4371c58a40aac7c4fdf7020374c1735 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 18:39:39 +0100 Subject: [PATCH 7/9] expand tests --- spec/lib/event_evaluation_spec.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/spec/lib/event_evaluation_spec.rb b/spec/lib/event_evaluation_spec.rb index a7853228..0abb10bb 100644 --- a/spec/lib/event_evaluation_spec.rb +++ b/spec/lib/event_evaluation_spec.rb @@ -2,6 +2,7 @@ require_relative '../plugin_helper' CHATBOT_QUERIES_CUSTOM_FIELD = "chatbot_queries" +CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD = "chatbot_queries_quota_reach_escalation_date" describe ::DiscourseChatbot::EventEvaluation do let(:normal_user) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) } @@ -54,7 +55,7 @@ expect(event.over_quota(high_trust_user.id)).to equal(false) end - it "returns the correct quota decision if user is in high trust group and user is outside of quota" do + it "returns the correct quota decision if user is in high trust group and user is outside of quota and escalates" do UserCustomField.create!(user_id: high_trust_user.id, name: CHATBOT_QUERIES_CUSTOM_FIELD, value: 3) SiteSetting.chatbot_high_trust_groups = "13|14" SiteSetting.chatbot_medium_trust_groups = "11|12" @@ -64,7 +65,22 @@ SiteSetting.chatbot_quota_low_trust = 1 event = ::DiscourseChatbot::EventEvaluation.new - expect { event.over_quota(high_trust_user.id) }.to change { Topic.where(archetype: "private_message").count }.by(1) + expect { event.over_quota(high_trust_user.id) }.to change { Topic.where(archetype: Archetype.private_message).count }.by(1) + expect(event.over_quota(high_trust_user.id)).to equal(true) + end + + it "returns the correct quota decision if user is in high trust group and user is outside of quota but doesn't escalate" do + UserCustomField.create!(user_id: high_trust_user.id, name: CHATBOT_QUERIES_CUSTOM_FIELD, value: 3) + UserCustomField.create!(user_id: high_trust_user.id, name: CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD, value: 30.minutes.ago) + SiteSetting.chatbot_high_trust_groups = "13|14" + SiteSetting.chatbot_medium_trust_groups = "11|12" + SiteSetting.chatbot_low_trust_groups = "10" + SiteSetting.chatbot_quota_high_trust = 3 + SiteSetting.chatbot_quota_medium_trust = 2 + SiteSetting.chatbot_quota_low_trust = 1 + + event = ::DiscourseChatbot::EventEvaluation.new + expect { event.over_quota(high_trust_user.id) }.to change { Topic.where(archetype: Archetype.private_message).count }.by(0) expect(event.over_quota(high_trust_user.id)).to equal(true) end From a39cb1ad322973b659b6e9a7de4dcd36bda439a3 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 18:42:57 +0100 Subject: [PATCH 8/9] FIX: rubocopped test --- spec/lib/event_evaluation_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/event_evaluation_spec.rb b/spec/lib/event_evaluation_spec.rb index 0abb10bb..bfde52f5 100644 --- a/spec/lib/event_evaluation_spec.rb +++ b/spec/lib/event_evaluation_spec.rb @@ -80,7 +80,7 @@ SiteSetting.chatbot_quota_low_trust = 1 event = ::DiscourseChatbot::EventEvaluation.new - expect { event.over_quota(high_trust_user.id) }.to change { Topic.where(archetype: Archetype.private_message).count }.by(0) + expect { event.over_quota(high_trust_user.id) }.not_to change { Topic.where(archetype: Archetype.private_message).count } expect(event.over_quota(high_trust_user.id)).to equal(true) end From 0c9820b76c9fb994572da6f4829b3385acea1429 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 19:00:33 +0100 Subject: [PATCH 9/9] IMPROVE: check for empty groups --- config/locales/server.en.yml | 1 + lib/discourse_chatbot/event_evaluation.rb | 30 +++++++------ .../functions/escalate_to_staff_function.rb | 42 +++++++++++-------- spec/lib/event_evaluation_spec.rb | 1 + 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 97194959..04f9d7a5 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -140,6 +140,7 @@ en: title: "Support bot chat escalation" announcement: "This is a follow-up Personal Message which includes Staff. The following chat conversation was had between the user and the bot:\n\n%{content}" answer_summary: "The function ran and this chat has been successfully escalated and raised to the attention of staff. A Personal Message to discuss things further with Staff has been created at the following url: %{url}. I must now share this news & the url with the user. There is no need for me to do anything more than communicate this fact with the web link and wish the user a good day." + no_escalation_groups: "No escalation groups have been set up for this function. Please contact your administrator." wrong_type_error: "This function cannot be used outside of Chat Messages" error: "Escalation of user and bot chat failed" forum_get_user_address: diff --git a/lib/discourse_chatbot/event_evaluation.rb b/lib/discourse_chatbot/event_evaluation.rb index b3ce0f06..da26d5b2 100644 --- a/lib/discourse_chatbot/event_evaluation.rb +++ b/lib/discourse_chatbot/event_evaluation.rb @@ -77,24 +77,28 @@ def escalate_quota_breach(user_id) target_group_names = [] Array(SiteSetting.chatbot_quota_reach_escalation_groups).each do |g| - target_group_names << Group.find(g.to_i).name + unless g.to_i == 0 + target_group_names << Group.find(g.to_i).name + end end - target_group_names = target_group_names.join(",") + if !target_group_names.empty? + target_group_names = target_group_names.join(",") - default_opts = { - post_alert_options: { skip_send_email: true }, - raw: I18n.t("chatbot.quota_reached.escalation.message", username: user.username), - skip_validations: true, - title: I18n.t("chatbot.quota_reached.escalation.title", username: user.username), - archetype: Archetype.private_message, - target_group_names: target_group_names - } + default_opts = { + post_alert_options: { skip_send_email: true }, + raw: I18n.t("chatbot.quota_reached.escalation.message", username: user.username), + skip_validations: true, + title: I18n.t("chatbot.quota_reached.escalation.title", username: user.username), + archetype: Archetype.private_message, + target_group_names: target_group_names + } - post = PostCreator.create!(system_user, default_opts) + post = PostCreator.create!(system_user, default_opts) - user.custom_fields[::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD] = DateTime.now - user.save_custom_fields + user.custom_fields[::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD] = DateTime.now + user.save_custom_fields + end end private diff --git a/lib/discourse_chatbot/functions/escalate_to_staff_function.rb b/lib/discourse_chatbot/functions/escalate_to_staff_function.rb index e1aa58d3..4357ebc5 100644 --- a/lib/discourse_chatbot/functions/escalate_to_staff_function.rb +++ b/lib/discourse_chatbot/functions/escalate_to_staff_function.rb @@ -37,32 +37,38 @@ def process(args, opts) target_group_names = [] Array(SiteSetting.chatbot_escalate_to_staff_groups).each do |g| - target_group_names << Group.find(g.to_i).name + unless g.to_i == 0 + target_group_names << Group.find(g.to_i).name + end end - target_group_names = target_group_names.join(",") + if !target_group_names.empty? + target_group_names = target_group_names.join(",") - message_or_post_id = opts[:reply_to_message_or_post_id] + message_or_post_id = opts[:reply_to_message_or_post_id] - message_collection = get_messages(message_or_post_id) - - content = generate_transcript(message_collection, bot_user) + message_collection = get_messages(message_or_post_id) - default_opts = { - post_alert_options: { skip_send_email: true }, - raw: I18n.t("chatbot.prompt.function.escalate_to_staff.announcement", content: content), - skip_validations: true, - title: I18n.t("chatbot.prompt.function.escalate_to_staff.title"), - archetype: Archetype.private_message, - target_usernames: target_usernames, - target_group_names: target_group_names - } + content = generate_transcript(message_collection, bot_user) - post = PostCreator.create!(current_user, default_opts) + default_opts = { + post_alert_options: { skip_send_email: true }, + raw: I18n.t("chatbot.prompt.function.escalate_to_staff.announcement", content: content), + skip_validations: true, + title: I18n.t("chatbot.prompt.function.escalate_to_staff.title"), + archetype: Archetype.private_message, + target_usernames: target_usernames, + target_group_names: target_group_names + } - url = "https://#{Discourse.current_hostname}/t/slug/#{post.topic_id}" + post = PostCreator.create!(current_user, default_opts) - response = I18n.t("chatbot.prompt.function.escalate_to_staff.answer_summary", url: url) + url = "https://#{Discourse.current_hostname}/t/slug/#{post.topic_id}" + + response = I18n.t("chatbot.prompt.function.escalate_to_staff.answer_summary", url: url) + else + response = I18n.t("chatbot.prompt.function.escalate_to_staff.no_escalation_groups") + end rescue I18n.t("chatbot.prompt.function.escalate_to_staff.error", parameter: args[parameters[0][:name]]) end diff --git a/spec/lib/event_evaluation_spec.rb b/spec/lib/event_evaluation_spec.rb index bfde52f5..abfbb06c 100644 --- a/spec/lib/event_evaluation_spec.rb +++ b/spec/lib/event_evaluation_spec.rb @@ -13,6 +13,7 @@ before(:each) do SiteSetting.chatbot_enabled = true + SiteSetting.chatbot_quota_reach_escalation_groups = "3" end it "returns the correct trust level for user in high trust group" do