Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions functions/billing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,32 @@ const PROJECT_NAME = `projects/${PROJECT_ID}`;
// [END functions_billing_limit]

// [START functions_billing_slack]
const slack = require('slack');
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 CHANNEL = process.env.SLACK_CHANNEL || 'general';

const slackClient = new WebClient(BOT_ACCESS_TOKEN);

exports.notifySlack = async pubsubEvent => {
const pubsubAttrs = pubsubEvent.attributes;
const pubsubData = Buffer.from(pubsubEvent.data, 'base64').toString();
const budgetNotificationText = `${JSON.stringify(
pubsubAttrs
)}, ${pubsubData}`;

await slack.chat.postMessage({
token: BOT_ACCESS_TOKEN,
channel: CHANNEL,
text: budgetNotificationText,
});

return 'Slack notification sent successfully';
try {
await slackClient.chat.postMessage({
channel: CHANNEL,
text: budgetNotificationText,
});
return 'Slack notification sent successfully';
} catch (error) {
console.error('Error sending Slack message:', error);
throw error;
}
};
// [END functions_billing_slack]

Expand Down
4 changes: 2 additions & 2 deletions functions/billing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"dependencies": {
"@google-cloud/billing": "^4.0.0",
"@google-cloud/compute": "^4.0.0",
"@slack/web-api": "^7.9.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",
Expand Down
Loading