Saroj Satapathy
Saroj Satapathy

Reputation: 79

how to read twitter list of dm from a specific message id using tweepy 3.10.0

I am trying to fetch the twitter dms since a specific message id. But it returns all the dms. can anyone help to get this done. I was trying with the following code snippet

    since_id = '134902xxxxxxxxx'
    while True:

        #last_dms = tweepy.Cursor(api.list_direct_messages, max_id=since_id).items()

        last_dms = api.list_direct_messages(10, max_id = since_id)
        for messages in last_dms:
            print(messages._json)

            #since_id = last_dms[0]["id"]
        print('since_id %d:', since_id)
        time.sleep(30)

with the above code i am able to get all dms from the userid. need hel to get the dms since a specific message id.

Upvotes: 0

Views: 207

Answers (1)

Beppe C
Beppe C

Reputation: 13973

The list_direct_messages does not support the max_id, see Twitter API reference which is the REST endpoint invoked by Tweepy.

You have 2 options:

  • delete the DM after it is processed (ie already replied), hence at the next call it won't be returned by Twitter
  • you need to implement the max_id concept yourself: at every iteration keep the DM id (after you reply) and ignore DMs which have a smaller ID. The inconvenience here is that you need to persist the ID (in a database for example) to ensure it is still available after a restart/redeployment

Upvotes: 1

Related Questions