Search code examples
twittertweepy

Tweepy bot to tweet DMs


I would like to make a bot on twitter to use with my friends, it would be like a spotted. The bot would need to tweet the messages they send on his dm. On the internet I found this function, but I don't know if it's right, nor how to close this puzzle, can someone help me?

api.list_direct_messages()

Solution

  • You can get the DM messages with

    auth = tweepy.OAuthHandler('api_key', 'api_secret')
    auth.set_access_token('access_token', 'token_secret')
    
    api = tweepy.API(auth)
    messages = get_api().list_direct_messages(count=5)
    

    The code above would rin in a thread to fetch the messages every x minutes.
    Once you have the messages you can process the list, send the tweet and delete the DMs (to avoid processing it again).

    for message in messages:
      text = message.message_create["message_data"]["text"]
      api.update_status(f'Tweet DM content: {text}'
      # remove DM
      api.destroy_direct_message(message.id)