Skip to content

Instantly share code, notes, and snippets.

View zengfenfei's full-sized avatar

Kevin Zeng zengfenfei

  • Xiamen
View GitHub Profile
@zengfenfei
zengfenfei / openApp.js
Last active August 4, 2021 15:18
Open native app from webpage, mobile safari "invalid address" warnings be suppressed when the app is not installed.
/*
Open native app through iframe so that mobile safari "invalid address" warnings be suppressed when the app is not installed.
* `window.location=url` will not open native app in the iframe of android chrome
* Android will go to the next page even if the url scheme is not supported
*/
function openApp (url) {
if (window.chrome && navigator.userAgent.search(/\bChrome\b/)!=-1) {
window.location = url;
} else {
subscription.subscribe(['/account/~/extension/~/presence?detailedTelephonyState=true']).then(subscription => {
console.log('Subscribe successfully.');
}, e => {
console.error('Fail to subscribe', e);
});
// 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);
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/~/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 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/~/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) {
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/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);
});
// 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);
});