await transmission of discord notif request instead of waiting for it to complete
This commit is contained in:
parent
736712ee79
commit
01fce99766
@ -3,6 +3,29 @@
|
||||
*/
|
||||
const { randomUUID } = require('crypto');
|
||||
const fetch = require('node-fetch');
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
|
||||
/**
|
||||
* Helper function to do an HTTP request and just await transmission, but not await a response.
|
||||
* ref: https://www.sensedeep.com/blog/posts/stories/lambda-fast-http.html
|
||||
* @param {*} url - the URL to do HTTP request to
|
||||
* @returns promise signalling HTTP request has been transmitted
|
||||
*/
|
||||
async function lambdaFastHttp(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let req;
|
||||
|
||||
if (url.startsWith("https://")) {
|
||||
req = https.request(URL.parse(url));
|
||||
} else {
|
||||
req = http.request(URL.parse(url));
|
||||
}
|
||||
req.end(null, null, () => {
|
||||
resolve(req);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function getConfig(context, buzzer) {
|
||||
return await fetch(context.DOORMAN_URL + `/api/door/info?buzzer=${buzzer}`)
|
||||
@ -12,13 +35,13 @@ async function getConfig(context, buzzer) {
|
||||
});
|
||||
}
|
||||
|
||||
function notifyDiscord(context, msg, u, optionalJsonStr) {
|
||||
return fetch(context.DOORMAN_URL +
|
||||
async function notifyDiscord(context, msg, u, optionalJsonStr) {
|
||||
return lambdaFastHttp(context.DOORMAN_URL +
|
||||
`/api/door/notify?discordUser=${encodeURIComponent(JSON.stringify(u))}&msg=${encodeURIComponent(JSON.stringify(msg))}&json=${encodeURIComponent(JSON.stringify(optionalJsonStr))}`,
|
||||
).catch(err => console.log(err))
|
||||
}
|
||||
|
||||
function notifyAllDiscord(context, config, msg, optionalJsonStr) {
|
||||
async function notifyAllDiscord(context, config, msg, optionalJsonStr) {
|
||||
return notifyDiscord(context, config.discordUsers.map(() => msg), config.discordUsers, config.discordUsers.map(() => optionalJsonStr).filter(Boolean));
|
||||
}
|
||||
|
||||
@ -90,7 +113,7 @@ exports.handler = async function(context, event, callback) {
|
||||
console.log(
|
||||
invokeId, "UnlockPromise: I was the fastest, so I will attempt to notify discord users before resolving with unlock"
|
||||
);
|
||||
notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, JSON.stringify(body.fingerprint));
|
||||
await notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, JSON.stringify(body.fingerprint));
|
||||
resolve(twiml);
|
||||
} else {
|
||||
console.log(
|
||||
@ -113,7 +136,7 @@ exports.handler = async function(context, event, callback) {
|
||||
console.log(
|
||||
invokeId, "GracefulFallbackPromise: I was the fastest, so I will attempt to notify discord users before resolving with a call"
|
||||
);
|
||||
notifyAllDiscord(
|
||||
await notifyAllDiscord(
|
||||
context,
|
||||
config,
|
||||
`📞 Somebody buzzed the door and it dialed through to fallback phone numbers @ Door "${config.door}"`,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user