deliveries/index.mjs

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-10-05 00:13:39 +00:00
import { fs } from 'fs';
import { v4 } from 'uuid';
import { child_process } from 'child_process';
2024-10-04 23:18:08 +00:00
import {
SecretsManagerClient,
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
2024-10-04 22:41:01 +00:00
export const handler = async (event) => {
let result = "did not send";
2024-10-05 00:13:39 +00:00
if (event.subject.includes("You have deliveries") || event.subject.includes("You have a delivery")) {
2024-10-04 22:41:01 +00:00
// Trigger home notification
2024-10-04 23:18:08 +00:00
console.info("Matched Subject, pull key");
2024-10-05 00:13:39 +00:00
const command = `ssh ${process.env.INTERNAL_HOST} "/usr/bin/tmux neww -d \"/usr/bin/mplayer '${process.env.INTERNAL_FILE}'\""`;
const tempFile = `./${v4()}.key`;
2024-10-04 23:18:08 +00:00
const keyClient = new SecretsManagerClient({ region: "us-west-2" });
let keyResponse;
try {
keyResponse = await keyClient.send(new GetSecretValueCommand({
SecretId: process.env.TRIGGER_SECRET
}));
2024-10-04 23:41:54 +00:00
keyResponse = JSON.parse(keyResponse.SecretString);
keyResponse = keyResponse["carbon-key"].replaceAll(" ", "\n");
2024-10-05 00:13:39 +00:00
fs.writeFileSync(tempFile, keyResponse);
2024-10-04 23:18:08 +00:00
} catch (err) {
console.warn(err);
result = err;
}
2024-10-05 00:13:39 +00:00
console.info("Run SSH");
let SSHResult = child_process.execSync(`ssh -i ${tempFile} ${process.env.TRIGGER_USERNAME}@${process.env.TRIGGER_HOST} "${command}"`);
console.info(SSHResult.toString());
fs.unlinkSync(tempFile);
console.info("Finished SSH");
const response = {
statusCode: 200,
body: {
result
},
};
return response;
2024-10-04 23:41:54 +00:00
}else{
const response = {
statusCode: 200,
body: {
result
},
};
return response;
2024-10-04 22:41:01 +00:00
}
};