2024-10-05 03:09:24 +00:00
import SSH2Promise from "ssh2-promise" ;
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 ) => {
2024-10-05 00:14:22 +00:00
console . info ( "Event: " , event ) ;
2024-10-04 22:41:01 +00:00
let result = "did not send" ;
2024-11-15 01:19:33 +00:00
if ( event . subject . includes ( "You have deliveries" ) || event . subject . includes ( "You have a delivery" ) || event . subject . includes ( "You have a package to pick up" ) ) {
2024-10-04 22:41:01 +00:00
// Trigger home notification
2024-10-05 00:14:22 +00:00
console . info ( "Matched Subject" ) ;
2024-10-05 03:09:24 +00:00
const command = ` ssh ${ process . env . INTERNAL _HOST } "/home/liz/.sound ' ${ process . env . INTERNAL _FILE } '" ` ;
2024-10-04 23:18:08 +00:00
const keyClient = new SecretsManagerClient ( { region : "us-west-2" } ) ;
let keyResponse ;
try {
2024-10-05 00:14:22 +00:00
console . info ( "Get Key" ) ;
2024-10-05 03:09:24 +00:00
let keyResponseObj = await keyClient . send ( new GetSecretValueCommand ( {
2024-10-04 23:18:08 +00:00
SecretId : process . env . TRIGGER _SECRET
} ) ) ;
2024-10-05 03:09:24 +00:00
keyResponseObj = JSON . parse ( keyResponseObj . SecretString ) ;
keyResponse = keyResponseObj [ "carbon-key" ] ;
keyResponse = keyResponse . replaceAll ( ':' , "\n" ) ;
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" ) ;
2024-10-05 03:09:24 +00:00
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" ) ;
2024-10-04 22:41:01 +00:00
}
2024-10-05 03:09:24 +00:00
const response = {
statusCode : 200 ,
body : {
result
} ,
} ;
return response ;
2024-10-04 22:41:01 +00:00
} ;