deliveries/index.mjs

50 lines
1.6 KiB
JavaScript

import SSH2Promise from "ssh2-promise";
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") || event.subject.includes("You have a package to pick up")) {
// Trigger home notification
console.info("Matched Subject");
const command = `ssh ${process.env.INTERNAL_HOST} "/home/liz/.sound '${process.env.INTERNAL_FILE}'"`;
const keyClient = new SecretsManagerClient({ region: "us-west-2" });
let keyResponse;
try {
console.info("Get Key");
let keyResponseObj = await keyClient.send(new GetSecretValueCommand({
SecretId: process.env.TRIGGER_SECRET
}));
keyResponseObj = JSON.parse(keyResponseObj.SecretString);
keyResponse = keyResponseObj["carbon-key"];
keyResponse = keyResponse.replaceAll(':', "\n");
} catch (err) {
console.warn(err);
result = err;
}
console.info("Run SSH");
let ssh = new SSH2Promise({
host: process.env.TRIGGER_HOST,
username: process.env.TRIGGER_USERNAME,
privateKey: keyResponse
});
await ssh.connect();
console.info("Connected via SSH");
let sshResponse = await ssh.exec(command);
console.info("SSH Response: ", sshResponse);
result = sshResponse;
ssh.close();
console.info("Closed SSH");
}
const response = {
statusCode: 200,
body: {
result
},
};
return response;
};