Skip to content

Commit

Permalink
IMPROVE: check for empty groups
Browse files Browse the repository at this point in the history
  • Loading branch information
merefield committed Apr 5, 2024
1 parent a39cb1a commit 0c9820b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 17 additions & 13 deletions lib/discourse_chatbot/event_evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 24 additions & 18 deletions lib/discourse_chatbot/functions/escalate_to_staff_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/lib/event_evaluation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c9820b

Please sign in to comment.