/
example.ts
132 lines (114 loc) · 3.2 KB
/
example.ts
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
import twilio = require('../../');
import {MessageListInstanceCreateOptions} from '../../lib/rest/api/v2010/account/message';
const accountSid: string = process.env.TWILIO_ACCOUNT_SID || '';
const token: string = process.env.TWILIO_AUTH_TOKEN || '';
const client = twilio(accountSid, token);
let i: number = 0;
client.calls.each({
pageSize: 7,
callback: (call, done) => {
console.log(call.sid);
i++;
if (i === 10) {
done();
}
},
done: (err: Error) => {
console.log('je suis fini');
console.log(err);
}
});
client.calls.each({}, call => {
console.log(call.sid);
});
const from = process.env.FROM_NUMBER || '';
const to = process.env.TO_NUMBER || '';
const msgData: MessageListInstanceCreateOptions = {
from,
to,
body: 'create using callback'
};
// Send message using callback
client.messages.create(msgData, (err, result) => {
console.log('Created message using callback');
console.log(result.sid);
});
// Send message using promise
const promise = client.messages.create({
from: from,
to: to,
body: 'create using promises'
});
promise.then(message => {
console.log('Created message using promises');
console.log(message.sid);
});
// Create sip trunk
client.trunking.v1.trunks.create(
{
friendlyName: 'sip trunking'
},
(err, result) => {
console.log('Created trunk with friendly name');
console.log(result.sid);
console.log(result.friendlyName);
}
);
const promiseTrunk = client.trunking.v1.trunks.create({
friendlyName: 'promise trunking'
});
promiseTrunk.then(trunk => {
console.log('Created trunk with friendly name and promises');
console.log(trunk.sid);
console.log(trunk.friendlyName);
});
const trunkSid = 'TK7e37e59861c14bb80dde245cfaad5522';
// Fetch trunk sid using callback
client.trunking.v1.trunks(trunkSid).fetch((err, result) => {
console.log('Fetch trunk using callback');
console.log(result.sid);
});
// Fetch trunk using promise
const promiseTrunk2 = client.trunking.v1.trunks(trunkSid).fetch();
promiseTrunk2.then(trunk => {
console.log('Fetch trunk using promise');
console.log(trunk.sid);
});
// Update trunk using callback
client.trunking.v1.trunks(trunkSid).update(
{
friendlyName: 'callback trunk'
},
(err, result) => {
console.log('Updated using callbacks');
console.log(result.sid);
console.log(result.friendlyName);
}
);
// Update trunk using promise
const promiseTrunk3 = client.trunking.v1.trunks(trunkSid).update({
friendlyName: 'promise trunk'
});
promiseTrunk3.then(trunk => {
console.log('Updated trunk with friendly name and promises');
console.log(trunk.sid);
console.log(trunk.friendlyName);
});
// List messages using callbacks
client.messages.list({}, (err, messages) => {
console.log('Listing messages using callbacks');
messages.forEach(function(message){
console.log(message.sid);
});
});
// List messages using promises
const promiseMessage = client.messages.list();
promiseMessage.then(messages => {
console.log('Listing messages using promises');
messages.forEach(function(message){
console.log(message.sid);
});
});
const twiml = new twilio.twiml.VoiceResponse();
twiml.dial({}, '+12345678901');
twiml.play('https://demo.twilio.com/docs/classic.mp3');