From 0c9820b76c9fb994572da6f4829b3385acea1429 Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 5 Apr 2024 19:00:33 +0100 Subject: [PATCH] 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 9719495..04f9d7a 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 b3ce0f0..da26d5b 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 e1aa58d..4357ebc 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 bfde52f..abfbb06 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