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

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:

  1. load articles from a local fixture or NewsAPI,
  2. identify stories that match the configured topics,
  3. remove near-duplicate titles,
  4. 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.

Deterministic news-bot selection funnel

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.

Florian Follonier

Florian Follonier · Cloud Solution Architect at Microsoft

Florian Follonier (PhD) is a Cloud Solution Architect at Microsoft based in Zurich and the author of relataly.com, writing hands-on tutorials on machine learning, Python, RAG, and AI agents.

1 Commentarchived from the original site

  • fatih
    Hello. I am writing from Turkey. I read your article, but I don't have clear information on these issues yet. I also have an idea, but since I don't know how to do it, I request your help for a fee.