clean up json output
All checks were successful
Build and push Doorman UI / API / docker (push) Successful in 1m22s

This commit is contained in:
Martin Dimitrov 2024-10-26 14:02:44 -07:00
parent 4cad1fbe2b
commit 641c64989c
2 changed files with 21 additions and 11 deletions

View File

@ -4,12 +4,20 @@ exports.handler = async function(context, event, callback) {
const discordPath = Runtime.getFunctions()['common/discord'].path;
const discord = require(discordPath);
let msg = event.msg;
if (event.json && event.json != "undefined") {
const fingerprint = JSON.parse(event.json);
msg += `\`\`\`# Unlocked by\n${JSON.stringify(fingerprint, null, 4)}\`\`\``;
}
console.log(event.msg);
// user must be in "Doorman" server
await discord.sendMessageToUser(context,
event.discordUser,
event.msg,
)
.catch((e) => console.error(e));
msg,
).catch((e) => console.error(e));
return callback(null, response);
};

View File

@ -6,24 +6,26 @@ const fetch = require('node-fetch');
exports.handler = async function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
let config = JSON.parse(event.config);
// should be a list of strings representing user ids in discord
let discordUsers = JSON.parse(event.config).discordUsers || [];
let discordUsers = config.discordUsers || [];
let bodyText;
let msg;
let optionalJson = event.fingerprint;
if (event.Method == 'doorman') {
bodyText = 'Doorman buzzed someone up!';
const fingerprint = JSON.parse(event.fingerprint);
bodyText += `\n\n\`\`\`${JSON.stringify(fingerprint, null, 4)}\`\`\``;
msg = '🔓 Doorman buzzed someone up';
} else if (event.Method == 'doorman-time-lock') {
bodyText = 'Doorman rejected a buzzer call due to time restriction';
msg = '🔒 Doorman rejected a buzzer call due to time restriction';
} else if (event.Method == 'call') {
bodyText = 'Somebody buzzed the door and it dialed through to a phone.';
msg = '📞 Somebody buzzed the door and it dialed through to a fallback phone number';
}
msg += ` @ Door "${config.door}"`;
let promises = discordUsers.map((u) =>
fetch(context.DOORMAN_URL + `/api/door/notify?discordUser=${u}&msg=${bodyText}`)
fetch(context.DOORMAN_URL + `/api/door/notify?discordUser=${u}&msg=${msg}&json=${optionalJson}`)
.catch(err => console.log(err))
);