Twilio firebase functions
1
// firebase functions:config:set twilio.sid="YOUR_ACCOUNT_SID" twilio.token="YOUR_AUTH_TOKEN"
2
// https://angularfirebase.com/lessons/sms-texting-with-twilio/
3
exports.makePhoneCall = functions.https.onCall((data, context) => {
4
if (!context.auth) {
5
throw new functions.https.HttpsError(
6
"failed-precondition",
7
"The function must be called " + "while authenticated."
8
);
9
}
10
const twillioPhoneNumber = "+12089532138";
11
const toPhoneNumber = data.phoneNumber;
12
​
13
const accountSid = functions.config().twilio.sid;
14
const authToken = functions.config().twilio.token;
15
const client = new twilio(accountSid, authToken);
16
​
17
const main = async () => {
18
try {
19
const call = await client.calls.create({
20
// method: 'GET',
21
// statusCallback: 'https://www.myapp.com/events',
22
// statusCallbackEvent: ['initiated', 'answered', 'completed'],
23
// statusCallbackMethod: 'POST',
24
// replace with https://www.twilio.com/console/twiml-bins/create,
25
// https://www.twilio.com/docs/voice/make-calls#manage-your-outbound-call
26
// record: true,
27
url: "http://demo.twilio.com/docs/voice.xml",
28
to: toPhoneNumber,
29
from: twillioPhoneNumber
30
});
31
​
32
console.log(call);
33
// should save callSid `call.sid` to firestore
34
return {
35
status: "OK",
36
text: `calling ${toPhoneNumber} from ${twillioPhoneNumber}`,
37
callSid: call.sid
38
};
39
} catch (e) {
40
console.error(e);
41
return { status: "error", error: e };
42
}
43
};
44
​
45
return main();
46
});
Copied!
Last modified 2yr ago
Copy link