-
Notifications
You must be signed in to change notification settings - Fork 517
/
example.js
140 lines (122 loc) · 3.38 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var Twilio = require("../lib");
var accountSid = process.env.TWILIO_ACCOUNT_SID;
var token = process.env.TWILIO_AUTH_TOKEN;
// Uncomment the following line to specify a custom CA bundle for HTTPS requests:
// process.env.TWILIO_CA_BUNDLE = '/path/to/cert.pem';
// You can also set this as a regular environment variable outside of the code
var twilio = new Twilio(accountSid, token);
var i = 0;
// Callback as second parameter
twilio.calls.each({
pageSize: 7,
callback: function (call, done) {
console.log(call.sid);
i++;
if (i === 10) {
done();
}
},
done: function (error) {
console.log("je suis fini");
console.log(error);
},
});
// Callback as first parameter
twilio.calls.each(function (call) {
console.log(call.sid);
});
var from = process.env.FROM_NUMBER;
var to = process.env.TO_NUMBER;
// Send message using callback
twilio.messages.create(
{
from: from,
to: to,
body: "create using callback",
},
function (err, result) {
console.log("Created message using callback");
console.log(result.sid);
}
);
// Send message using promise
var promise = twilio.messages.create({
from: from,
to: to,
body: "create using promises",
});
promise.then(function (message) {
console.log("Created message using promises");
console.log(message.sid);
});
// Create sip trunk using callback as first parameter
twilio.trunking.v1.trunks.create(function (err, result) {
console.log("Created default trunk");
console.log(result.sid);
});
// Create sip trunk using callback as second parameter
twilio.trunking.v1.trunks.create(
{
friendlyName: "sip trunking",
},
function (err, result) {
console.log("Created trunk with friendly name");
console.log(result.sid);
console.log(result.friendlyName);
}
);
promise = twilio.trunking.v1.trunks.create({
friendlyName: "promise trunking",
});
promise.then(function (trunk) {
console.log("Created trunk with friendly name and promises");
console.log(trunk.sid);
console.log(trunk.friendlyName);
});
var trunkSid = "TK7e37e59861c14bb80dde245cfaad5522";
// Fetch trunk sid using callback
twilio.trunking.v1.trunks(trunkSid).fetch(function (err, result) {
console.log("Fetch trunk using callback");
console.log(result.sid);
});
// Fetch trunk using promise
promise = twilio.trunking.v1.trunks(trunkSid).fetch();
promise.then(function (trunk) {
console.log("Fetch trunk using promise");
console.log(trunk.sid);
});
// Update trunk using callback
twilio.trunking.v1.trunks(trunkSid).update(
{
friendlyName: "callback trunk",
},
function (err, result) {
console.log("Updated using callbacks");
console.log(result.sid);
console.log(result.friendlyName);
}
);
// Update trunk using promise
promise = twilio.trunking.v1.trunks(trunkSid).update({
friendlyName: "promise trunk",
});
promise.then(function (trunk) {
console.log("Updated trunk with friendly name and promises");
console.log(trunk.sid);
console.log(trunk.friendlyName);
});
// List messages using callbacks
twilio.messages.list(function (err, messages) {
console.log("Listing messages using callbacks");
messages.forEach(function (message) {
console.log(message.sid);
});
});
// List messages using promises
promise = twilio.messages.list();
promise.then(function (messages) {
console.log("Listing messages using promises");
messages.forEach(function (message) {
console.log(message.sid);
});
});