twilio / twilio-python

A Python module for communicating with the Twilio API and generating TwiML.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't get past 24h calls from API

Andreluizfc opened this issue · comments

Issue Summary

I can't find a way to get the last calls from past X hours from API.
Can I query the calls database based on hours?

The code snippet bellow is something that should be implemented.

Code Snippet

import os
from twilio.rest import Client
from datetime import datetime, timedelta

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

# Calculate start and end date for the last 24 hours
end_time = datetime.utcnow()
start_time = end_time - timedelta(hours=24)

# Format dates for Twilio API query
start_time_formatted = start_time.strftime('%Y-%m-%dT%H:%M:%SZ')
end_time_formatted = end_time.strftime('%Y-%m-%dT%H:%M:%SZ')

calls = client.calls.list(start_time=start_time_formatted, end_time=end_time_formatted)

Exception/Log

# paste exception/log here

Technical details:

  • twilio-python version: 9.0.4
  • python version: 3.9

I think you can use start_time_after parameter. Pass start_time_formatted as its value and that should do the work. See here.

No, you can't query the database on hours. We currently support start_time, end_time and their inequality fields.

Great. Thanks for the help. I end up going to this list() method.

@tiwarishubham635 Clarified the right usage.