Skip to content

Instantly share code, notes, and snippets.

Avatar

Kevin Zeng zengfenfei

  • Xiamen
View GitHub Profile
View ts-sdk-update-extension.ts
// 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.");
});
View ts-sdk-extension-list.ts
// 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);
});
View ts-sdk-get-extension.ts
// 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);
});
View ts-sdk-send-fax.ts
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
]);
View ts-sdk-send-sms.ts
// 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) {
View ts-sdk-recent-calls.ts
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);
});
View ts-sdk-active-list.ts
// 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);
});
View ts-sdk-telephony-status.ts
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);
View ts-sdk-ringout.ts
// 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);
View ts-sdk-subscribe-by-events.ts
subscription.subscribe(['/account/~/extension/~/presence?detailedTelephonyState=true']).then(subscription => {
console.log('Subscribe successfully.');
}, e => {
console.error('Fail to subscribe', e);
});