twilio / twilio-python

A Python module for communicating with the Twilio API and generating TwiML.
MIT License
1.83k stars 700 forks source link

Can't get past 24h calls from API #784

Closed Andreluizfc closed 4 months ago

Andreluizfc commented 4 months ago

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:

tiwarishubham635 commented 4 months ago

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

tiwarishubham635 commented 4 months ago

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

Andreluizfc commented 4 months ago

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

Andreluizfc commented 4 months ago

@tiwarishubham635 Clarified the right usage.