如何识别推文是回复还是新提及?Twitter Bot开发求助
解决Twitter Bot判断提及是否为Bot原推文回复的问题
问题说明
开发一款Twitter Bot,目标是回复所有提及它的用户,但需要添加判断逻辑:仅当提及是针对Bot原推文的回复时才回复。目前使用Tweepy实现,但尝试通过in_reply_to_status_id判断时无效。
原实现代码
import tweepy import time from datetime import datetime # Connecting to Twitter API client = tweepy.Client(bearer_token, api_key, api_secret, access_token, access_token_secret) auth = tweepy.OAuth1UserHandler(api_key, api_secret, access_token, access_token_secret) api = tweepy.API(auth) # Message to reply with if someone mentions the bot message = "Hi, Thank you so much for reaching TalkyTweety Out :) Please reply to continue this conversation. " # Bot's unique ID client_id = client.get_me().data.id # This is so the bot only looks for the most recent tweets. start_id = 1 initialisation_resp = client.get_users_mentions(client_id) if initialisation_resp.data != None: start_id = initialisation_resp.data[0].id # Looking for mentions tweets in an endless loop while True: response = client.get_users_mentions(client_id, since_id=start_id) # Reply Code if response.data != None: for tweet in response.data: try: print(tweet.text) client.create_tweet(in_reply_to_tweet_id=tweet.id, text=message) start_id = tweet.id except Exception as error: print(error) # Delay (so the bot doesn't search for new tweets a bunch of times each second) time.sleep(5)
解决方案
问题出在Tweepy v2 API的get_users_mentions方法默认不会返回in_reply_to_status_id和in_reply_to_user_id字段,需要显式指定tweet_fields参数获取这些信息。判断逻辑可通过以下步骤实现:
- 在调用
get_users_mentions时,添加tweet_fields参数,请求回复相关字段 - 检查提及推文的
in_reply_to_user_id是否等于Bot的ID(说明该提及是直接回复Bot的推文) - 仅符合条件的提及才触发回复
修改后的代码
import tweepy import time from datetime import datetime # Connecting to Twitter API client = tweepy.Client(bearer_token, api_key, api_secret, access_token, access_token_secret) auth = tweepy.OAuth1UserHandler(api_key, api_secret, access_token, access_token_secret) api = tweepy.API(auth) # Message to reply with if someone mentions the bot message = "Hi, Thank you so much for reaching TalkyTweety Out :) Please reply to continue this conversation. " # Bot's unique ID client_id = client.get_me().data.id # This is so the bot only looks for the most recent tweets. start_id = 1 initialisation_resp = client.get_users_mentions(client_id, tweet_fields=["in_reply_to_status_id", "in_reply_to_user_id"]) if initialisation_resp.data != None: start_id = initialisation_resp.data[0].id # Looking for mentions tweets in an endless loop while True: # 请求包含回复相关字段的提及数据 response = client.get_users_mentions(client_id, since_id=start_id, tweet_fields=["in_reply_to_status_id", "in_reply_to_user_id"]) # Reply Code if response.data != None: for tweet in response.data: try: print(tweet.text) # 判断该提及是否是回复Bot的推文 if tweet.in_reply_to_user_id == client_id: client.create_tweet(in_reply_to_tweet_id=tweet.id, text=message) print(f"已回复推文ID: {tweet.id}") else: print(f"推文ID {tweet.id} 不是回复Bot的推文,跳过") start_id = tweet.id except Exception as error: print(f"错误: {error}") # Delay (so the bot doesn't search for new tweets a bunch of times each second) time.sleep(5)
补充说明
- 如果需要更严格的判断(比如确认被回复的推文确实是Bot发布的),可以通过
client.get_tweet(tweet.in_reply_to_status_id)获取被回复的推文,检查其作者ID是否等于Bot的ID,但这会增加API调用次数,需要注意限流。 - 确保你的Twitter开发者账号已开通v2 API的相关权限,避免因权限不足导致字段无法获取。
内容的提问来源于stack exchange,提问作者Ankit Sharma
相关产品推荐
相关产品推荐

