Fetches tech/AI news from Reddit and X, generates one professional LinkedIn post and one concise X post using OpenAI (gpt-4o), posts them, and logs status to MongoDB. Designed to run as an AWS Lambda on a schedule via EventBridge.
- Pulls items from:
- Reddit (multiple tech/programming subreddits, keyword filtered)
- X Recent Search (robust keyword trimming and fallback; rate-limit aware)
- Uses OpenAI to select the single most valuable item and generate:
- LinkedIn post (professional, with Source: link and spaced hashtags)
- X post (<= 280 chars + hashtags)
- Posts to LinkedIn (UGC API) and X (OAuth1 or OAuth2), once per trigger
- Stores post records and pending/retry state in MongoDB
main.py: single-run orchestrator (used by Lambda)lambda_handler.py: Lambda entrypoint callingrun_once()app/config.py: reads env and keywordsfetch_reddit.py,fetch_x.py: source fetchersgenerate.py: OpenAI gpt-4o prompt/generation (one item)post_linkedin.py,post_x.py: posting clientsdb_mongo.py: MongoDB helpers (pending/posted records)
serverless.yml: Serverless Framework config (Lambda + EventBridge schedules)
- Python 3.9+ locally (Lambda currently set to python3.9 in
serverless.yml) - AWS account + credentials configured for Serverless Framework
- OpenAI API key
- LinkedIn access token with permissions for UGC posts
- X API credentials:
- Either OAuth1 keys with write permissions (read+write)
- Or an OAuth2 user access token with
tweet.writescope
- MongoDB (Atlas or DocumentDB or local)
Required (match serverless.yml):
OPENAI_API_KEYLINKEDIN_ACCESS_TOKENREDDIT_CLIENT_ID,REDDIT_CLIENT_SECRETX_BEARER_TOKEN(for search)- One of (for posting to X):
- OAuth1:
X_API_KEY,X_API_SECRET,X_ACCESS_TOKEN,X_ACCESS_TOKEN_SECRET - OAuth2:
X_CLIENT_ID,X_CLIENT_SECRET(to mint tokens externally), andX_OAUTH2_ACCESS_TOKENfor posting
- OAuth1:
- MongoDB:
MONGO_URI,MONGO_DB(defaultautoposter),MONGO_COLLECTION(defaultposts)
Optional/unused by core flow (may be present in serverless.yml): LINKEDIN_ID_TOKEN, LINKEDIN_CLIENTID, LINKEDIN_SECRETID, Discord vars.
Install dependencies and run one cycle locally:
pip install -r requirements.txt
python lambda_handler.pyThis prints {"status": "ok"} and executes a full run.
- Ensure Serverless is installed and AWS credentials are set.
- Place your environment variables in a local
.env(the config usesuseDotenv: true). - Install plugins:
sls plugin install -n serverless-python-requirements
sls plugin install -n serverless-dotenv-plugin- Deploy:
sls deploy- Invoke once to test:
sls invoke -f autoposterserverless.ymldefines two EventBridge schedules (UTC). Adjustrate: cron(...)or remove theeventsblock if you prefer manual invocation.- Region defaults to
ap-south-1; changeprovider.regionas needed.
- service/frameworkVersion: project name and Serverless v3.
- useDotenv: true: loads
.envintoprovider.environmentfor deploys. - provider:
- runtime: Python runtime for Lambda (python3.9 set here).
- region/stage: AWS region and stage; change region to where you deploy.
- memorySize/timeout: Lambda resources (adjust if needed).
- environment: all env vars your function reads (OpenAI, LinkedIn, X, Mongo, etc.).
- plugins:
serverless-python-requirements: builds Python deps into the bundle (uses Docker ifdockerizePip: true).serverless-dotenv-plugin: injects.envinto env variables at deploy time.
- custom.pythonRequirements:
- dockerizePip: set
truefor native wheels compatibility;falseuses local pip. - slim/strip: reduce package size by removing extraneous files.
- dockerizePip: set
- package.patterns: include everything except caches/dist-info/pyc and old sqlite file.
- functions.autoposter:
- handler: entry is
lambda_handler.handler. - events.schedule: two EventBridge cron triggers in UTC; set
enabled: true/falseor edit the cron to change run times.
- handler: entry is
- X rate limits: The fetcher trims keywords and retries with a minimal set. If you still hit limits or see 403, reduce keyword breadth or ensure your app has appropriate access.
- LinkedIn posts with URLs are sent as ARTICLE shares with
originalUrland DataMap-wrappedtitle. - MongoDB is used to avoid reposts and to retry pending items automatically on next run.
- Keywords/subreddits are broad by default; override via
KEYWORDSenv if desired.
- Do not commit secrets. Use
.envlocally and set environment variables in Lambda/Serverless for production. - Consider AWS Secrets Manager/SSM for managing sensitive values.