0

I'm trying to pull a list of all phone numbers used by my subaccounts but I'm only able to get it to pull one messaging service at a time.

Here is my code:

services = client.messaging.services.list(limit=2000)

messageid= []

for record in services:
    messageid.append(record.sid)

ddphones = []

phones = client.messaging.services(record.sid).phone_numbers.list(limit=1000)

for record in phones:
    ddphones.append(record.service_sid)
    ddphones.append(record.phone_number)

df = pd.DataFrame(ddphones)

ddd = pd.DataFrame(df.values.reshape(-1, 2), columns = ['MID', 'Phone'])
AHS
  • 1
  • 1
  • When you say you are having trouble, what do you mean? What is not working? What errors are you getting? What do you expect to happen with this code? – philnash Jun 02 '21 at 05:39
  • Sorry, I was bleary eyed when I wrote this. I have it working with one MessageID, but I can't get it to loop through all the messageIDs. Also, related, the response only shows Phone ID and not actual phone number. – AHS Jun 02 '21 at 14:28
  • I got it display phone numbers, but i'm still only seeing one message service at a time. – AHS Jun 02 '21 at 19:00

1 Answers1

0

Twilio developer evangelist here.

I think your code is, for the most part, correct but your indentation might be causing you issues. Your second loop (for record in phones:) should be within the first loop (for record in services:).

Try this instead:

ddphones = []
services = client.messaging.services.list(limit=2000)
for record in services:
    phones = client.messaging.services(record.sid).phone_numbers.list(limit=1000)
    for phone in phones:
        ddphones.append(phone.service_sid)
        ddphones.append(phone.phone_number)

df = pd.DataFrame(ddphones)
ddd = pd.DataFrame(df.values.reshape(-1, 2), columns = ['MID', 'Phone'])
philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thank you so much!! That worked! Is there any way to cycle through the Account_SIDs and Tokens? I have 12 subaccounts right now, so it's manageable, but I fear it will become less so over time. – AHS Jun 03 '21 at 22:15