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 { randomUUID } = require('crypto');
|
||||||
const fetch = require('node-fetch');
|
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) {
|
async function getConfig(context, buzzer) {
|
||||||
return await fetch(context.DOORMAN_URL + `/api/door/info?buzzer=${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) {
|
async function notifyDiscord(context, msg, u, optionalJsonStr) {
|
||||||
return fetch(context.DOORMAN_URL +
|
return lambdaFastHttp(context.DOORMAN_URL +
|
||||||
`/api/door/notify?discordUser=${encodeURIComponent(JSON.stringify(u))}&msg=${encodeURIComponent(JSON.stringify(msg))}&json=${encodeURIComponent(JSON.stringify(optionalJsonStr))}`,
|
`/api/door/notify?discordUser=${encodeURIComponent(JSON.stringify(u))}&msg=${encodeURIComponent(JSON.stringify(msg))}&json=${encodeURIComponent(JSON.stringify(optionalJsonStr))}`,
|
||||||
).catch(err => console.log(err))
|
).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));
|
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(
|
console.log(
|
||||||
invokeId, "UnlockPromise: I was the fastest, so I will attempt to notify discord users before resolving with unlock"
|
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);
|
resolve(twiml);
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
@ -113,7 +136,7 @@ exports.handler = async function(context, event, callback) {
|
|||||||
console.log(
|
console.log(
|
||||||
invokeId, "GracefulFallbackPromise: I was the fastest, so I will attempt to notify discord users before resolving with a call"
|
invokeId, "GracefulFallbackPromise: I was the fastest, so I will attempt to notify discord users before resolving with a call"
|
||||||
);
|
);
|
||||||
notifyAllDiscord(
|
await notifyAllDiscord(
|
||||||
context,
|
context,
|
||||||
config,
|
config,
|
||||||
`📞 Somebody buzzed the door and it dialed through to fallback phone numbers @ Door "${config.door}"`,
|
`📞 Somebody buzzed the door and it dialed through to fallback phone numbers @ Door "${config.door}"`,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user