47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
function jsonMsgSuffix(jsonString) {
|
|
if (jsonString === undefined) {
|
|
return "";
|
|
}
|
|
try {
|
|
const fingerprint = JSON.parse(jsonString);
|
|
return `\`\`\`# Unlocked by\n${JSON.stringify(fingerprint, null, 4)}\`\`\``;
|
|
} catch (e) {
|
|
return `\`\`\`# Unlocked by\n# WARN: Unknown or corrupt raw fingerprint:\n ${jsonString}\`\`\``;
|
|
}
|
|
}
|
|
|
|
exports.handler = async function(context, event, callback) {
|
|
const response = new Twilio.Response();
|
|
const discordPath = Runtime.getFunctions()['common/discord'].path;
|
|
const discord = require(discordPath);
|
|
|
|
let users = event.discordUser;
|
|
let msgs = event.msg;
|
|
let jsons = event.json;
|
|
let promises;
|
|
|
|
try {
|
|
users = JSON.parse(users);
|
|
msgs = JSON.parse(msgs);
|
|
jsons = JSON.parse(jsons);
|
|
|
|
promises = msgs.map((msg, i) =>
|
|
discord.sendMessageToUser(
|
|
context,
|
|
users[i],
|
|
msg + jsonMsgSuffix(jsons[i])
|
|
).catch(e => console.error(e))
|
|
);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
setTimeout(() => {
|
|
console.log("Ungraceful finish: running out of time");
|
|
callback(null, response);
|
|
}, 5000);
|
|
|
|
await Promise.all(promises);
|
|
return callback(null, response);
|
|
};
|