How to get call logs for last 10 days using Twilio API? - node.js

I am working on a Twilio project in which I need to fetch call logs for last 10 days in a single call, but I see no favourable parameters in twilio api docs (https://www.twilio.com/docs/api/voice/call#list-get) that can help me. I tried DateCreated,StartTime parameters but they work differently. Could anyone help me out here please?
Thanks in advance.

I think you are in the wrong area of the api, i think the area you are looking at is for calls currently in progress.
Try usage-records
Sometimes when I'm stuck, I run a curl example in Twilio's API explorer; This shows me 2 things:
Options used in call
What to expect as results
Good luck

Try the startTimeAfter and startTimeBefore parameters.
// Download the Node helper library from twilio.com/docs/node/install
// These identifiers are your accountSid and authToken from
// https://www.twilio.com/console
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);
const filterOpts = {
status: 'completed',
startTimeAfter: '2016-07-04',
startTimeBefore: '2016-07-06',
};
client.calls.each(filterOpts, call => console.log(call.direction));
Twilio Docs:
https://www.twilio.com/docs/api/voice/call?code-sample=code-retrieve-completed-calls-from-a-period-of-time&code-language=js&code-sdk-version=3.x

curl 'https://api.twilio.com/2010-04-01/Accounts/[ACCOUNT SID]/Calls.json?StartTime%3E=2020-06-01T0&EndTime%3C=2020-07-01' -u [ACCOUT SID]:[AuthToken]
This curl script will give you a list of calls from 2020-06-01 to 2020-07-01. Don't forget to substitute your ACCOUNT_SID and AuthToken (without the brackets)

Related

How to add a "say" message to the beginning of conference using Twilio API in Nodejs

This is my code and it is working perfectly for creating a conference by calling two numbers. However, I wanted to add a message at the beginning of the conference to say "Hello, you will be contacted shortly"
I read that I can use ngrok to create the URL with the XML response but ngrok is not working for me. because ngrok urls are only working for me when the code is still running locally. so If I stop the Twilio script that I wrote to generate this URL from running locally the ngrok URL stops working.
function CallTwilio(number1, number2) {
const Twilio = require('twilio');
var accountSid = 'XXX';
var authToken = 'YYY';
const client = Twilio (accountSid, authToken);
[number1, number2].forEach(function(number_i) {
client.calls.create({
url: 'https://handler.twilio.com/twiml/EH05a82ce144a55344f0d39ac6b20204f1',
to: number_i,
from: 'mynumber',
})
.then((call) => onprogress.stdout.write('Called'));
});
}
Twilio developer evangelist here.
I note that when you create the call you pass a url which is a TwiML Bin. My guess is that your TwiML Bin code looks a bit like:
<Response>
<Dial><Conference>Conference name</Conference></Dial>
</Response>
If you want to <Say> a message before the person you call enters the conference, then you need to adjust this to include the <Say> before the <Dial>. Like this:
<Response>
<Say>Hello, you will be connected shortly.</Say>
<Dial><Conference>Conference name</Conference></Dial>
</Response>

Implementing isRequestFromAssistant in Node.js on actions-on-google project fulfillment

I am having trouble implementing the isRequestFromAssistant method to verify requests to my fulfillment webhook. Using Node.js, I instantiate the following variables at the start of my index.js file:
const App = require('actions-on-google').ApiAiApp;
const app = new App({ request, response });
I then use "app" with the .ask and .tell and other methods throughout my functions.
The code I see in the docs for implementing isRequestFromAssistant is:
const app = new ActionsSdkApp({request, response});
app.isRequestFromAssistant('my-project-id')
.then(() => {
app.ask('Hey there, thanks for stopping by!');
})
.catch(err => {
response.status(400).send();
});
If I leave out the first line and use my existing app variable, created with the .ApiAi method instead of the .ActionsSdkApp method, it doesn't work. If I create a new variable App1 and app1 using the .ActionsSdkApp method and change the above code to be app1.isRequestFromAssistant, it also doesn't work. I have tried other variations with no luck.
When I say it doesn't work, I mean I receive a 500 Internal Server Error when I call it. I am hosting it with NGROK currently. I am still a beginner with Node.js, although I have managed to get the other 700 lines of code working just fine, learning mostly from Google searches and reading these forums.
You have a few things going on here which, individually or separately, may be causing the problem.
First - make sure you have the most recent version of the actions-on-google library. The isRequestFromAssistant() function was added in version 1.6.0, I believe.
Second - Make sure you're creating the right kind of App instance. If you're using Dialogflow (formerly API.AI), you should be creating it with something like
const App = require('actions-on-google').DialogflowApp;
const app = new App( {request, response} );
or
const { DialogflowApp } = require('actions-on-google');
const app = new DialogflowApp( {request, response} );
(They both do the same thing, but you'll see both forms in documentation.) You should switch to DialogflowApp from ApiAiApp (which your example uses) to reflect the new name, but the old form has been retained.
If you're using the Actions SDK directly (not using Dialogflow / API.AI), then you should be using the ActionsSdkApp object, something like
const { ActionsSdkApp } = require('actions-on-google');
const app = new ActionsSdkApp({request: request, response: response});
(Again, you'll see variants on this, but they're all fundamentally the same.)
Third - Make sure you're using the right function that matches the object you're using. The isRequestFromAssistant() function is only if you are using the Actions SDK.
If you are using Dialogflow, the corresponding function is isRequestFromDialogflow(). The parameters are different, however, since it requires you to set confirmation information as part of your Dialogflow configuration.
Finally - If you're getting a 500 error, then check your logs (or the output from stderr) for the node.js server that is running. Typically there will be an error message there that points you in the right direction. If not - posting that error message as part of your StackOverflow question is always helpful.
Set the secure (randomly generated) auth header & key values in the dialogflow Fulfillment page, then in nodejs:
if (app.isRequestFromDialogflow("replace_with_key", "replace_with_value")) {
console.log("Request came from dialogflow!");
// rest of bot
} else {
console.log("Request did not come from dialogflow!");
response.status(400).send();
}
Also see: https://developers.google.com/actions/reference/nodejs/DialogflowApp#isRequestFromDialogflow

'context.getTwilioClient(...).document is not a function?

I'm working with Twilio Functions and I'm trying to use Sync Documents but I keep getting this error: context.getTwilioClient(...).document is not a function
exports.handler = function(context, event, callback) {
context.getTwilioClient().document('data').then(function(doc) {..});
};
Twilio developer evangelist here.
As Andy said, the client you retrieve from context.getTwilioClient() is a generic Twilio REST API client that can access all the resources.
To get your document you need to traverse the objects in the client and get a handle on your service. You need the Sync Service SID, ideally in your environment variables, and the code would look like this:
const client = context.getTwilioClient();
const service = client.sync.services(process.env.SYNC_SERVICE_SID);
service.document('data').fetch().then(function(doc) {
// do something with the document.
});
Let me know if that helps at all.

Trouble with Twilio / TwiML statusCallback using Node.js

When a user texts my number, I would like to respond with a message using TwiML. I'd also like to know whether my response message sends or fails. I'm creating my TwiML response using the Twilio library for Node.js.
Here is my code:
const response = new twilio.TwimlResponse();
response.message(Response, {
statusCallback: '/sms/status/'
});
Here is the code for the route. Please note that the router is mounted upon the "/sms/" router:
router.post('/status/', acceptRequestsOnlyFromTwilio, (req, res) => {
const Event = require('./../models/schemas/event');
const event = new Event({
description:
`MessageSid = ${req.body.MessageSid}
MessageStatus = ${req.body.MessageStatus}`
});
event.save();
console.log(
`MessageSid = ${req.body.MessageSid}
MessageStatus = ${req.body.MessageStatus}`
);
res.sendStatus(200);
});
When I check my logs using the Heroku CLI, I don't log nor do I see a record when I check MongoDB.
Anyone have any ideas or tips on how I can go about debugging this or if I'm implementing this incorrectly?
Thanks for you time!
Twilio developer evangelist here.
Firstly, have you set your Messaging request URL for the Twilio phone number you are using? Is it pointing at the correct route for your Message TwiML response?
Also, your first code block should also be within a router.post endpoint for your server. Do you correctly return the TwiML response as the server response?
As a tip, it might be better to develop this application locally and test your webhook endpoints using a tool like ngrok. I love using ngrok to test webhooks.

how to connect two anonymous callers together using twilio

I am currently doing using node js twilio module for a functionality in a project that i am working on for a client. Basically the server is going to initiate a call using twilio api to call a certain person A the connect him to another person B. I am new to twilio so i am still a noob, but this is the code i have written so far. Please i need u guys input on how to achieve this. cheers
var client = require('twilio')(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
exports.makeCall = function(callDetails, cb) {
client.makeCall({
to:'+16515556677', // Any number Twilio can call
from: TWILIO_CALLER_ID,
url: 'https://demo.twilio.com/welcome/voice' // A URL that produces an XML document (TwiML) which contains instructions for the call
}, function(err, responseData) {
//executed when the call has been initiated.
console.log(responseData.from); // outputs "+14506667788"
var resp = new client.TwimlResponse();
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' });
});
});
};
Twilio developer evangelist here.
You're part way there with this, but you don't want to be creating a TwiML response in the callback to your call to client.makeCall. The responseData there is a representation of the call in Twilio's system.
What you need to do is provide a URL to the makeCall function that hosts some TwiML that connects the call to another phone number. Currently you have the demo URL in place, so you'll need to point that to a URL that you own.
There's a very good in depth tutorial on how to accomplish all of this on the Twilio site which you might find helps out. You can find the tutorial here, give it a go and let me know if that helps.

Resources