mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-06 15:57:13 +00:00
Fixed importing and added examples from twilio-node as tests.
This commit is contained in:
+224
-10
@@ -1,19 +1,233 @@
|
||||
/// <reference path="./twilio.d.ts" />
|
||||
import { twilio } from './twilio';
|
||||
|
||||
import twilio = require('twilio');
|
||||
// Examples taken from https://twilio.github.io/twilio-node/ (v2.1.0)
|
||||
|
||||
var str: string;
|
||||
|
||||
const client = new twilio.RestClient(str, str);
|
||||
const account = client.accounts(str);
|
||||
// Create a client:
|
||||
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');
|
||||
|
||||
account.messages.post({
|
||||
body: str,
|
||||
from: str,
|
||||
to: str
|
||||
//Get a list of calls made by this account
|
||||
// GET /2010-04-01/Accounts/ACCOUNT_SID/Calls
|
||||
// alias for get is "list", if you prefer
|
||||
client.calls.get(function(err, response) {
|
||||
response.calls.forEach(function(call) {
|
||||
console.log('Received call from: ' + call.from);
|
||||
console.log('Call duration (in seconds): ' + call.duration);
|
||||
});
|
||||
});
|
||||
|
||||
account.sms.messages(str).get(function(err, data) {
|
||||
// Do nothing
|
||||
//Get a list of calls made by this account, from this phone number
|
||||
// GET /2010-04-01/Accounts/ACCOUNT_SID/Calls?From=+16513334455
|
||||
client.calls.get({
|
||||
from:'+16513334455'
|
||||
}, function(err, response) {
|
||||
response.calls.forEach(function(call) {
|
||||
console.log('Received call from: ' + call.from);
|
||||
console.log('This call\'s unique ID is: ' + call.sid);
|
||||
});
|
||||
});
|
||||
|
||||
//Get data for a specific call
|
||||
// GET /2010-04-01/Accounts/ACCOUNT_SID/Calls/abc123...
|
||||
client.calls('abc123...').get(function(err, call) {
|
||||
console.log('This call\'s unique ID is: ' + call.sid);
|
||||
console.log('This call was created at: ' + call.dateCreated);
|
||||
});
|
||||
|
||||
//Get data for a specific call, for a specific account
|
||||
// GET /2010-04-01/Accounts/AC.../Calls/abc123...
|
||||
client.accounts('AC...').calls('abc123...').get(function(err, response) {
|
||||
response.calls.forEach(function(call) {
|
||||
console.log('Received call from: ' + call.from);
|
||||
console.log('This call\'s unique ID is: ' + call.sid);
|
||||
});
|
||||
});
|
||||
|
||||
// Create (send) an SMS message
|
||||
// POST /2010-04-01/Accounts/ACCOUNT_SID/SMS/Messages
|
||||
// "create" and "update" aliases are in place where appropriate on PUT and POST requests
|
||||
client.sms.messages.post({
|
||||
to:'+16515559999',
|
||||
from:'+14503334455',
|
||||
body:'word to your mother.'
|
||||
}, function(err, text) {
|
||||
console.log('You sent: '+ text.body);
|
||||
console.log('Current status of this text message is: '+ text.status);
|
||||
});
|
||||
|
||||
// Delete a TwiML application
|
||||
// DELETE /2010-04-01/Accounts/ACCOUNT_SID/Applications/APP...
|
||||
client.applications('APP...').delete(function(err, response, nodeResponse) {
|
||||
//DELETE requests do not return data - if there was no error, it worked.
|
||||
err ? console.log('There was an error') : console.log('it worked!');
|
||||
});
|
||||
|
||||
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN'),
|
||||
SOME_SUBACCOUNT_SID = 'AC...';
|
||||
|
||||
//Send a text message, associated with the given subaccount
|
||||
client.accounts(SOME_SUBACCOUNT_SID).sms.messages.create({
|
||||
to:'+16512223333',
|
||||
from:'+14505556677',
|
||||
body:'word to your subaccount mother.'
|
||||
}, function(err, text) {
|
||||
console.log('You sent: '+ text.body);
|
||||
console.log('Current status of this text message is: '+ text.status);
|
||||
});
|
||||
|
||||
//This REST call using the master/default account for the client...
|
||||
client.makeCall({
|
||||
to:'+16512223333',
|
||||
from:'+14505556677',
|
||||
url:'http://example.com/someTwiml.php'
|
||||
}, function(err, call) {
|
||||
console.log('This call\'s unique ID is: ' + call.sid);
|
||||
console.log('This call was created at: ' + call.dateCreated);
|
||||
});
|
||||
|
||||
//...is the same as...
|
||||
client.accounts(str).calls.create({
|
||||
to:'+16512223333',
|
||||
from:'+14505556677',
|
||||
url:'http://example.com/someTwiml.php'
|
||||
}, function(err, call) {
|
||||
console.log('This call\'s unique ID is: ' + call.sid);
|
||||
console.log('This call was created at: ' + call.dateCreated);
|
||||
});
|
||||
|
||||
var restClient = new twilio.RestClient('ACCOUNT_SID', 'AUTH_TOKEN');
|
||||
|
||||
// A simple example of making a phone call using promises
|
||||
var promise = restClient.makeCall({
|
||||
to:'+16515556667777', // a number to call
|
||||
from:'+16518889999', // a Twilio number you own
|
||||
url:'https://demo.twilio.com/welcome/voice' // A URL containing TwiML instructions for the call
|
||||
});
|
||||
|
||||
// You can assign functions to be called, at any time, after the request to
|
||||
// Twilio has been completed. The first function is called when the request
|
||||
// succeeds, the second if there was an error.
|
||||
promise.then(function(call) {
|
||||
console.log('Call success! Call SID: '+call.sid);
|
||||
}, function(error) {
|
||||
console.error('Call failed! Reason: '+error.message);
|
||||
});
|
||||
|
||||
// Let's look at an example where we're making multiple requests to Twilio, like
|
||||
// buying a new phone number. This is where promises can become very useful:
|
||||
|
||||
// First, search for available phone numbers
|
||||
restClient.availablePhoneNumbers('US').local.get({
|
||||
areaCode:'651'
|
||||
}).then(function(searchResults) {
|
||||
|
||||
// handle the case where there are no numbers found
|
||||
if (searchResults.availablePhoneNumbers.length < 1) {
|
||||
throw { message:'No numbers found with that area code' };
|
||||
}
|
||||
|
||||
// Okay, so there are some available numbers. Now, let's buy the first one
|
||||
// in the list. Return the promise created by the next call to Twilio:
|
||||
return restClient.incomingPhoneNumbers.create({
|
||||
phoneNumber:searchResults.availablePhoneNumbers[0].phoneNumber,
|
||||
voiceUrl:'https://demo.twilio.com/welcome/voice',
|
||||
smsUrl:'https://demo.twilio.com/welcome/sms/reply'
|
||||
});
|
||||
|
||||
}).then(function(number) {
|
||||
|
||||
// We bought the number! Everything worked!
|
||||
console.log('Your new number: '+number.phoneNumber);
|
||||
|
||||
}).fail(function(error) {
|
||||
|
||||
// This callback will be invoked on any error returned in the
|
||||
// process.
|
||||
console.log('Number purchase failed! Reason: '+error.message);
|
||||
|
||||
}).fin(function() {
|
||||
|
||||
// You can use this optional callback like a "finally" block
|
||||
// It will always execute last. Perform any cleanup necessary here.
|
||||
|
||||
});
|
||||
|
||||
client.request({
|
||||
url:'/Accounts',
|
||||
method:'GET'
|
||||
}, function (error, responseData) {
|
||||
//work with response data
|
||||
});
|
||||
|
||||
/// TwiML
|
||||
var resp = new twilio.TwimlResponse();
|
||||
|
||||
resp.say('Welcome to Twilio!');
|
||||
resp.say('Please let us know if we can help during your development.', {
|
||||
voice:'woman',
|
||||
language:'en-gb'
|
||||
});
|
||||
|
||||
console.log(resp.toString());
|
||||
|
||||
resp.say('Welcome to Twilio!')
|
||||
.pause({ length:3 })
|
||||
.say('Please let us know if we can help during your development.', {
|
||||
voice:'woman',
|
||||
language:'en-gb'
|
||||
})
|
||||
.play('http://www.example.com/some_sound.mp3');
|
||||
|
||||
resp.say('Welcome to Acme Customer Service!')
|
||||
.gather({
|
||||
action:'http://www.example.com/callFinished.php',
|
||||
finishOnKey:'*'
|
||||
}, function() {
|
||||
this.say('Press 1 for customer service')
|
||||
.say('Press 2 for British customer service', { language:'en-gb' });
|
||||
});
|
||||
|
||||
resp.say('Welcome to Acme Customer Service!')
|
||||
.gather({
|
||||
action:'http://www.example.com/callFinished.php',
|
||||
finishOnKey:'*'
|
||||
}, function(node) { //note the use of the "node" variable in the anonymous function
|
||||
|
||||
//Now you can use this reference as well, if using "this" wrankles you
|
||||
node.say('Press 1 for customer service')
|
||||
.say('Press 2 for British customer service', { language:'en-gb' });
|
||||
|
||||
});
|
||||
|
||||
resp.say('Your conference call is starting.',
|
||||
{
|
||||
voice:'woman',
|
||||
language:'en-gb'
|
||||
})
|
||||
.dial({
|
||||
action:'http://example.com/something.php'
|
||||
}, function(node) {
|
||||
node.conference('waitingRoom', {
|
||||
beep:'false'
|
||||
});
|
||||
});
|
||||
|
||||
/// Capabilities
|
||||
var capability = new twilio.Capability(str, str);
|
||||
capability.allowClientIncoming('jenny');
|
||||
var token = capability.generate();
|
||||
|
||||
capability.allowClientOutgoing('AP123');
|
||||
var token = capability.generate();
|
||||
|
||||
capability.allowClientOutgoing('AP123');
|
||||
var token = capability.generate(120);
|
||||
|
||||
/// Utilities
|
||||
twilio.validateRequest(token, str, 'http://example.herokuapp.com', { query: 'val' });
|
||||
twilio.validateExpressRequest({}, 'YOUR_TWILIO_AUTH_TOKEN');
|
||||
twilio.validateExpressRequest({}, 'YOUR_TWILIO_AUTH_TOKEN', {});
|
||||
twilio.webhook({ validate: false });
|
||||
|
||||
|
||||
|
||||
Vendored
+39
-4
@@ -9,7 +9,11 @@
|
||||
|
||||
import * as Http from 'http';
|
||||
|
||||
declare module twilio {
|
||||
export interface twilio {
|
||||
(sid?: string, tkn?: string, options?: twilio.ClientOptions): twilio.RestClient;
|
||||
}
|
||||
|
||||
export module twilio {
|
||||
|
||||
// Composite Classes:
|
||||
//==============================
|
||||
@@ -236,8 +240,8 @@ declare module twilio {
|
||||
getMessage(messageSid: string, callback?: RequestCallback): Q.Promise<any>;
|
||||
|
||||
// Calls shorthand
|
||||
makeCall(): RestMethod;
|
||||
listCalls(): RestMethod;
|
||||
makeCall: RestMethod;
|
||||
listCalls: RestMethod;
|
||||
getCall(callSid: string, callback?: RequestCallback): Q.Promise<any>;
|
||||
|
||||
// Overrides Client.request(...)
|
||||
@@ -338,6 +342,10 @@ declare module twilio {
|
||||
legalNodes: Array<string>;
|
||||
}
|
||||
|
||||
export interface TwimlMethod { (arg1: any | string | TwimlCallback, arg2?: any | string | TwimlCallback): Node }
|
||||
|
||||
export interface TwimlCallback { (node?: Node): void; }
|
||||
|
||||
export class Node implements NodeOptions {
|
||||
name: string;
|
||||
attributes: any;
|
||||
@@ -347,10 +355,37 @@ declare module twilio {
|
||||
|
||||
constructor(config?: NodeOptions);
|
||||
|
||||
// TwiML Verbs/Nouns:
|
||||
gather: TwimlMethod;
|
||||
say: TwimlMethod;
|
||||
play: TwimlMethod;
|
||||
pause: TwimlMethod;
|
||||
|
||||
dial: TwimlMethod;
|
||||
number: TwimlMethod;
|
||||
client: TwimlMethod;
|
||||
conference: TwimlMethod;
|
||||
queue: TwimlMethod;
|
||||
sip: TwimlMethod;
|
||||
|
||||
message: TwimlMethod;
|
||||
media: TwimlMethod;
|
||||
body: TwimlMethod;
|
||||
|
||||
enqueue: TwimlMethod;
|
||||
task: TwimlMethod;
|
||||
|
||||
record: TwimlMethod;
|
||||
sms: TwimlMethod;
|
||||
hangup: TwimlMethod;
|
||||
redirect: TwimlMethod;
|
||||
reject: TwimlMethod;
|
||||
leave: TwimlMethod;
|
||||
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
function TwimlResponse(): Node;
|
||||
export class TwimlResponse extends Node {}
|
||||
|
||||
/// webhook.js
|
||||
export interface webhookOptions {
|
||||
|
||||
Reference in New Issue
Block a user