0

I use Tweepy to reply to certain Tweets as a bot on Twitter. I would like the reply to be addressed only to the author of the Tweet and not to any other users that may be mentioned in it.

Currently I use this to reply:

reply_status = api.update_status(status = 'tweet content', in_reply_to_status_id = tweet.id, auto_populate_reply_metadata=True)

This call replies to all users mentioned in the original tweet. I've noticed there is an exclude_reply_user_ids= option in the update_status function. This however requires a list of user IDs to exclude.

  1. How do I get a list of user IDs mentioned in a Tweet?
  2. Is there a simpler way to reply only to the author?

I would like to avoid having to write the username at the beginning of the Tweet, which I would need to do if I didn't use auto_populate_reply_metadata=True (see this).

Harmon758
  • 5,084
  • 3
  • 22
  • 39
siria
  • 169
  • 3
  • 8

1 Answers1

0

How do I get a list of user ids mentioned in a tweet?

You can use the entities attribute of the Status/Tweet object, which is an Entities object with a user_mentions attribute. This will give you an array of User mention objects, and you can use the id or id_str fields of each one.

Is there a simpler way to reply only to the author?

I would like to avoid having to write the username at the beginning of the tweet, which I would need to do if I didn't use auto_populate_reply_metadata=True

Yes, by manually including the mention at the start of the Tweet.
You can do this programatically by retrieving the screen name of the author of the Tweet, e.g. tweet.user.screen_name.

Otherwise, if you're using auto_populate_reply_metadata, the only way to exclude mentions is using exclude_reply_user_ids.

Harmon758
  • 5,084
  • 3
  • 22
  • 39
  • Thanks. It appears that the ```user_mentions``` attribute only lists users mentioned explicitly within the tweet body. I am looking for a list of all users in the tweet (including the author and any previous repliers). – siria May 03 '21 at 05:19
  • It should include the users being replied to as well, and like I said, if you're using `auto_populate_reply_metadata`, the only way to exclude mentions is using `exclude_reply_user_ids`. – Harmon758 May 03 '21 at 09:16
  • `user_mentions` only includes users mentioned in the tweet body (see [here](https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities#mentions)). – siria May 04 '21 at 07:58