Skip to content

Instantly share code, notes, and snippets.

View zengfenfei's full-sized avatar

Kevin Zeng zengfenfei

  • Xiamen
View GitHub Profile
// Builds the url: '/account/~/extension/~', and make a PUT request to it
rc.account().extension().put({ status: "Enabled" }).then(function () {
console.log("Success to update extension.");
}).catch(function () {
console.error("Fail to update extension.");
});
// Builds the url: '/account/theAccountId/extension', and make a GET request to it
rc.account("theAccountId").extension().list().then(function (extensions) {
console.log("The list of extension info", extensions.records);
}).catch(function (e) {
console.error("Get extension list error", e);
});
// Builds the url: '/account/~/extension/theExtensionId', and make a GET request to it
rc.account().extension('theExtensionId').get().then(function (extInfo) {
console.log("The extension info", extInfo);
}).catch(function (e) {
console.error("Get extension error", e);
});
import * as fs from "fs";
// Builds the url: '/account/~/extension/~/fax', and make a POST request to it
rc.account().extension().fax().post({
to: [{ phoneNumber: "{receiverPhoneNumber}" }],
faxResolution: 'High'
}, [ // Second argument is an array of attachments, attachment can be string, Blob, node readable stream.
"{Message text}",
fs.createReadStream("{filePath}") // In node only
]);
// Builds the url: '/account/~/extension/~/sms', and make a POST request to it
rc.account().extension().sms().post({
to: [{
phoneNumber: "{receiverPhoneNumber}"
}],
from: {
phoneNumber: "{yourSmsNumber}"
},
text: "Sms content"
}).then(function (messageInfo) {
let dateFrom = new Date(Date.now() - 24 * 60 * 60 * 1000); // A day ago
// Builds the url: '/account/~/extension/~/call-log/', and make a GET request to it
rc.account().extension().callLog().list({ dateFrom: dateFrom.toISOString() }).then(results => {
console.log("Recent call logs", results.records);
}, e => {
console.error("Fail to get call logs", e);
});
// Builds the url: '/account/~/extension/~/active-calls/', and make a GET request to it
rc.account().extension().activeCalls().list({
page: 1, // Get the 1st page of the result
direction: "Inbound" // Specify the direction of the call, omit to get all directions
}).then(results => {
console.log("Active calls", results.records);
}, e => {
console.error("Fail to get active calls", e);
});
let subscription = rc.createSubscription();
subscription.onMessage(msg => {
let presenceEvt = msg.body;
console.log('>> telephonyStatus', presenceEvt.telephonyStatus);
console.log('>> activeCalls', presenceEvt.activeCalls);
console.log('@@ presence event', presenceEvt);
});
subscription.subscribe(['/account/~/extension/~/presence?detailedTelephonyState=true'])
.then(subscription => {
console.log('Subscription created', subscription);
// Builds the url: '/account/~/extension/~/ringout', and make a POST request to it
rc.account().extension().ringout().post({
from: { phoneNumber: 'xxx' },
to: { phoneNumber: 'xxx' },
callerId: { phoneNumber: 'xxx' }
}).then(ringout => {
console.log('Ringout sucess', ringout);
// To check the call status: `rc.account().extension().ringout(ringout.id).get();`
}, e => {
console.error('Fail to ringout', e);
subscription.subscribe(['/account/~/extension/~/presence?detailedTelephonyState=true']).then(subscription => {
console.log('Subscribe successfully.');
}, e => {
console.error('Fail to subscribe', e);
});