From 5033fcd7dcaf400667bff7774fc392c8941b7498 Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Mon, 28 Apr 2025 20:13:26 +0000 Subject: [PATCH 1/3] chore(deps): remove deprecated slack package & install @slack/bolt --- functions/billing/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/billing/package.json b/functions/billing/package.json index 0c59d6c3e9..019e501983 100644 --- a/functions/billing/package.json +++ b/functions/billing/package.json @@ -16,9 +16,9 @@ "dependencies": { "@google-cloud/billing": "^4.0.0", "@google-cloud/compute": "^4.0.0", + "@slack/bolt": "^4.2.1", "google-auth-library": "^9.0.0", - "googleapis": "^143.0.0", - "slack": "^11.0.1" + "googleapis": "^143.0.0" }, "devDependencies": { "@google-cloud/functions-framework": "^3.0.0", From eeb6164413470ab6d288a462c6a0e6381c3f46f4 Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Mon, 28 Apr 2025 20:31:57 +0000 Subject: [PATCH 2/3] refactor(slack): migrate notifySlack function to use @slack/bolt --- functions/billing/index.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/functions/billing/index.js b/functions/billing/index.js index 38d3bdfe5a..c34c7121b2 100644 --- a/functions/billing/index.js +++ b/functions/billing/index.js @@ -23,13 +23,20 @@ const PROJECT_NAME = `projects/${PROJECT_ID}`; // [END functions_billing_limit] // [START functions_billing_slack] -const slack = require('slack'); +const {App} = require('@slack/bolt'); // TODO(developer) replace these with your own values const BOT_ACCESS_TOKEN = process.env.BOT_ACCESS_TOKEN || 'xxxx-111111111111-abcdefghidklmnopq'; +const SLACK_SIGNING_SECRET = + process.env.SLACK_SIGNING_SECRET || 'default-signing-secret'; const CHANNEL = process.env.SLACK_CHANNEL || 'general'; +const app = new App({ + token: BOT_ACCESS_TOKEN, + signingSecret: SLACK_SIGNING_SECRET, +}); + exports.notifySlack = async pubsubEvent => { const pubsubAttrs = pubsubEvent.attributes; const pubsubData = Buffer.from(pubsubEvent.data, 'base64').toString(); @@ -37,13 +44,17 @@ exports.notifySlack = async pubsubEvent => { pubsubAttrs )}, ${pubsubData}`; - await slack.chat.postMessage({ - token: BOT_ACCESS_TOKEN, - channel: CHANNEL, - text: budgetNotificationText, - }); - - return 'Slack notification sent successfully'; + try { + await app.client.chat.postMessage({ + token: BOT_ACCESS_TOKEN, + channel: CHANNEL, + text: budgetNotificationText, + }); + return 'Slack notification sent successfully'; + } catch (error) { + console.error('Error sending Slack message:', error); + throw error; + } }; // [END functions_billing_slack] From f8ffb61bb16d169a356b1c14274c1dbbafa47707 Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Mon, 28 Apr 2025 20:50:10 +0000 Subject: [PATCH 3/3] fix(slack): replace bolt with web-api to prevent server startup issues --- functions/billing/index.js | 12 +++--------- functions/billing/package.json | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/functions/billing/index.js b/functions/billing/index.js index c34c7121b2..c99b222e25 100644 --- a/functions/billing/index.js +++ b/functions/billing/index.js @@ -23,19 +23,14 @@ const PROJECT_NAME = `projects/${PROJECT_ID}`; // [END functions_billing_limit] // [START functions_billing_slack] -const {App} = require('@slack/bolt'); +const {WebClient} = require('@slack/web-api'); // TODO(developer) replace these with your own values const BOT_ACCESS_TOKEN = process.env.BOT_ACCESS_TOKEN || 'xxxx-111111111111-abcdefghidklmnopq'; -const SLACK_SIGNING_SECRET = - process.env.SLACK_SIGNING_SECRET || 'default-signing-secret'; const CHANNEL = process.env.SLACK_CHANNEL || 'general'; -const app = new App({ - token: BOT_ACCESS_TOKEN, - signingSecret: SLACK_SIGNING_SECRET, -}); +const slackClient = new WebClient(BOT_ACCESS_TOKEN); exports.notifySlack = async pubsubEvent => { const pubsubAttrs = pubsubEvent.attributes; @@ -45,8 +40,7 @@ exports.notifySlack = async pubsubEvent => { )}, ${pubsubData}`; try { - await app.client.chat.postMessage({ - token: BOT_ACCESS_TOKEN, + await slackClient.chat.postMessage({ channel: CHANNEL, text: budgetNotificationText, }); diff --git a/functions/billing/package.json b/functions/billing/package.json index 019e501983..7417efc7bc 100644 --- a/functions/billing/package.json +++ b/functions/billing/package.json @@ -16,7 +16,7 @@ "dependencies": { "@google-cloud/billing": "^4.0.0", "@google-cloud/compute": "^4.0.0", - "@slack/bolt": "^4.2.1", + "@slack/web-api": "^7.9.1", "google-auth-library": "^9.0.0", "googleapis": "^143.0.0" },