-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathexample.js
More file actions
192 lines (172 loc) · 5.19 KB
/
Copy pathexample.js
File metadata and controls
192 lines (172 loc) · 5.19 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var Twilio = require("../lib");
var { RestException } = 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 with RestException handling
twilio.messages.create(
{
from: from,
to: to,
body: "create using callback",
},
function (err, result) {
if (err) {
if (err instanceof RestException) {
console.log(`Twilio Error ${err.code}: ${err.message}`);
console.log(`Status: ${err.status}`);
console.log(`More info: ${err.moreInfo}`);
} else {
console.log("Other error:", err);
}
return;
}
console.log("Created message using callback");
console.log(result.sid);
}
);
// Send message using promise with RestException handling
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);
})
.catch(function (error) {
if (error instanceof RestException) {
console.log(`Twilio Error ${error.code}: ${error.message}`);
console.log(`Status: ${error.status}`);
console.log(`More info: ${error.moreInfo}`);
} else {
console.log("Other error:", error);
}
});
// 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);
});
});
// Example: Create message with HTTP response info (status code and headers)
// Using createWithHttpInfo to get response headers like rate limits, request IDs
promise = twilio.messages.createWithHttpInfo({
from: from,
to: to,
body: "create with HTTP info",
});
promise.then(function (response) {
console.log("Created message with HTTP info");
console.log("Message SID:", response.body.sid);
console.log("Status Code:", response.statusCode);
console.log("Response Headers:", response.headers);
console.log(
"Rate Limit Remaining:",
response.headers["x-ratelimit-remaining"]
);
});
// Example: List messages with HTTP response info
// Using listWithHttpInfo to get first page metadata including rate limits
promise = twilio.messages.listWithHttpInfo({ limit: 5 });
promise.then(function (response) {
console.log("Listed messages with HTTP info");
console.log("Total messages retrieved:", response.body.length);
console.log("Status Code:", response.statusCode);
console.log("Response Headers:", response.headers);
response.body.forEach(function (message) {
console.log(message.sid);
});
});