AI · Tip
Building a Style-Mimicking News Bot with Grok API
Recipe for scraping news URLs and rewriting them in your voice using Grok
This tutorial shows how to build an automated news-rewriting bot using Python and xAI's Grok API via its OpenAI-compatible endpoint. It scrapes article text from a provided URL and prompts the model to summarize it using few-shot style examples from your own writing. It serves as a practical, lightweight template for automating newsletter drafting or social media commentary.
How it works
The xAI API is OpenAI-compatible: use the openai library with base_url https://api.x.ai/v1 and model grok-beta (XAI_API_KEY from environment). Setup: create an account at x.ai, generate an API key, get $25 free credits. Build: 1) fetch_news_content(url) — requests.get with a browser User-Agent header, then BeautifulSoup to clean the article text. 2) Define my_writing_style by pasting sample posts (e.g. an emoji-headed rundown: 'The Rundown: ... The details: ...'). 3) Set a system_message and build prompt = f"Write a post about this news: {news_content}". 4) Call generate_response(client, system_message, prompt) and print the completion.choices[0].message.content. Result shown: entering a URL produces a formatted post ('Coffee Brand Brews Up AI-Made Video Magic') in the author's rundown style with linked tool suggestions. Notebook runs in Google Colab (xAI.ipynb).
What it's for
- Turn news articles into drafts for a daily tech or industry newsletter
- Repurpose online articles into structured social media updates in your own voice
- Test and integrate the xAI Grok API into Python automation workflows
Key points
- Uses the standard OpenAI Python SDK configured with xAI's base URL
- Scrapes and cleans news article content via Requests and BeautifulSoup
- Conditioned on sample posts to match specific formatting and tone
- Generates structured rundown summaries from raw web URLs
- Runs self-contained in a Google Colab notebook
Pricing
Free tutorial; xAI API requires credits ($25 free credit upon sign-up).
Details
- Type
- Tip
- Category
- AI
- Topic
- AI Workflows
- Open source
- No
- Platforms
- API, Web
- For
- Developers, newsletter creators, and social media managers experimenting with the xAI API
- Alternative to
- OpenAI API workflows, Anthropic Claude API workflows
- Tags
- grok, xai, python, scraping, newsletter, prompt engineering
Why it was saved
- @dr_cintas: Shows how to clone your own writing style and turn any article into a newsletter-style post using the Grok API's OpenAI-compatible endpoint.
Related
Workflow for generating and animating Airbnb-style isometric 3D icons using AI
Two-stage AI workflow for auditing and fixing UI animations across codebases
A fast iOS development workflow combining Perplexity, Cursor, and Xcode
Four-step AI agent pipeline to extract audience insights from YouTube comments
Autonomous harness engineering framework that optimizes AI agent setups overnight
Open-source Python library enabling AI agents to control web browsers.
Updated