能否创建Twitter Bot自动接受指定列表用户的受限账号关注请求?
Absolutely, you can build a bot to automate this exact workflow for your restricted Twitter (X) account. Let’s break down how to make this happen, step by step:
可行性确认
First off, this is totally doable with X's official API. Even for restricted accounts, as long as you have valid developer access and the right API permissions, you can read pending follow requests and accept them programmatically. Just note that some restricted account states might limit certain API actions, so it’s worth testing basic API calls first to confirm access.
核心工作流程
The bot will follow this simple loop every time it runs:
- Fetch the latest list of users you’ve approved for access
- Pull all pending follow requests sent to your restricted account
- Cross-reference the two lists to find approved users who’ve sent a request
- Automatically accept those matching follow requests
具体实现步骤
1. Set up X Developer Access
- Head to the X Developer Portal and create a new project linked to your restricted account.
- Request the necessary API permissions: you’ll need Read access to fetch follow requests and Write access to accept them.
- Generate your API credentials (API Key, API Secret, Access Token, Access Token Secret) — keep these secure, never hardcode them in your script.
2. Choose Your Development Tool
Python is a great choice for this thanks to libraries like tweepy that simplify X API interactions. Alternatively, you can use Node.js with the twit library if that’s more your speed.
3. Store & Update Your Approved User List
Keep your approved users in an easy-to-update format:
- A simple text file (one username per line)
- A CSV file
- A cloud-based sheet like Google Sheets (for easier remote updates)
The bot will pull this list each time it runs, so any changes you make will take effect on the next execution.
4. Schedule the Bot to Run Automatically
You don’t want to run this script manually every time. Use:
- Cron jobs (Linux/macOS) to schedule hourly/daily runs
- Task Scheduler (Windows) for the same purpose
- Cloud functions (AWS Lambda, Google Cloud Functions) for serverless, automated execution
关键注意事项
- Respect API Rate Limits: X’s API has strict rate limits (e.g., the
get_users_follow_requestsendpoint allows 15 calls every 15 minutes). Don’t overload the API, or your access might be restricted further. - Log Everything: Add logging to your script to track which requests were accepted, when, and any errors that occur. This makes troubleshooting way easier.
- Test First: Before letting the bot run automatically, test it with a small approved list and a test follow request to make sure it works as expected.
- Secure Credentials: Store your API keys in environment variables or a secure secrets manager, not in plain text.
示例Python代码
Here’s a basic implementation using tweepy to get you started:
import tweepy import os from dotenv import load_dotenv # Load environment variables (install python-dotenv first with pip) load_dotenv() # Initialize X API client client = tweepy.Client( consumer_key=os.getenv("X_API_KEY"), consumer_secret=os.getenv("X_API_SECRET"), access_token=os.getenv("X_ACCESS_TOKEN"), access_token_secret=os.getenv("X_ACCESS_TOKEN_SECRET") ) def load_approved_users(): # Load approved usernames from a text file (one per line) with open("approved_users.txt", "r") as file: return [username.strip().lower() for username in file if username.strip()] def process_pending_requests(): approved_users = load_approved_users() try: # Fetch all pending follow requests pending_requests = client.get_users_follow_requests() for user in pending_requests.data: if user.username.lower() in approved_users: # Accept the follow request client.accept_follow_request(user_id=user.id) print(f"Successfully accepted follow request from @{user.username}") except Exception as e: print(f"Error processing requests: {str(e)}") if __name__ == "__main__": process_pending_requests()
内容的提问来源于stack exchange,提问作者4pa17cs011 ARKAAN SAYED

