twilio-remote-cli
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -1,2 +0,2 @@ | ||
const { prompt } = require('enquirer'); | ||
const { prompt, Select } = require('enquirer'); | ||
const { | ||
@@ -63,22 +63,41 @@ init, | ||
const twiliocc = new TwilioCC(...config); | ||
const callSid = await twiliocc[operation['operation']].call( | ||
twiliocc, | ||
destination.number, | ||
message | ||
); | ||
let taskSid; | ||
if (operation.operation === 'call') { | ||
await twiliocc.getCallStatus(callSid); | ||
const callOperation = await prompt(questions.callQuestion); | ||
if (operation.operation === 'sms') { | ||
taskSid = await twiliocc.sms(destination.number, message); | ||
await twiliocc.getSmsStatus(taskSid); | ||
return; | ||
} | ||
if (!callOperation.action) { | ||
return; | ||
} | ||
let callStatus; | ||
callSid = await twiliocc.call(destination.number); | ||
if (callOperation.action == 'drop') { | ||
await twiliocc.terminateCall(callSid); | ||
} | ||
const promptInput = new Select({ | ||
name: 'dropCall', | ||
choices: ['Drop call'], | ||
separator: () => '', | ||
header: () => callStatus, | ||
message: state => | ||
state.submitted | ||
? 'Call ended' | ||
: `Press return to end the call once you're done.` | ||
}); | ||
return; | ||
} | ||
promptInput | ||
.once('close', () => promptInput.clear()) | ||
.once('run', () => { | ||
setInterval(async () => { | ||
const call = await twiliocc.keepCallingLoop(callSid); | ||
if (!call) { | ||
promptInput.cancel(); | ||
} | ||
callStatus = call; | ||
promptInput.render(); | ||
}, 500); | ||
}); | ||
await promptInput.run(); | ||
await twiliocc.dropCall(callSid); | ||
} catch (err) { | ||
@@ -85,0 +104,0 @@ console.log(err.message || ''); |
@@ -14,13 +14,2 @@ module.exports = { | ||
callQuestion: { | ||
type: 'select', | ||
name: 'action', | ||
message: 'Select an operation', | ||
initial: 0, | ||
choices: [ | ||
{ name: 'keep', message: 'Keep ringing', value: 'keep' }, | ||
{ name: 'drop', message: 'Drop the call', value: 'drop' } | ||
] | ||
}, | ||
destinationNumber: lastUsedNumber => ({ | ||
@@ -27,0 +16,0 @@ type: 'input', |
const twilio = require('twilio'); | ||
const axios = require('axios'); | ||
const chalk = require('chalk'); | ||
const ora = require('ora'); | ||
@@ -14,13 +15,11 @@ module.exports.TwilioCC = class TwilioCC { | ||
async sms(to, body) { | ||
try { | ||
await this.client.messages.create({ | ||
from: this.phoneNumber, | ||
to, | ||
body | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
let message; | ||
console.log('Message sent'); | ||
message = await this.client.messages.create({ | ||
from: this.phoneNumber, | ||
to, | ||
body | ||
}); | ||
return message.sid; | ||
} | ||
@@ -31,58 +30,64 @@ | ||
try { | ||
task = await this.client.calls.create({ | ||
url: 'http://demo.twilio.com/docs/voice.xml', | ||
from: this.phoneNumber, | ||
to | ||
}); | ||
task = await this.client.calls.create({ | ||
url: 'http://demo.twilio.com/docs/voice.xml', | ||
from: this.phoneNumber, | ||
to | ||
}); | ||
if (!(task.status && task.sid)) { | ||
throw 'Something went wrong. Please try again or check your Twilio credentials.'; | ||
} | ||
} catch (err) { | ||
throw err; | ||
if (!(task.status && task.sid)) { | ||
throw 'Something went wrong. Please try again or check your Twilio credentials.'; | ||
} | ||
console.log(`Calling ${to}...`); | ||
return new Promise(res => { | ||
setTimeout(() => res(task.sid), 500); | ||
}); | ||
return task.sid; | ||
} | ||
async getCallStatus(callSid) { | ||
const baseURL = `https://api.twilio.com`; | ||
const auth = Buffer.from(`${this.accountSid}:${this.authToken}`).toString( | ||
'base64' | ||
); | ||
async getSmsStatus(taskId) { | ||
const message = await this.client.messages(taskId).fetch(); | ||
const callStatusReq = axios.create({ | ||
baseURL, | ||
timeout: 1500, | ||
headers: { Authorization: `Basic ${auth}` } | ||
}); | ||
const copy = `${chalk.cyan('Sending message to:')} ${message.to}...`; | ||
const spinner = ora(copy).start(); | ||
return new Promise(res => { | ||
callStatusReq | ||
.get(`/2010-04-01/Accounts/${this.accountSid}/Calls/${callSid}.json`) | ||
.then(function(response) { | ||
if (response.status !== 200) { | ||
throw 'Server error'; | ||
const checkDelivery = async () => | ||
new Promise(async res => { | ||
const sms = await this.client.messages(taskId).fetch(); | ||
setTimeout(() => { | ||
if (sms.status === 'delivered') { | ||
spinner.clear(); | ||
return res(); | ||
} | ||
setTimeout(() => res(), 500); | ||
}) | ||
.catch(function(error) { | ||
// handle error | ||
console.log(error); | ||
return; | ||
}); | ||
checkDelivery(taskId); | ||
}, 1000); | ||
}); | ||
await checkDelivery(taskId); | ||
} | ||
async dropCall(callSid) { | ||
const callTask = await this.client.calls(callSid); | ||
return callTask.update({ | ||
status: 'completed' | ||
}); | ||
} | ||
async terminateCall(callSid) { | ||
const task = await this.client.calls(callSid); | ||
async keepCallingLoop(callSid) { | ||
const taskDetails = await this.client.calls(callSid).fetch(); | ||
const callStatus = taskDetails.status; | ||
const completedStatus = ['canceled', 'completed', 'failed']; | ||
const callMessage = `Calling ${taskDetails.to}`; | ||
return await task.update({ status: 'completed' }); | ||
if (callStatus === 'queued') { | ||
return `${callMessage}...`; | ||
} | ||
if (completedStatus.includes(callStatus)) { | ||
return false; | ||
} | ||
if (callStatus !== 'queued') { | ||
return `${callMessage}: ${callStatus}`; | ||
} | ||
} | ||
}; |
{ | ||
"name": "twilio-remote-cli", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "A remote control for your Twilio API", | ||
@@ -32,6 +32,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"axios": "^0.18.0", | ||
"enquirer": "^2.0.7", | ||
"chalk": "^2.4.2", | ||
"enquirer": "^2.3.0", | ||
"meow": "^5.0.0", | ||
"nconf": "^0.10.0", | ||
"ora": "^3.2.0", | ||
"twilio": "^3.23.2", | ||
@@ -38,0 +39,0 @@ "update-notifier": "^2.5.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
687162
395
7
+ Addedchalk@^2.4.2
+ Addedora@^3.2.0
+ Addedansi-regex@4.1.1(transitive)
+ Addedcli-cursor@2.1.0(transitive)
+ Addedcli-spinners@2.9.2(transitive)
+ Addedclone@1.0.4(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addedlog-symbols@2.2.0(transitive)
+ Addedmimic-fn@1.2.0(transitive)
+ Addedonetime@2.0.1(transitive)
+ Addedora@3.4.0(transitive)
+ Addedrestore-cursor@2.0.0(transitive)
+ Addedstrip-ansi@5.2.0(transitive)
+ Addedwcwidth@1.0.1(transitive)
- Removedaxios@^0.18.0
- Removedaxios@0.18.1(transitive)
- Removedfollow-redirects@1.5.10(transitive)
- Removedis-buffer@2.0.5(transitive)
Updatedenquirer@^2.3.0