How to Automatize your Twitter News Account with OpenAI ChatGPT and NewsAPI in Python

Large language models can help curate news and draft social posts, but publishing should remain an explicit, reviewable action. This updated tutorial builds a deterministic AI news-bot pipeline that runs without credentials, keeps network calls behind an opt-in flag, and never publishes in its default mode.
Architecture of the news bot
The pipeline has four stages:
- load articles from a local fixture or NewsAPI,
- identify stories that match the configured topics,
- remove near-duplicate titles,
- create a review queue of posts below the platform limit.
The live adapters read credentials from environment variables. No API keys are stored in the notebook.
Fetch news safely
def fetch_news(number=10, live=RUN_LIVE_APIS):
if not live:
return pd.DataFrame(NEWS_FIXTURE).head(number).copy()
response = requests.get(
"https://newsapi.org/v2/top-headlines",
params={"country": "us", "category": "technology", "apiKey": api_key},
timeout=20,
)
response.raise_for_status()
Select relevant and novel stories
The offline baseline uses transparent keyword matching and Jaccard overlap between normalized title words. It is intentionally simple so each filtering decision can be tested.
relevant_news, review_queue = build_review_queue(news)
assert review_queue["characters"].le(280).all()
The verified fixture starts with five articles, retains three topic-relevant articles, and places two novel stories in the review queue.

Optional OpenAI and X adapters
Live text generation uses OpenAI().responses.create(...). Live publishing uses tweepy.Client.create_tweet(...) only when RUN_LIVE_APIS=True and all required X credentials are present.
Keep a human approval step, respect source licenses and platform automation rules, disclose generated summaries where appropriate, and retain a durable publication log to prevent repeats.




1 Commentarchived from the original site