Twilio client.messages.stream() returning infinite results?

Viewed 50

so I know my Twilio account has sent upwards of 50,000 texts. However, when I run the following:

for item in client.messages.stream():
     i+=1
     lst.append([item.body.replace('|',''),item.from_,item.date_sent])
     if i % 100 == 0:
          print(i)

It just keeps running and running. I was originally using client.messages.list, but that hit my 1 minute, then 5 minute, then 10 minute timeout in lambda, so I decided to debug locally, and run the above. I stopped it after it had gotten to 230,000, which is many multiple more messages than we've actually sent.

I don't quite know why it's doing that? The docs don't say anything about this? I can't find in the docs either a way to tell the stream to move on, if what it's doing is just returning the same page over and over.

It doesn't appear to be returning the same page, though - when I print the message body for the hundredth one it changes every so often.

https://www.twilio.com/docs/libraries/reference/twilio-python/7.8.0/docs/source/_rst/twilio.rest.api.v2010.account.message.html#twilio.rest.api.v2010.account.message.MessageList

1 Answers

client.messages.stream(): will return all the messages you have sent as well as received.

You can filter the list by the number the number a message was sent from, if you are only looking to retrieve your outbound messages.

If you are looking to limit the results you are getting, you can set a limit. If you want to speed long requests like this up, you can increase the pageSize to a 1000 (default is 50).

Related