I have this file which is a job called officeChatArchiver.ts and this will eventually be…
- User
I have this file which is a job called officeChatArchiver.ts and this will eventually be called as a cron job using aws lambda, so that it will be called everyday. However I want to be able to execute this on command, so that I can test that it functions properly:
import { deleteOfficeChatByName, getOfficeChatsToArchive } from '@/services/officeChat'; import { WebClient } from '@slack/web-api'; import { Callback, Context, Handler } from 'aws-lambda';
const webClient = new WebClient(process.env.SLACK_BOT_TOKEN);
export const handler: Handler = async ( event: any, context: Context, callback: Callback ) => { try { // Query the database for all channel names to archive const channels = await getOfficeChatsToArchive();
if (!channels) { callback(null, { statusCode: 200, body: 'No channels to archive' }); console.log('No channels to archive'); return; } // Archive each channel using the Slack Web API for (const channel of channels) { await webClient.conversations .info({ channel: channel.channel }) .then(async (info) => { if (!info.channel?.is_archived) { await webClient.conversations.archive({ channel: channel.channel }); } await deleteOfficeChatByName(channel.name); }); } callback(null, { statusCode: 200, body: 'Channels archived successfully.' });} catch (error) { console.error(error); callback(error as string | Error | null | undefined, { statusCode: 500, body: 'Error archiving channels.' }); } };
- Assistant
To execute this file on command, you can either run it locally or deploy it as a single Lambda function in AWS.
To run it locally, you will need to set up an environment that replicates the Lambda environment, and then run the command using Node.js. First, make sure you have installed all the required packages for this file:
npm install @slack/web-api aws-lambdaThen, create a
.envfile in the same directory asofficeChatArchiver.ts, and add the following line:SLACK_BOT_TOKEN=<your_slack_bot_token>Replace
<your_slack_bot_token>with your actual Slack bot token. - User
I can't use node to run a typescript script
- Assistant
You're right, apologies for my mistake. To run the TypeScript script, you'll need to use a transpiler like
tscto compile the TypeScript code into executable JavaScript code.First, make sure you have installed TypeScript globally:
npm install -g typescriptThen, create a
tsconfig.jsonfile in the same directory asofficeChatArchiver.ts:{ "compilerOptions": { "target": "es2017", "module": "commonjs", "esModuleInterop": true, "outDir": "dist" }, "include": ["officeChatArchiver.ts"] }This file specifies the TypeScript compiler options and includes the
officeChatArchiver.tsfile.Next, compile the TypeScript file by running the command:
tscThis will transpile your TypeScript code into JavaScript code and output it to the
distdirectory specified intsconfig.json.Now, you can execute the JavaScript file using Node.js:
node dist/officeChatArchiver.jsThis should execute the file and produce an output in your console. You can also pass in any required CLI arguments the same way as you would with a regular JavaScript file.
- User
I have multiple files in a runJobs folder, like runOfficeChatArchiver & runBookingDeletion. I want to create an npm command in my package.json like
npm run jobsthat will list all of the jobs available to run individually or an all option which will run them all one after the other - Assistant
To create an npm command in your
package.json, you can use thescriptsfield. Here's an example script that allows you to run all the jobs or specify one to run:{ "name": "my-app", "version": "1.0.0", "scripts": { "jobs": "echo 'Available jobs: runOfficeChatArchiver, runBookingDeletion';", "runOfficeChatArchiver": "node dist/runJobs/runOfficeChatArchiver.js", "runBookingDeletion": "node dist/runJobs/runBookingDeletion.js", "allJobs": "npm run runOfficeChatArchiver && npm run runBookingDeletion" }, "dependencies": { "aws-sdk": "^2.100.0", "@slack/web-api": "^5.8.1" }, "devDependencies": { "typescript": "^4.4.4", "@types/node": "^16.9.1" } }In the above
package.json, the"jobs"script just prints a list of available jobs to the console. The"runOfficeChatArchiver"and"runBookingDeletion"scripts run the individual jobs using their respective scripts in thedist/runJobsfolder.Finally, the
"allJobs"script runs both jobs sequentially by running each one in turn using the command. - User
How could I make it so the user could select the job they want to run by using up/down keys in the terminal
- Assistant