deliveries/index.mjs

53 lines
1.6 KiB
JavaScript

import { fs } from 'fs';
import { v4 } from 'uuid';
import { child_process } from 'child_process';
import {
SecretsManagerClient,
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
export const handler = async (event) => {
console.info("Event: ", event);
let result = "did not send";
if (event.subject.includes("You have deliveries") || event.subject.includes("You have a delivery")) {
// Trigger home notification
console.info("Matched Subject");
const command = `ssh ${process.env.INTERNAL_HOST} "/usr/bin/tmux neww -d \"/usr/bin/mplayer '${process.env.INTERNAL_FILE}'\""`;
const tempFile = `./${v4()}.key`;
const keyClient = new SecretsManagerClient({ region: "us-west-2" });
let keyResponse;
try {
console.info("Get Key");
keyResponse = await keyClient.send(new GetSecretValueCommand({
SecretId: process.env.TRIGGER_SECRET
}));
keyResponse = JSON.parse(keyResponse.SecretString);
keyResponse = keyResponse["carbon-key"].replaceAll(" ", "\n");
fs.writeFileSync(tempFile, keyResponse);
} catch (err) {
console.warn(err);
result = err;
}
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;
}else{
const response = {
statusCode: 200,
body: {
result
},
};
return response;
}
};