buzz-me-in 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +68 -0
- package/LICENSE +21 -0
- package/README.md +452 -0
- package/homekit.js +789 -0
- package/intercom.service +48 -0
- package/package.json +73 -0
- package/server.js +420 -0
- package/src/core/config.js +133 -0
- package/src/core/log.js +132 -0
- package/src/core/media-stream.js +335 -0
- package/src/core/mulaw-audio.js +233 -0
- package/src/core/return-audio-sdp.js +95 -0
- package/src/core/safe-equal.js +22 -0
- package/src/core/state.js +149 -0
- package/src/core/stream-auth.js +130 -0
- package/src/core/twilio-ids.js +27 -0
- package/src/core/types.js +145 -0
- package/src/core/ws-events-schema.js +110 -0
- package/twilio-api.js +53 -0
package/twilio-api.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Twilio REST API helpers.
|
|
5
|
+
* All functions accept a callSid string and return a Promise.
|
|
6
|
+
*
|
|
7
|
+
* Required environment variables:
|
|
8
|
+
* TWILIO_ACCOUNT_SID – e.g. ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
9
|
+
* TWILIO_AUTH_TOKEN – from Twilio Console
|
|
10
|
+
* TWILIO_PHONE_NUMBER – the Twilio number that receives the intercom call
|
|
11
|
+
* (only needed if you ever want to initiate outbound calls)
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const twilio = require('twilio');
|
|
15
|
+
const config = require('./src/core/config');
|
|
16
|
+
const { createLogger } = require('./src/core/log');
|
|
17
|
+
|
|
18
|
+
const logger = createLogger({ component: 'twilio-api' });
|
|
19
|
+
let _client = null;
|
|
20
|
+
function client() {
|
|
21
|
+
if (!_client) {
|
|
22
|
+
_client = twilio(config.twilioAccountSid, config.twilioAuthToken);
|
|
23
|
+
}
|
|
24
|
+
return _client;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Terminate the call immediately.
|
|
29
|
+
* Called when the HomeKit session is dismissed by the user.
|
|
30
|
+
*/
|
|
31
|
+
async function hangUpCall(callSid) {
|
|
32
|
+
logger.info('Hanging up call', { event: 'hangup', callSid });
|
|
33
|
+
try {
|
|
34
|
+
return await client().calls(callSid).update({
|
|
35
|
+
twiml: `<Response><Hangup/></Response>`,
|
|
36
|
+
});
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (isCallAlreadyEndedError(error)) {
|
|
39
|
+
return { alreadyEnded: true, callSid };
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isCallAlreadyEndedError(error) {
|
|
46
|
+
if (!error) return false;
|
|
47
|
+
// Twilio error 21220: call cannot be updated because it is not in progress.
|
|
48
|
+
if (error.code === 21220) return true;
|
|
49
|
+
// Fallback for SDK errors that carry only a message.
|
|
50
|
+
return typeof error.message === 'string' && error.message.includes('Call is not in-progress');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = { hangUpCall };
|