Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Report failed Metabot alerts, subscriptions and Slack charts as errors (#84857)

Andrei Simionescueaa858716f2b
diff --git a/src/metabase/metabot/tools/create_alert.clj b/src/metabase/metabot/tools/create_alert.clj
index 4b16302b219..c5a1f1fbb2d 100644
--- a/src/metabase/metabot/tools/create_alert.clj
+++ b/src/metabase/metabot/tools/create_alert.clj
@@ -1,17 +1,17 @@
(ns metabase.metabot.tools.create-alert
- "Agent tool wrapper for creating alerts on saved questions.
- Gets slack_channel_id from agent context and calls the create-alert tool endpoint directly."
+ "Create alerts and check Slack delivery for alerts and dashboard subscriptions.
+
+ The alert watches a saved question and posts to the Slack conversation that the request came from."
(:require
- [clojure.set :as set]
[metabase.api.common :as api]
[metabase.channel.settings :as channel.settings]
[metabase.metabot.db :as metabot.db]
[metabase.metabot.scope :as scope]
- [metabase.metabot.tools.shared :as shared]
+ [metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.util :as metabot.tools.u]
[metabase.notification.api :as notification.api]
[metabase.util.cron :as u.cron]
- [metabase.util.malli :as mu]))
+ [metabase.util.i18n :refer [tru]]))
(set! *warn-on-reflection* true)
@@ -22,63 +22,62 @@
[schedule]
(u.cron/schedule-map->cron-string (metabot.tools.u/schedule->schedule-map schedule)))
-(defn- create-alert*
- "Private helper for create-alert (call that instead)."
- [{:keys [card-id slack-channel schedule send-condition send-once]
- :or {send-once false}}]
- (let [card (some-> (metabot.db/card card-id) api/read-check)
- channel-name (some->> slack-channel
- channel.settings/find-cached-slack-channel-or-username
- :display-name)]
- (cond
- (nil? card)
- {:error "no saved question with this card_id found"}
-
- (nil? channel-name)
- {:error "no slack channel found with this name"}
-
- :else
- (do
- (notification.api/create-notification!
- {:payload_type :notification/card
- :active true
- :creator_id api/*current-user-id*
- :payload {:card_id card-id
- :send_condition send-condition
- :send_once send-once}
- :subscriptions [{:type :notification-subscription/cron
- :cron_schedule (schedule->cron schedule)}]
- :handlers [{:channel_type :channel/slack
- :recipients [{:type :notification-recipient/raw-value
- :details {:value channel-name}}]}]})
- {:output "success"}))))
-
-(defn create-alert
- "Create an alert (notification when a saved question returns results)."
- [{:keys [card-id send-condition send-once slack-channel]
- :as args}]
- (cond
- (not (int? card-id))
- {:error "invalid card_id"}
-
- (and (some? send-once) (not (boolean? send-once)))
- {:error "send_once must be a boolean"}
-
- (not (#{:has_result :goal_above :goal_below} send-condition))
- {:error (str "unsupported send_condition: " (some-> send-condition name))}
-
- (not (channel.settings/slack-configured?))
- {:error "slack is not configured. Ask an admin to set up slack notifications in Metabase settings."}
-
- (empty? slack-channel)
- {:error "slack_channel is required"}
-
- :else
- (try
- (create-alert* args)
- (catch Exception e
- (-> (metabot.tools.u/handle-agent-error e)
- (set/rename-keys {:output :error}))))))
+(defn check-slack-configured!
+ "Throw an unrecoverable error when Slack isn't connected to Metabase, which only an admin can fix."
+ []
+ (when-not (channel.settings/slack-configured?)
+ (tools/unrecoverable!
+ ::slack-not-configured
+ {:user-message (tru "Slack isn''t connected to Metabase. An admin can connect it in Admin settings.")})))
+
+(defn current-slack-channel
+ "Return the display name of the Slack conversation for the call in `ctx`.
+
+ Alerts and subscriptions name their channel this way. Throws an unrecoverable error when the call didn't come
+ from Slack, when Slack isn't connected, or when the conversation isn't in Metabase's cached list of channels.
+ Slack posts each error's user message in the conversation, so none of them names a channel."
+ [ctx]
+ (let [channel-id (or (some-> (:memory-atom ctx) deref :context :slack_channel_id)
+ (tools/unrecoverable!
+ ::not-in-slack
+ {:user-message (tru "This can only be set up from a Slack conversation.")}))]
+ (check-slack-configured!)
+ (or (:display-name (channel.settings/find-cached-slack-channel-or-username channel-id))
+ (tools/unrecoverable!
+ ::unknown-slack-conversation
+ {:user-message (tru "Metabase can''t send alerts or subscriptions to this conversation. It can send them to public channels and to private channels that the Metabase app has been added to. A channel created in the last few hours may not be available yet.")
+ :data {:channel-id channel-id}}))))
+
+(defn do-with-refusal
+ "Call `thunk`, and turn a 403 that it throws into the unrecoverable error `code` with `user-message`.
+
+ Anything else that it throws passes through."
+ [code user-message thunk]
+ (try
+ (thunk)
+ (catch Throwable e
+ (if (= 403 (:status-code (ex-data e)))
+ (tools/unrecoverable! code {:user-message user-message :cause e})
+ (throw e)))))
+
+(defn- create-alert!
+ "Create an alert on the card with `card-id` that posts to the Slack `channel`, and return it."
+ [{:keys [card-id channel schedule send-condition send-once]}]
+ (do-with-refusal
+ ::no-alert-permission
+ (tru "You don''t have permission to create alerts.")
+ #(notification.api/create-notification!
+ {:payload_type :notification/card
+ :active true
+ :creator_id api/*current-user-id*
+ :payload {:card_id card-id
+ :send_condition send-condition
+ :send_once send-once}
+ :subscriptions [{:type :notification-subscription/cron
+ :cron_schedule (schedule->cron schedule)}]
+ :handlers [{:channel_type :channel/slack
+ :recipients [{:type :notification-recipient/raw-value
+ :details {:value channel}}]}]})))
(def schedule-schema
"Schedule schema shared with create-dashboard-subscription tool."
@@ -110,40 +109,46 @@
[:schedule schedule-schema]
[:send_once {:optional true :default false} :boolean]])
-(mu/defn ^{:tool-name "create_alert"
- :scope scope/agent-alert-create}
- create-alert-tool
- "Create an alert that notifies the user's current Slack channel based on a saved question's
- results on a recurring schedule. Alerts are for saved questions (cards), not dashboards — use
- the dashboard subscription tool for dashboard delivery.
-
- When the user asks to be alerted or notified about a saved question's results, you MUST call
- this tool; never claim an alert was created without calling it. If you are missing required
- information, ask the user for it rather than guessing.
-
- `card_id` is the id of a saved question, from a prior search result or the conversation context.
-
- `send_condition` decides when the alert fires:
- - `has_result` — the question returns any rows. Default this when the user doesn't say.
- - `goal_above` — the result exceeds the goal line set on the question.
- - `goal_below` — the result drops below the goal line.
- `goal_above` and `goal_below` only work on questions that have a goal line configured.
-
- `schedule` is a frequency (hourly, daily, weekly, monthly) plus that frequency's time fields.
- `send_once` is optional (default false); when true the alert is deleted after it fires once."
- [{:keys [card_id send_condition schedule send_once]} :- alert-schema]
- (let [slack-channel-id (:slack_channel_id (shared/current-context))]
- (try
- (when-not slack-channel-id
- (throw (ex-info "This tool can only be used from a Slack channel"
- {:agent-error? true})))
- (let [result (create-alert {:card-id card_id
- :send-condition (keyword send_condition)
+(def ^:private tool-description
+ (str "Create an alert that notifies the user's current Slack channel based on a saved question's\n"
+ "results on a recurring schedule. Alerts are for saved questions (cards), not dashboards; use\n"
+ "the dashboard subscription tool for dashboard delivery.\n"
+ "\n"
+ "When the user asks to be alerted or notified about a saved question's results, you MUST call\n"
+ "this tool; never claim an alert was created without calling it. If you are missing required\n"
+ "information, ask the user for it rather than guessing.\n"
+ "\n"
+ "`card_id` is the ID of a saved question, from a prior search result or the conversation context.\n"
+ "\n"
+ "`send_condition` decides when the alert fires:\n"
+ "- `has_result`: the question returns any rows. Default this when the user doesn't say.\n"
+ "- `goal_above`: the result exceeds the goal line set on the question.\n"
+ "- `goal_below`: the result drops below the goal line.\n"
+ "`goal_above` and `goal_below` only work on questions that have a goal line configured.\n"
+ "\n"
+ "`schedule` is a frequency (hourly, daily, weekly, monthly) plus that frequency's time fields.\n"
+ "`send_once` is optional (default false); when true the alert is deleted after it fires once."))
+
+(defrecord CreateAlertTool []
+ tools/Tool
+ (declaration [_]
+ {:name "create_alert"
+ :description tool-description
+ :scope scope/agent-alert-create
+ :args alert-schema})
+
+ (handle [_ {:keys [card_id send_condition schedule send_once]} ctx]
+ (let [channel (current-slack-channel ctx)
+ card (tools/with-entity {:kind :card :id card_id}
+ (api/read-check (metabot.db/card card_id)))
+ alert (create-alert! {:card-id card_id
+ :channel channel
:schedule schedule
- :send-once (boolean send_once)
- :slack-channel slack-channel-id})]
- (if (:error result)
- {:output (:error result)}
- {:output (or (:output result) "Alert created successfully.")}))
- (catch Exception e
- (metabot.tools.u/handle-agent-or-api-error e)))))
+ :send-condition (keyword send_condition)
+ :send-once (boolean send_once)})]
+ {:output (str "Created alert " (:id alert) " on " (name (:type card)) " \"" (:name card) "\" (ID " card_id
+ "), posting to " channel ".")})))
+
+(def create-alert-tool
+ "The `create_alert` tool."
+ (->CreateAlertTool))
diff --git a/src/metabase/metabot/tools/create_dashboard_subscription.clj b/src/metabase/metabot/tools/create_dashboard_subscription.clj
index 27e6298ddf9..7aa9fdc5765 100644
--- a/src/metabase/metabot/tools/create_dashboard_subscription.clj
+++ b/src/metabase/metabot/tools/create_dashboard_subscription.clj
@@ -1,41 +1,45 @@
(ns metabase.metabot.tools.create-dashboard-subscription
+ "Create dashboard subscriptions for the Slack profile.
+
+ It subscribes the conversation that the request came from."
(:require
[metabase.metabot.scope :as scope]
+ [metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.create-alert :as tools.create-alert]
- [metabase.metabot.tools.shared :as shared]
- [metabase.metabot.tools.subscriptions :as tools.subscriptions]
- [metabase.metabot.tools.util :as metabot.tools.u]
- [metabase.util.malli :as mu]))
+ [metabase.metabot.tools.subscriptions :as tools.subscriptions]))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private tool-description
+ (str "Create a recurring subscription that delivers a dashboard's contents to the user's current\n"
+ "Slack channel. The destination channel is taken from the conversation: there is nothing to\n"
+ "pass for it, and the tool only works from a Slack channel.\n"
+ "\n"
+ "When the user asks to subscribe to a dashboard, set up scheduled delivery, or receive regular\n"
+ "updates for a dashboard, you MUST call this tool; never claim a subscription was created\n"
+ "without calling it. If you are missing required information, ask the user for it rather than\n"
+ "guessing.\n"
+ "\n"
+ "`dashboard_id` is the ID of the dashboard, from a prior search result or the conversation\n"
+ "context. `schedule` is a frequency (hourly, daily, weekly, monthly) plus that frequency's\n"
+ "time fields."))
-(mu/defn ^{:tool-name "create_dashboard_subscription"
- :scope scope/agent-dashboard-subscribe}
- slackbot-create-dashboard-subscription-tool
- "Create a recurring subscription that delivers a dashboard's contents to the user's current
- Slack channel. The destination channel is taken from the conversation — there is nothing to
- pass for it, and the tool only works from a Slack channel.
+(defrecord SlackbotCreateDashboardSubscriptionTool []
+ tools/Tool
+ (declaration [_]
+ {:name "create_dashboard_subscription"
+ :description tool-description
+ :scope scope/agent-dashboard-subscribe
+ :args [:map {:closed true}
+ [:dashboard_id :int]
+ [:schedule tools.create-alert/schedule-schema]]})
- When the user asks to subscribe to a dashboard, set up scheduled delivery, or receive regular
- updates for a dashboard, you MUST call this tool; never claim a subscription was created
- without calling it. If you are missing required information, ask the user for it rather than
- guessing.
+ (handle [_ {:keys [dashboard_id schedule]} ctx]
+ (tools.subscriptions/create-dashboard-subscription!
+ {:dashboard-id dashboard_id
+ :channel (tools.create-alert/current-slack-channel ctx)
+ :schedule schedule})))
- `dashboard_id` is the id of the dashboard, from a prior search result or the conversation
- context. `schedule` is a frequency (hourly, daily, weekly, monthly) plus that frequency's
- time fields."
- [{:keys [dashboard_id schedule]} :- [:map {:closed true}
- [:dashboard_id :int]
- [:schedule tools.create-alert/schedule-schema]]]
- (let [slack-channel-id (:slack_channel_id (shared/current-context))]
- (try
- (when-not slack-channel-id
- (throw (ex-info "This tool can only be used from a Slack channel"
- {:agent-error? true})))
- (let [result (tools.subscriptions/create-dashboard-subscription
- {:dashboard-id dashboard_id
- :schedule schedule
- :slack-channel slack-channel-id})]
- (if (:error result)
- {:output (:error result)}
- {:output (or (:output result) "Dashboard subscription created successfully.")}))
- (catch Exception e
- (metabot.tools.u/handle-agent-or-api-error e)))))
+(def slackbot-create-dashboard-subscription-tool
+ "`create_dashboard_subscription` for the Slack profile."
+ (->SlackbotCreateDashboardSubscriptionTool))
diff --git a/src/metabase/metabot/tools/static_viz.clj b/src/metabase/metabot/tools/static_viz.clj
index 24bf498f1a1..e0047b4fbb8 100644
--- a/src/metabase/metabot/tools/static_viz.clj
+++ b/src/metabase/metabot/tools/static_viz.clj
@@ -1,22 +1,42 @@
(ns metabase.metabot.tools.static-viz
- "Static visualization tool for the slackbot profile.
- Renders existing saved questions/metrics as PNG images in Slack."
+ "Render saved questions and metrics as PNG images in Slack."
(:require
+ [metabase.api.common :as api]
[metabase.metabot.agent.streaming :as streaming]
+ [metabase.metabot.db :as metabot.db]
[metabase.metabot.scope :as scope]
- [metabase.util.malli :as mu]))
+ [metabase.metabot.tools.core :as tools]
+ [metabase.metabot.tools.shared.llm-shape :as llm-shape]))
(set! *warn-on-reflection* true)
-(mu/defn ^{:tool-name "static_viz"
- :scope scope/agent-viz-create}
- static-viz-tool
- "Generate a static visualization (PNG image) of a saved question or metric. The visualization will be posted as a separate follow-up message in the thread."
- [{:keys [entity_id]} :- [:map {:closed true}
- [:entity_id :int]]]
- {:instructions (str "Visualization queued for entity " entity_id ". "
- "The visualization will be posted as a separate follow-up message "
- "with the question or metric's name as its title and a link to open "
- "it in Metabase. Use future tense when referring to the visualization "
- "in your response — it hasn't appeared yet when the user sees your text.")
- :data-parts [(streaming/static-viz-part {:entity_id entity_id})]})
+(defn- queued-output
+ [{card-id :id card-name :name card-type :type}]
+ (str "<result>\n"
+ "Visualization queued for " (name card-type) " \"" (llm-shape/escape-xml-content card-name) "\" (ID "
+ card-id "). It will be posted as a separate follow-up message in the thread, with its name as the title "
+ "and a link to open it in Metabase.\n"
+ "</result>\n"
+ "<instructions>\n"
+ "Use the future tense when describing the visualization, because it has not appeared when the user reads "
+ "your message.\n"
+ "</instructions>"))
+
+(defrecord StaticVizTool []
+ tools/Tool
+ (declaration [_]
+ {:name "static_viz"
+ :description (str "Generate a static visualization (PNG image) of a saved question or metric. The "
+ "visualization will be posted as a separate follow-up message in the thread.")
+ :scope scope/agent-viz-create
+ :args [:map {:closed true} [:entity_id :int]]})
+
+ (handle [_ {:keys [entity_id]} _ctx]
+ (let [card (tools/with-entity {:kind :card :id entity_id}
+ (api/read-check (metabot.db/card entity_id)))]
+ {:output (queued-output card)
+ :data-parts [(streaming/static-viz-part {:entity_id entity_id})]})))
+
+(def static-viz-tool
+ "The `static_viz` tool."
+ (->StaticVizTool))
diff --git a/src/metabase/metabot/tools/subscriptions.clj b/src/metabase/metabot/tools/subscriptions.clj
index f9dc3dc085d..c15ef327877 100644
--- a/src/metabase/metabot/tools/subscriptions.clj
+++ b/src/metabase/metabot/tools/subscriptions.clj
@@ -1,15 +1,18 @@
(ns metabase.metabot.tools.subscriptions
- "Dashboard subscription tool wrapper."
+ "Create dashboard subscriptions for the Chat and Slack profiles.
+
+ The Chat profile's variant subscribes a Slack channel that the model names."
(:require
- [clojure.set :as set]
[metabase.api.common :as api]
[metabase.channel.settings :as channel.settings]
[metabase.metabot.db :as metabot.db]
[metabase.metabot.scope :as scope]
+ [metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.create-alert :as tools.create-alert]
[metabase.metabot.tools.util :as metabot.tools.u]
+ [metabase.models.interface :as mi]
[metabase.pulse.api :as pulse.api]
- [metabase.util.malli :as mu]
+ [metabase.util.i18n :refer [tru]]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -22,90 +25,97 @@
:details {:channel slack-channel}}
(metabot.tools.u/schedule->schedule-map schedule)))
-(defn- create-dashboard-subscription*
- "Private helper for create-dashboard-subscription (call that instead)."
- [{:keys [dashboard-id slack-channel schedule]}]
- (let [dashboard (some-> (metabot.db/dashboard dashboard-id)
- api/read-check
- (t2/hydrate [:dashcards :card]))
- cards (for [{:keys [id card]} (:dashcards dashboard)
- :when (-> card :id int?)]
- (-> card
- api/read-check
- (select-keys [:id :name :collection_id :description :display :parameter_mappings])
- (assoc :dashboard_card_id id
- :dashboard_id dashboard-id
- :include_csv false
- :include_xls false)))
- channel-name (some->> slack-channel
- channel.settings/find-cached-slack-channel-or-username
- ;; match existing code which stores display names like "#some-channel"
- :display-name)
- pulse-data (-> dashboard
- (select-keys [:collection_id :collection_position :name :parameters])
- (assoc :dashboard_id dashboard-id
- :creator_id api/*current-user-id*
- :skip_if_empty false))]
- (cond
- (nil? dashboard)
- {:error "no dashboard with this dashboard_id found"}
+(defn- pulse-card
+ "Return the question on a dashboard card as a pulse card."
+ [dashboard-id {:keys [id card]}]
+ (-> card
+ (select-keys [:id :name :collection_id :description :display :parameter_mappings])
+ (assoc :dashboard_card_id id
+ :dashboard_id dashboard-id
+ :include_csv false
+ :include_xls false)))
+
+(defn- create-pulse!
+ "Create the pulse that sends the questions on `dashcards` to the Slack `channel`, and return it.
- (nil? channel-name)
- {:error "no slack channel found with this name"}
+ The pulse subscribes to `dashboard` and sends on `schedule`."
+ [{dashboard-id :id :as dashboard} dashcards channel schedule]
+ (tools.create-alert/do-with-refusal
+ ::no-subscription-permission
+ (tru "You don''t have permission to create a subscription for this dashboard.")
+ #(pulse.api/create-pulse-with-perm-checks!
+ (map (partial pulse-card dashboard-id) dashcards)
+ [(make-slack-channel schedule channel)]
+ (-> dashboard
+ (select-keys [:collection_id :collection_position :name :parameters])
+ (assoc :dashboard_id dashboard-id
+ :creator_id api/*current-user-id*
+ :skip_if_empty false)))))
- :else
- (do (pulse.api/create-pulse-with-perm-checks!
- cards
- [(make-slack-channel schedule channel-name)]
- pulse-data)
- {:output "success"}))))
+(defn- dashboard-for-subscription [dashboard-id]
+ (let [dashboard (tools/with-entity {:kind :dashboard :id dashboard-id}
+ (-> (metabot.db/dashboard dashboard-id)
+ api/read-check
+ (t2/hydrate [:dashcards :card])))
+ dashcards (filter (comp int? :id :card) (:dashcards dashboard))]
+ (when-not (every? (comp mi/can-read? :card) dashcards)
+ (tools/unrecoverable!
+ ::dashboard-cards-not-readable
+ {:user-message (tru "You can''t subscribe to this dashboard, because you don''t have access to every question on it.")
+ :data {:dashboard-id dashboard-id}}))
+ (assoc dashboard :dashcards dashcards)))
-(defn create-dashboard-subscription
- "Create a dashboard subscription and send it to a slack channel."
- [{:keys [dashboard-id slack-channel] :as args}]
- (cond
- (not (int? dashboard-id))
- {:error "invalid dashboard_id"}
+(defn- subscribe-to-dashboard! [{dashboard-id :id :as dashboard} channel schedule]
+ (let [pulse (create-pulse! dashboard (:dashcards dashboard) channel schedule)]
+ {:output (str "Created subscription " (:id pulse) " to dashboard \"" (:name dashboard) "\" (ID " dashboard-id
+ "), posting to " channel ".")}))
- (not (channel.settings/slack-configured?))
- {:error "slack is not configured. Ask an admin to connect slack in Metabase settings."}
+(defn create-dashboard-subscription!
+ "Subscribe Slack `channel` to dashboard `dashboard-id` and return the tool result.
- (empty? slack-channel)
- {:error "slack_channel is required"}
+ `channel` is a display name from Metabase's cached list of Slack channels. Delivery follows `schedule`.
+ Throws the declared not-found error when the dashboard is missing or unreadable, and an
+ unrecoverable error when the user can read it but may not subscribe to it."
+ [{:keys [dashboard-id channel schedule]}]
+ (subscribe-to-dashboard! (dashboard-for-subscription dashboard-id) channel schedule))
- :else
- (try
- (create-dashboard-subscription* args)
- (catch Exception e
- (-> (metabot.tools.u/handle-agent-error e)
- (set/rename-keys {:output :error}))))))
+(tools/defrecoverable unknown-slack-channel!
+ "`slack_channel` names no channel or user in Metabase's cached list of Slack channels and users."
+ {:payload [:map {:closed true} [:slack-channel :string]]}
+ [{:keys [slack-channel]}]
+ {:message (str "Metabase can't find a Slack channel or user named " (pr-str slack-channel) "."
+ " It can send to public channels, private channels that the Metabase app"
+ " has been added to, and users. A channel created in the last few hours may not be available yet.")
+ :recovery [{:uses #{}
+ :text "Ask the user for the channel's exact name, as it appears in Slack."}]})
-(def ^:private subscription-schema
- [:map {:closed true}
- [:dashboard_id :int]
- [:email {:optional true} [:maybe :string]]
- [:slack_channel {:optional true} [:maybe :string]]
- [:schedule tools.create-alert/schedule-schema]])
+(def ^:private tool-description
+ (str "Create a dashboard subscription that sends regular updates to a Slack channel.\n"
+ "\n"
+ "Use when a user wants to receive or send regular updates on a dashboard's contents.\n"
+ "Requires a valid `dashboard_id`, a `slack_channel` name, and a `schedule`.\n"
+ "\n"
+ "Delivery is Slack-only, and Slack must be connected in Metabase settings. If the user asks to\n"
+ "be emailed a dashboard, tell them this tool can only deliver to Slack."))
-(mu/defn ^{:tool-name "create_dashboard_subscription"
- :scope scope/agent-dashboard-subscribe}
- create-dashboard-subscription-tool
- "Create a dashboard subscription that sends regular updates to a Slack channel.
+(defrecord CreateDashboardSubscriptionTool []
+ tools/Tool
+ (declaration [_]
+ {:name "create_dashboard_subscription"
+ :description tool-description
+ :scope scope/agent-dashboard-subscribe
+ :args [:map {:closed true}
+ [:dashboard_id :int]
+ [:slack_channel [:string {:min 1}]]
+ [:schedule tools.create-alert/schedule-schema]]})
- Use when a user wants to receive or send regular updates on a dashboard's contents.
- Requires a valid `dashboard_id`, a `slack_channel` name, and a `schedule`.
+ (handle [_ {:keys [dashboard_id slack_channel schedule]} _ctx]
+ (tools.create-alert/check-slack-configured!)
+ (let [dashboard (dashboard-for-subscription dashboard_id)
+ channel (or (:display-name (channel.settings/find-cached-slack-channel-or-username slack_channel))
+ (unknown-slack-channel! {:slack-channel slack_channel}))]
+ (subscribe-to-dashboard! dashboard channel schedule))))
- Delivery is Slack-only: the `email` argument is not implemented and is ignored, and Slack must
- be connected in Metabase settings. A call without `slack_channel` fails, so if the user asks to
- be emailed a dashboard, tell them this tool can only deliver to Slack rather than accepting an
- email address."
- [{:keys [dashboard_id email slack_channel schedule]} :- subscription-schema]
- (try
- (-> (create-dashboard-subscription
- {:dashboard-id dashboard_id
- :email email
- :slack-channel slack_channel
- :schedule schedule})
- (set/rename-keys {:error :output}))
- (catch Exception e
- (metabot.tools.u/handle-agent-or-api-error e))))
+(def create-dashboard-subscription-tool
+ "`create_dashboard_subscription` for the Chat profile."
+ (->CreateDashboardSubscriptionTool))
diff --git a/test/metabase/metabot/tools/create_alert_test.clj b/test/metabase/metabot/tools/create_alert_test.clj
index d21791f1ab3..12b28ff50b5 100644
--- a/test/metabase/metabot/tools/create_alert_test.clj
+++ b/test/metabase/metabot/tools/create_alert_test.clj
@@ -1,98 +1,111 @@
(ns metabase.metabot.tools.create-alert-test
(:require
[clojure.test :refer :all]
+ [metabase.api-scope.core :as api-scope]
[metabase.channel.settings :as channel.settings]
- [metabase.metabot.tools.create-alert
- :as metabot.tools.create-alert]
- [metabase.metabot.tools.shared :as shared]
+ [metabase.metabot.tools.core :as tools.core]
+ [metabase.metabot.tools.create-alert :as tools.create-alert]
+ [metabase.metabot.tools.recoverable.common :as recoverable.common]
+ [metabase.metabot.tools.runtime :as tools.runtime]
+ [metabase.notification.api :as notification.api]
+ [metabase.notification.models :as models.notification]
[metabase.test :as mt]))
-(defn- alert-data [card-id]
- {:card-id card-id
- :send-condition :has_result
- :slack-channel "data-team"
- :schedule {:frequency :daily
- :hour 9}})
-
(def ^:private fake-channels
{:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})
-(deftest create-alert-test
+(defmacro ^:private with-slack-channels [& body]
+ `(mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
+ channel.settings/slack-cached-channels-and-usernames (constantly fake-channels)]
+ ~@body))
+
+(defn- create-alert
+ "Run `create_alert` through the Slack agent loop and return its result.
+ The call comes from Slack conversation `channel-id`."
+ ([args] (create-alert args "C123"))
+ ([args channel-id]
+ (tools.runtime/invoke (tools.core/entries [tools.create-alert/create-alert-tool])
+ {:profile-id :slackbot
+ :metabot nil
+ :user-scope api-scope/unrestricted
+ :tool-names #{"create_alert" "search"}
+ :memory-atom (atom {:context (cond-> {} channel-id (assoc :slack_channel_id channel-id))})}
+ "create_alert"
+ args)))
+
+(defn- daily-alert [card-id]
+ {:card_id card-id :send_condition "has_result" :schedule {:frequency "daily" :hour 9}})
+
+(deftest creates-an-alert-test
(mt/with-model-cleanup [:model/Notification]
- (mt/with-temp [:model/Card {card-id :id} {}]
- (let [invoke-tool #(mt/with-current-user (mt/user->id :rasta)
- (metabot.tools.create-alert/create-alert %))
- base-data (alert-data card-id)]
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
- channel.settings/slack-cached-channels-and-usernames (constantly fake-channels)]
- (testing "Slack alert can be created"
- (is (= {:output "success"}
- (invoke-tool base-data))))
- (testing "Alert with goal_above send-condition can be created"
- (is (= {:output "success"}
- (invoke-tool (assoc base-data :send-condition :goal_above)))))
- (testing "Alert with goal_below send-condition can be created"
- (is (= {:output "success"}
- (invoke-tool (assoc base-data :send-condition :goal_below)))))
- (testing "Alert with send-once can be created"
- (is (= {:output "success"}
- (invoke-tool (assoc base-data :send-once true))))))))))
+ (mt/with-temp [:model/Card {card-id :id} {:name "Revenue"}]
+ (with-slack-channels
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [{:keys [output error]} (create-alert (assoc (daily-alert card-id)
+ :send_condition "goal_above"
+ :send_once true))
+ [alert] (models.notification/notifications-for-card card-id)]
+ (is (nil? error))
+ (testing "it posts to the conversation that asked for it, when its condition is met"
+ (is (=? {:payload {:card_id card-id :send_condition :goal_above :send_once true}
+ :handlers [{:channel_type :channel/slack
+ :recipients [{:details {:value "#data-team"}}]}]}
+ alert)))
+ (testing "the result names the alert that was created"
+ (is (= (str "Created alert " (:id alert) " on question \"Revenue\" (ID " card-id
+ "), posting to #data-team.")
+ output)))))))))
-(deftest create-alert-validation-test
- (mt/with-temp [:model/Card {card-id :id} {}]
- (let [invoke-tool #(mt/with-current-user (mt/user->id :rasta)
- (metabot.tools.create-alert/create-alert %))
- base-data (alert-data card-id)]
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
- channel.settings/slack-cached-channels-and-usernames (constantly fake-channels)]
- (testing "Return an error message if the card-id is invalid"
- (is (= {:error "invalid card_id"}
- (invoke-tool (update base-data :card-id str)))))
- (testing "Return an error message if the card cannot be found"
- (is (= {:error "no saved question with this card_id found"}
- (invoke-tool (update base-data :card-id + 100)))))
- (testing "Slack alert requires slack-channel"
- (is (= {:error "slack_channel is required"}
- (invoke-tool (dissoc base-data :slack-channel)))))
- (testing "Invalid send-condition returns an error"
- (is (= {:error "unsupported send_condition: always"}
- (invoke-tool (assoc base-data :send-condition :always)))))
- (testing "Invalid send-once returns an error"
- (is (= {:error "send_once must be a boolean"}
- (invoke-tool (assoc base-data :send-once "true")))))))))
+(deftest a-card-it-cannot-read-is-not-found-test
+ (testing "the model gets the same answer for a card that is missing and one that the user can't read"
+ (with-slack-channels
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Card {card-id :id} {}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (doseq [id [card-id Integer/MAX_VALUE]]
+ (is (= {:output (str "Card " id " was not found. It may not exist, or you may not have access to it.\n"
+ "Call `search` to find the entity you want and use an id from the results.")
+ :error {:class :recoverable :code ::recoverable.common/not-found}}
+ (create-alert (daily-alert id)))))))))))
-(deftest create-alert-channel-not-configured-test
- (mt/with-temp [:model/Card {card-id :id} {}]
- (let [invoke-tool #(mt/with-current-user (mt/user->id :rasta)
- (metabot.tools.create-alert/create-alert %))
- base-data (alert-data card-id)]
- (testing "Slack alert fails when Slack is not configured"
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly false)]
- (is (= {:error "slack is not configured. Ask an admin to set up slack notifications in Metabase settings."}
- (invoke-tool base-data)))))
- (testing "Slack alert fails when channel does not exist"
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
- channel.settings/slack-cached-channels-and-usernames (constantly {:channels []})]
- (is (= {:error "no slack channel found with this name"}
- (invoke-tool (assoc base-data :slack-channel "no-such-channel")))))))))
+(deftest slack-problems-end-the-turn-test
+ (testing "only the user or an admin can fix these, so the turn ends with a message that names no channel"
+ (mt/with-temp [:model/Card {card-id :id} {}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (with-slack-channels
+ (testing "outside of Slack"
+ (is (= {:class :unrecoverable
+ :code ::tools.create-alert/not-in-slack
+ :user-message "This can only be set up from a Slack conversation."}
+ (:error (create-alert (daily-alert card-id) nil)))))
+ (testing "a DM cannot receive alerts"
+ (is (= {:class :unrecoverable
+ :code ::tools.create-alert/unknown-slack-conversation
+ :user-message (str "Metabase can't send alerts or subscriptions to this conversation. "
+ "It can send them to public channels and to private channels that the "
+ "Metabase app has been added to. A channel created in the last few hours "
+ "may not be available yet.")}
+ (:error (create-alert (daily-alert card-id) "D0123"))))))
+ (testing "Slack must be connected"
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly false)]
+ (is (= {:class :unrecoverable
+ :code ::tools.create-alert/slack-not-configured
+ :user-message "Slack isn't connected to Metabase. An admin can connect it in Admin settings."}
+ (:error (create-alert (daily-alert card-id)))))))))))
-(deftest create-alert-tool-errors-test
- (let [create-alert! #(binding [shared/*memory-atom* (atom {:context {:slack_channel_id "C123"}})]
- (metabot.tools.create-alert/create-alert-tool
- {:card_id %
- :send_condition "has_result"
- :schedule {:frequency "daily" :hour 9}}))]
- (testing "a call from outside Slack goes back to the agent as output"
- (is (= {:output "This tool can only be used from a Slack channel"}
- (metabot.tools.create-alert/create-alert-tool
- {:card_id 1 :send_condition "has_result" :schedule {:frequency "daily" :hour 9}}))))
- (testing "a card the user can't read goes back to the agent as output"
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)]
- (mt/with-non-admin-groups-no-root-collection-perms
- (mt/with-temp [:model/Card {card-id :id} {}]
- (mt/with-current-user (mt/user->id :rasta)
- (is (= "You don't have permissions to do that."
- (:output (create-alert! card-id)))))))))
- (testing "an unexpected error propagates to the agent loop"
- (mt/with-dynamic-fn-redefs [metabot.tools.create-alert/create-alert (fn [_] (throw (ex-info "boom" {})))]
- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom" (create-alert! 1)))))))
+(deftest refused-alert-test
+ (mt/with-temp [:model/Card {card-id :id} {}]
+ (with-slack-channels
+ (mt/with-current-user (mt/user->id :rasta)
+ (testing "a user who may not create alerts is told so"
+ (mt/with-dynamic-fn-redefs [notification.api/create-notification!
+ (fn [_] (throw (ex-info "You don't have permissions to do that."
+ {:status-code 403})))]
+ (is (= {:class :unrecoverable
+ :code ::tools.create-alert/no-alert-permission
+ :user-message "You don't have permission to create alerts."}
+ (:error (create-alert (daily-alert card-id)))))))
+ (testing "any other failure stays an internal error"
+ (mt/with-dynamic-fn-redefs [notification.api/create-notification! (fn [_] (throw (ex-info "boom" {})))]
+ (is (= {:class :unrecoverable :code :internal}
+ (:error (create-alert (daily-alert card-id)))))))))))
diff --git a/test/metabase/metabot/tools/create_dashboard_subscription_test.clj b/test/metabase/metabot/tools/create_dashboard_subscription_test.clj
index 00f69c375a2..e1c6b47742c 100644
--- a/test/metabase/metabot/tools/create_dashboard_subscription_test.clj
+++ b/test/metabase/metabot/tools/create_dashboard_subscription_test.clj
@@ -1,10 +1,51 @@
(ns metabase.metabot.tools.create-dashboard-subscription-test
(:require
[clojure.test :refer :all]
- [metabase.metabot.tools.create-dashboard-subscription :as tools.create-dashboard-subscription]))
+ [metabase.api-scope.core :as api-scope]
+ [metabase.channel.settings :as channel.settings]
+ [metabase.metabot.tools.core :as tools.core]
+ [metabase.metabot.tools.create-alert :as tools.create-alert]
+ [metabase.metabot.tools.create-dashboard-subscription :as tools.create-dashboard-subscription]
+ [metabase.metabot.tools.runtime :as tools.runtime]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
-(deftest ^:parallel slackbot-create-dashboard-subscription-outside-slack-test
- (testing "a call from outside Slack goes back to the agent as output"
- (is (= {:output "This tool can only be used from a Slack channel"}
- (tools.create-dashboard-subscription/slackbot-create-dashboard-subscription-tool
- {:dashboard_id 1 :schedule {:frequency "daily" :hour 9}})))))
+(defn- subscribe
+ "Run `create_dashboard_subscription` through the Slack agent loop and return its result.
+ The call comes from Slack conversation `channel-id`."
+ [args channel-id]
+ (tools.runtime/invoke (tools.core/entries
+ [tools.create-dashboard-subscription/slackbot-create-dashboard-subscription-tool])
+ {:profile-id :slackbot
+ :metabot nil
+ :user-scope api-scope/unrestricted
+ :tool-names #{"create_dashboard_subscription" "search"}
+ :memory-atom (atom {:context (cond-> {} channel-id (assoc :slack_channel_id channel-id))})}
+ "create_dashboard_subscription"
+ args))
+
+(deftest subscribes-the-current-conversation-test
+ (mt/with-model-cleanup [:model/Pulse]
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
+ channel.settings/slack-cached-channels-and-usernames
+ (constantly {:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Sales"}
+ :model/Card {card-id :id} {}
+ :model/DashboardCard _ {:dashboard_id dash-id :card_id card-id}]
+ (let [{:keys [output error]} (subscribe {:dashboard_id dash-id :schedule {:frequency "daily" :hour 9}}
+ "C123")
+ pulse (t2/select-one :model/Pulse :dashboard_id dash-id)]
+ (is (nil? error))
+ (is (= (str "Created subscription " (:id pulse) " to dashboard \"Sales\" (ID " dash-id
+ "), posting to #data-team.")
+ output))
+ (is (=? [{:channel_type :slack :details {:channel "#data-team"}}]
+ (t2/select :model/PulseChannel :pulse_id (:id pulse))))))))))
+
+(deftest ^:parallel outside-slack-test
+ (testing "a subscription requires a Slack conversation as its destination"
+ (is (= {:class :unrecoverable
+ :code ::tools.create-alert/not-in-slack
+ :user-message "This can only be set up from a Slack conversation."}
+ (:error (subscribe {:dashboard_id 1 :schedule {:frequency "daily" :hour 9}} nil))))))
diff --git a/test/metabase/metabot/tools/static_viz_test.clj b/test/metabase/metabot/tools/static_viz_test.clj
new file mode 100644
index 00000000000..6fcba935857
--- /dev/null
+++ b/test/metabase/metabot/tools/static_viz_test.clj
@@ -0,0 +1,31 @@
+(ns metabase.metabot.tools.static-viz-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.metabot.test-util :as test-util]
+ [metabase.metabot.tools.recoverable.common :as recoverable.common]
+ [metabase.metabot.tools.static-viz :as tools.static-viz]
+ [metabase.test :as mt]))
+
+(deftest queues-a-readable-card-test
+ (mt/with-temp [:model/Card {card-id :id} {:name "Revenue <by> month"}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [{:keys [output data-parts]} (test-util/call-tool tools.static-viz/static-viz-tool {:entity_id card-id})]
+ (testing "the model reads which card it queued, by name"
+ (is (re-find (re-pattern (str "Visualization queued for question \"Revenue &lt;by&gt; month\" \\(ID "
+ card-id "\\)"))
+ output)))
+ (testing "and the Slack bridge gets the card to render"
+ (is (= [{:type :data :data-type "static_viz" :data {:entity_id card-id}}]
+ data-parts)))))))
+
+(deftest a-card-it-cannot-read-is-not-queued-test
+ (testing "an unreadable card and a missing card both return a recoverable error"
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Card {card-id :id} {}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (doseq [id [card-id Integer/MAX_VALUE]]
+ (is (= {:class :recoverable
+ :code ::recoverable.common/not-found
+ :text (str "Card " id " was not found. It may not exist, or you may not have access to it.\n"
+ "Call `search` to find the entity you want and use an id from the results.")}
+ (test-util/tool-failure tools.static-viz/static-viz-tool {:entity_id id})))))))))
diff --git a/test/metabase/metabot/tools/subscriptions_test.clj b/test/metabase/metabot/tools/subscriptions_test.clj
index 5f6f4f09c27..4cc760db752 100644
--- a/test/metabase/metabot/tools/subscriptions_test.clj
+++ b/test/metabase/metabot/tools/subscriptions_test.clj
@@ -1,189 +1,136 @@
(ns metabase.metabot.tools.subscriptions-test
- "Tests for the create_dashboard_subscription agent tool wrapper."
+ "Test the Chat profile's dashboard subscription tool and the code shared with the Slack profile."
(:require
[clojure.test :refer :all]
+ [metabase.api-scope.core :as api-scope]
[metabase.channel.settings :as channel.settings]
[metabase.metabot.test-util :as test-util]
+ [metabase.metabot.tools.core :as tools.core]
+ [metabase.metabot.tools.create-alert :as tools.create-alert]
+ [metabase.metabot.tools.recoverable.common :as recoverable.common]
+ [metabase.metabot.tools.runtime :as tools.runtime]
[metabase.metabot.tools.subscriptions :as agent-subscriptions]
+ [metabase.pulse.api :as pulse.api]
[metabase.test :as mt]
[toucan2.core :as t2]))
-;;; ------------------------------------------ schema / metadata tests -----------------------------------------------
+(def ^:private fake-channels
+ {:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})
-(deftest create-dashboard-subscription-tool-schema-test
- (let [m (meta #'agent-subscriptions/create-dashboard-subscription-tool)]
- (testing "tool has correct :tool-name"
- (is (= "create_dashboard_subscription" (:tool-name m))))
- (testing "tool var has expected metadata"
- (is (some? (:schema m)))
- (is (string? (:doc m))))))
+(defn- subscribe
+ "Run `create_dashboard_subscription` through the Chat agent loop and return its result."
+ [args]
+ (tools.runtime/invoke (tools.core/entries [agent-subscriptions/create-dashboard-subscription-tool])
+ {:profile-id :internal
+ :metabot nil
+ :user-scope api-scope/unrestricted
+ :tool-names #{"create_dashboard_subscription" "search"}}
+ "create_dashboard_subscription"
+ args))
-;;; --------------------------------------- integration tests (with-temp) -------------------------------------------
+(defn- daily [dashboard-id slack-channel]
+ {:dashboard_id dashboard-id :slack_channel slack-channel :schedule {:frequency "daily" :hour 9}})
-(deftest create-dashboard-subscription-happy-path-test
- (testing "valid dashboard, valid user email, valid schedule → success"
- (let [captured-args (atom nil)]
- (mt/with-dynamic-fn-redefs [agent-subscriptions/create-dashboard-subscription
- (fn [args] (reset! captured-args args) {:output "success"})]
- (mt/with-current-user (mt/user->id :crowberto)
- (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}]
- (let [email (:email (mt/fetch-user :crowberto))
- result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id dash-id
- :email email
- :schedule {:frequency "daily" :hour 9}})]
- (is (= "success" (:output result)))
- (is (= email (:email @captured-args)))
- (is (= dash-id (:dashboard-id @captured-args))))))))))
-
-(deftest create-dashboard-subscription-invalid-dashboard-id-test
- (testing "nonexistent dashboard_id → error about missing dashboard"
- (mt/with-current-user (mt/user->id :crowberto)
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id 0
- :schedule {:frequency "daily" :hour 9}})]
- ;; dashboard_id 0 is int but won't match any dashboard
- (is (string? (:output result)))))))
-
-(deftest create-dashboard-subscription-unknown-email-test
- (testing "unknown email → 'no user with this email found'"
- (mt/with-dynamic-fn-redefs [agent-subscriptions/create-dashboard-subscription
- (fn [{:keys [email]}]
- (if (= email "[email protected]")
- {:output "no user with this email found"}
- {:output "success"}))]
- (mt/with-current-user (mt/user->id :crowberto)
- (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}]
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id dash-id
- :schedule {:frequency "daily" :hour 9}})]
- (is (= "no user with this email found" (:output result)))))))))
-
-(deftest create-dashboard-subscription-nonexistent-dashboard-test
- (testing "nonexistent dashboard → 404 or error message"
- (mt/with-dynamic-fn-redefs [agent-subscriptions/create-dashboard-subscription
- (fn [{:keys [dashboard-id]}]
- (if (= dashboard-id 999999)
- {:output "no dashboard with this dashboard_id found"}
- {:output "success"}))]
- (mt/with-current-user (mt/user->id :crowberto)
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id 999999
- :email (:email (mt/fetch-user :crowberto))
- :schedule {:frequency "daily" :hour 9}})]
- (is (= "no dashboard with this dashboard_id found" (:output result))))))))
-
-(deftest ^:parallel create-dashboard-subscription-unknown-day-of-week-test
- (testing "a day_of_week outside the listed days is rejected at argument validation"
+(deftest ^:parallel arguments-test
+ (testing "an unsupported day_of_week fails argument validation"
(is (=? #"Invalid tool arguments: `schedule` .*should be either \"sunday\".*"
(test-util/tool-boundary-error "create_dashboard_subscription"
#'agent-subscriptions/create-dashboard-subscription-tool
{:dashboard_id 1
:slack_channel "data-team"
- :schedule {:frequency "weekly" :day_of_week "mo" :hour 9}})))))
+ :schedule {:frequency "weekly" :day_of_week "mo" :hour 9}}))))
+ (testing "Slack delivery requires a channel"
+ (is (= "Invalid tool arguments: `slack_channel` is required."
+ (test-util/tool-boundary-error "create_dashboard_subscription"
+ #'agent-subscriptions/create-dashboard-subscription-tool
+ {:dashboard_id 1 :schedule {:frequency "daily" :hour 9}})))))
-;;; ---------------------------------------- Slack integration tests --------------------------------------------------
-
-(deftest create-dashboard-subscription-slack-happy-path-test
- (testing "valid dashboard, Slack channel, daily schedule → success"
- (mt/with-model-cleanup [:model/Pulse]
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
- channel.settings/slack-cached-channels-and-usernames
- (constantly {:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})]
- (mt/with-current-user (mt/user->id :crowberto)
- (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}
- :model/Card {card-id :id} {}
- :model/DashboardCard _ {:dashboard_id dash-id
- :card_id card-id
- :row 0
- :col 0}]
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id dash-id
- :slack_channel "data-team"
- :schedule {:frequency "daily" :hour 9}})]
- (is (= {:output "success"} result)))))))))
-
-(deftest create-dashboard-subscription-slack-monthly-schedule-test
- (testing "Slack subscription with monthly schedule → success, stored with the requested schedule"
- (mt/with-model-cleanup [:model/Pulse]
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
- channel.settings/slack-cached-channels-and-usernames
- (constantly {:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})]
- (mt/with-current-user (mt/user->id :crowberto)
- (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}
- :model/Card {card-id :id} {}
- :model/DashboardCard _ {:dashboard_id dash-id
- :card_id card-id
- :row 0
- :col 0}]
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id dash-id
- :slack_channel "data-team"
- :schedule {:frequency "monthly"
- :day_of_month "last-sunday"
- :hour 7}})]
- (is (= {:output "success"} result))
- (is (=? [{:schedule_type :monthly
+(deftest subscribes-a-slack-channel-test
+ (mt/with-model-cleanup [:model/Pulse]
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
+ channel.settings/slack-cached-channels-and-usernames (constantly fake-channels)]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Sales"}
+ :model/Card {card-id :id} {}
+ :model/DashboardCard _ {:dashboard_id dash-id :card_id card-id}]
+ (let [{:keys [output error]} (subscribe (assoc (daily dash-id "#data-team")
+ :schedule {:frequency "monthly"
+ :day_of_month "last-sunday"
+ :hour 7}))
+ pulse (t2/select-one :model/Pulse :dashboard_id dash-id)]
+ (is (nil? error))
+ (testing "the model reads what it created"
+ (is (= (str "Created subscription " (:id pulse) " to dashboard \"Sales\" (ID " dash-id
+ "), posting to #data-team.")
+ output)))
+ (testing "and it posts to that channel on the requested schedule"
+ (is (=? [{:channel_type :slack
+ :details {:channel "#data-team"}
+ :schedule_type :monthly
:schedule_hour 7
:schedule_day "sun"
:schedule_frame :last}]
- (t2/select :model/PulseChannel :pulse_id (t2/select-one-pk :model/Pulse :dashboard_id dash-id)))))))))))
+ (t2/select :model/PulseChannel :pulse_id (:id pulse)))))))))))
-(deftest create-dashboard-subscription-slack-required-test
- (testing "empty slack_channel → error"
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)]
+(deftest unknown-slack-channel-test
+ (testing "a channel that Metabase has never seen is for the model to sort out with the user"
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
+ channel.settings/slack-cached-channels-and-usernames (constantly fake-channels)]
(mt/with-current-user (mt/user->id :crowberto)
- (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}]
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id dash-id
- :slack_channel ""
- :schedule {:frequency "daily" :hour 9}})]
- (is (= {:output "slack_channel is required"} result))))))))
+ (mt/with-temp [:model/Dashboard {dash-id :id} {}]
+ (is (= {:output (str "Metabase can't find a Slack channel or user named \"data team\". "
+ "It can send to public channels, private channels that the "
+ "Metabase app has been added to, and users. A channel created in the last few hours "
+ "may not be available yet.\n"
+ "Ask the user for the channel's exact name, as it appears in Slack.")
+ :error {:class :recoverable :code ::agent-subscriptions/unknown-slack-channel}}
+ (subscribe (daily dash-id "data team")))))))))
-(deftest create-dashboard-subscription-slack-not-configured-test
- (testing "Slack not configured → error"
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly false)]
- (mt/with-current-user (mt/user->id :crowberto)
- (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}]
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id dash-id
- :slack_channel "data-team"
- :schedule {:frequency "daily" :hour 9}})]
- (is (= {:output "slack is not configured. Ask an admin to connect slack in Metabase settings."}
- result))))))))
+(deftest slack-not-connected-test
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly false)]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (is (= {:class :unrecoverable
+ :code ::tools.create-alert/slack-not-configured
+ :user-message "Slack isn't connected to Metabase. An admin can connect it in Admin settings."}
+ (:error (subscribe (daily 1 "data-team"))))))))
-(deftest create-dashboard-subscription-slack-channel-not-found-test
- (testing "nonexistent Slack channel → error"
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
- channel.settings/slack-cached-channels-and-usernames (constantly {:channels []})]
- (mt/with-current-user (mt/user->id :crowberto)
- (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}
- :model/Card {card-id :id} {}
- :model/DashboardCard _ {:dashboard_id dash-id
- :card_id card-id
- :row 0
- :col 0}]
- (let [result (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id dash-id
- :slack_channel "no-such-channel"
- :schedule {:frequency "daily" :hour 9}})]
- (is (= {:output "no slack channel found with this name"} result))))))))
+(deftest a-dashboard-it-cannot-read-is-not-found-test
+ (testing "the model gets the same answer for a dashboard that is missing and one that the user can't read"
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
+ channel.settings/slack-cached-channels-and-usernames (constantly fake-channels)]
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Dashboard {dash-id :id} {}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (doseq [id [dash-id Integer/MAX_VALUE]
+ channel ["data-team" "unknown-channel"]]
+ (is (= {:output (str "Dashboard " id " was not found. It may not exist, or you may not have access "
+ "to it.\nCall `search` to find the entity you want and use an id from the results.")
+ :error {:class :recoverable :code ::recoverable.common/not-found}}
+ (subscribe (daily id channel)))))))))))
-(deftest create-dashboard-subscription-tool-errors-test
- (let [subscribe! #(agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id %
- :slack_channel "data-team"
- :schedule {:frequency "daily" :hour 9}})]
- (testing "a dashboard the user can't read goes back to the agent as output"
- (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)]
- (mt/with-non-admin-groups-no-root-collection-perms
- (mt/with-temp [:model/Dashboard {dash-id :id} {}]
- (mt/with-current-user (mt/user->id :rasta)
- (is (= "You don't have permissions to do that."
- (:output (subscribe! dash-id)))))))))
- (testing "an unexpected error propagates to the agent loop"
- (mt/with-dynamic-fn-redefs [agent-subscriptions/create-dashboard-subscription
- (fn [_] (throw (ex-info "boom" {})))]
- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom" (subscribe! 1)))))))
+(deftest refused-subscription-test
+ (testing "a subscription refusal names neither the dashboard nor its questions"
+ (mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
+ channel.settings/slack-cached-channels-and-usernames (constantly fake-channels)]
+ (mt/with-temp [:model/Collection collection {}
+ :model/Dashboard {dash-id :id} {}
+ :model/Card {card-id :id} {:collection_id (:id collection)}
+ :model/DashboardCard _ {:dashboard_id dash-id :card_id card-id}]
+ (testing "a hidden question prevents the subscription"
+ (doseq [channel ["data-team" "unknown-channel"]]
+ (mt/with-non-admin-groups-no-collection-perms collection
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (= {:class :unrecoverable
+ :code ::agent-subscriptions/dashboard-cards-not-readable
+ :user-message (str "You can't subscribe to this dashboard, because you don't have access to "
+ "every question on it.")}
+ (:error (subscribe (daily dash-id channel)))))))))
+ (testing "a 403 prevents the subscription"
+ (mt/with-dynamic-fn-redefs [pulse.api/create-pulse-with-perm-checks!
+ (fn [& _] (throw (ex-info "You don't have permissions to do that."
+ {:status-code 403})))]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (is (= {:class :unrecoverable
+ :code ::agent-subscriptions/no-subscription-permission
+ :user-message "You don't have permission to create a subscription for this dashboard."}
+ (:error (subscribe (daily dash-id "data-team"))))))))))))
diff --git a/test/metabase/metabot/tools_test.clj b/test/metabase/metabot/tools_test.clj
index 67d8a099d76..92746705d7b 100644
--- a/test/metabase/metabot/tools_test.clj
+++ b/test/metabase/metabot/tools_test.clj
@@ -389,8 +389,8 @@
(->> (t2/hydrate (t2/select :model/Pulse :dashboard_id dash-id) :channels)
(mapcat :channels)
(mapv (juxt :schedule_type :schedule_hour :schedule_day :schedule_frame))))]
- (binding [shared/*memory-atom* (atom {:context {:slack_channel_id "C123"}})]
- (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [ctx {:memory-atom (atom {:context {:slack_channel_id "C123"}})}]
(doseq [[schedule expected-cron expected-subscription]
[[{:frequency "hourly"}
"0 0 * * * ? *" [:hourly nil nil nil]]
@@ -413,5 +413,6 @@
{:dashboard_id inapp-dash-id :slack_channel "data-team"}
#(subscription-schedules inapp-dash-id) expected-subscription]]]
(testing (str tool)
- (is (= {:output "success"} (tool (assoc args :schedule schedule))))
+ (is (=? {:output #"^Created (alert|subscription) \d+ .*"}
+ (tools.core/call @tool (assoc args :schedule schedule) ctx)))
(is (= [expected] (stored-schedules))))))))))))))