The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Share Metabot dashboard subscription code (#83409)
Chris Truterb6907dafbbc4
diff --git a/src/metabase/metabot/tools/create_dashboard_subscription.clj b/src/metabase/metabot/tools/create_dashboard_subscription.cljindex 5d0b52f96be..27e6298ddf9 100644--- a/src/metabase/metabot/tools/create_dashboard_subscription.clj+++ b/src/metabase/metabot/tools/create_dashboard_subscription.clj@@ -1,79 +1,11 @@(ns metabase.metabot.tools.create-dashboard-subscription(: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.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.pulse.api :as pulse.api]- [metabase.util.malli :as mu]- [toucan2.core :as t2]))--(defn- make-slack-channel- "Build a pulse channel for Slack delivery."- [schedule slack-channel]- (merge {:channel_type :slack- :enabled true- :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)))- 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"}-- (nil? channel-name)- {:error "no slack channel found with this name"}-- :else- (do (pulse.api/create-pulse-with-perm-checks!- cards- [(make-slack-channel schedule channel-name)]- pulse-data)- {:output "success"}))))--(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"}-- (not (channel.settings/slack-configured?))- {:error "slack is not configured. Ask an admin to connect slack in Metabase settings."}-- (empty? slack-channel)- {:error "slack_channel is required"}-- :else- (try- (create-dashboard-subscription* args)- (catch Exception e- (-> (metabot.tools.u/handle-agent-error e)- (set/rename-keys {:output :error}))))))+ [metabase.util.malli :as mu]))(mu/defn ^{:tool-name "create_dashboard_subscription":scope scope/agent-dashboard-subscribe}@@ -98,7 +30,7 @@(when-not slack-channel-id(throw (ex-info "This tool can only be used from a Slack channel"{:agent-error? true})))- (let [result (create-dashboard-subscription+ (let [result (tools.subscriptions/create-dashboard-subscription{:dashboard-id dashboard_id:schedule schedule:slack-channel slack-channel-id})]