Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs-infra] move feedback to ESM #41381

Merged
merged 4 commits into from Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,7 +1,8 @@
const querystring = require('node:querystring');
const { App, AwsLambdaReceiver } = require('@slack/bolt');
const { JWT } = require('google-auth-library');
const { sheets } = require('@googleapis/sheets');
import querystring from 'node:querystring';
import { App, AwsLambdaReceiver, BlockAction, ButtonAction } from '@slack/bolt';
import { JWT } from 'google-auth-library';
import { sheets } from '@googleapis/sheets';
import { Handler } from '@netlify/functions';

const X_FEEBACKS_CHANNEL_ID = 'C04U3R2V9UK';
const JOY_FEEBACKS_CHANNEL_ID = 'C050VE13HDL';
Expand Down Expand Up @@ -35,7 +36,7 @@ const spreadSheetsIds = {

// Setup of the slack bot (taken from https://slack.dev/bolt-js/deployments/aws-lambda)
const awsLambdaReceiver = new AwsLambdaReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
signingSecret: process.env.SLACK_SIGNING_SECRET!,
});

const app = new App({
Expand All @@ -45,37 +46,44 @@ const app = new App({
});

// Define slack actions to answer
app.action('delete_action', async ({ ack, body, client, logger }) => {
app.action<BlockAction<ButtonAction>>('delete_action', async ({ ack, body, client, logger }) => {
try {
await ack();

const {
user: { username },
channel: { id: channelId },
channel,
message,
actions: [{ value }],
} = body;

const channelId = channel?.id;

const { comment, currentLocationURL = '', commmentSectionURL = '' } = JSON.parse(value);

const googleAuth = new JWT({
email: 'service-account-804@docs-feedbacks.iam.gserviceaccount.com',
key: process.env.G_SHEET_TOKEN.replace(/\\n/g, '\n'),
key: process.env.G_SHEET_TOKEN!.replace(/\\n/g, '\n'),
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
const service = sheets({ version: 'v4', auth: googleAuth });

await service.spreadsheets.values.append({
// @ts-ignore
service.spreadsheets.values.append({
spreadsheetId: spreadSheetsIds.forLater,
range: 'Deleted messages!A:D',
valueInputOption: 'USER_ENTERED',
resource: {
values: [[username, comment, currentLocationURL, commmentSectionURL]],
},
});

if (!channelId) {
throw Error('feedback-management: Unknonw channel Id');
}
await client.chat.delete({
channel: channelId,
ts: message.ts,
ts: message!.ts,
as_user: true,
token: process.env.SLACK_BOT_TOKEN,
});
Expand All @@ -89,31 +97,37 @@ app.action('save_message', async ({ ack, body, client, logger }) => {
await ack();
const {
user: { username },
channel: { id: channelId },
channel,
message,
actions: [{ value }],
} = body;
} = body as BlockAction<ButtonAction>;

const channelId = channel?.id;
const { comment, currentLocationURL = '', commmentSectionURL = '' } = JSON.parse(value);

const googleAuth = new JWT({
email: 'service-account-804@docs-feedbacks.iam.gserviceaccount.com',
key: process.env.G_SHEET_TOKEN.replace(/\\n/g, '\n'),
key: process.env.G_SHEET_TOKEN!.replace(/\\n/g, '\n'),
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
const service = sheets({ version: 'v4', auth: googleAuth });

await service.spreadsheets.values.append({
// @ts-ignore
service.spreadsheets.values.append({
spreadsheetId: spreadSheetsIds.forLater,
range: 'Sheet1!A:D',
valueInputOption: 'USER_ENTERED',
resource: {
updates: {
values: [[username, comment, currentLocationURL, commmentSectionURL]],
},
});
await client.chat.postMessage({

if (!channelId) {
throw Error('feedback-management: Unknonw channel Id');
}
client.chat.postMessage({
channel: channelId,
thread_ts: message.ts,
thread_ts: message!.ts,
as_user: true,
text: `Saved in <https://docs.google.com/spreadsheets/d/${spreadSheetsIds.forLater}/>`,
});
Expand All @@ -122,11 +136,8 @@ app.action('save_message', async ({ ack, body, client, logger }) => {
}
});

/**
* @param {object} event
* @param {object} context
*/
exports.handler = async (event, context, callback) => {
// eslint-disable-next-line import/prefer-default-export
export const handler: Handler = async (event, context, callback) => {
if (event.httpMethod !== 'POST') {
return { statusCode: 404 };
}
Expand Down Expand Up @@ -227,8 +238,9 @@ from ${commmentSectionURL}
unfurl_media: false,
});
} else {
const handler = await awsLambdaReceiver.start();
return handler(event, context, callback);
const awsHandler = await awsLambdaReceiver.start();
// @ts-ignore
return awsHandler(event, context, callback);
}
} catch (error) {
// eslint-disable-next-line no-console
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -84,6 +84,7 @@
"dependencies": {
"@googleapis/sheets": "^5.0.5",
"@slack/bolt": "^3.17.1",
"@netlify/functions": "^2.6.0",
"execa": "^8.0.1",
"google-auth-library": "^9.6.3"
},
Expand Down
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.