Skip to content
Daniel Kliewer

Complete Guide: Building AI Agent-Based Cross-Platform Content Generator for Automated Social Media Distribution

By
4 min read
Complete Guide: Building AI Agent-Based Cross-Platform Content Generator for Automated Social Media Distribution

Image

Guide to Building an AI Agent-Based Cross-Platform Content Generator and Distributor

This guide will walk you through building an application that automates content creation and posting across multiple social media platforms by generating unique, platform-specific content based on a single post. We'll focus on terminal commands, instructions, and code to help you implement this system step by step.


Prerequisites

  • Programming Knowledge: Intermediate proficiency in Python.
  • Python Environment: Python 3.8 or later installed on your machine.
  • API Access: Developer accounts and API credentials for the social media platforms you plan to use.
  • OpenAI API Key: Access to OpenAI's API for GPT-4 and DALL·E (or equivalents).
  • Virtual Environment Tool: venv or conda.
  • Additional Tools: git, ffmpeg (for video processing).

Step 1: Set Up the Project Environment

1.1 Create a Project Directory

Open your terminal and create a new directory for your project:

mkdir CrossPlatformContentGenerator
cd CrossPlatformContentGenerator

1.2 Initialize a Git Repository (Optional)

git init

1.3 Create a Virtual Environment

python3 -m venv venv

Activate the virtual environment:

  • On Linux/macOS:

    source venv/bin/activate
    
  • On Windows:

    venv\Scripts\activate
    

1.4 Upgrade pip and Install Required Python Packages

pip install --upgrade pip
pip install openai praw python-dotenv requests requests_oauthlib langchain

Install additional packages for specific platforms:

pip install facebook-sdk google-api-python-client tweepy moviepy

1.5 Create a .env File for Environment Variables

Create a file named .env in your project directory to store your API keys and credentials:

touch .env

Add .env to .gitignore to prevent it from being tracked by git:

echo ".env" >> .gitignore

1.6 Install FFmpeg (Required by moviepy)

  • On Linux:

    sudo apt-get install ffmpeg
    
  • On macOS (using Homebrew):

    brew install ffmpeg
    
  • On Windows:

    Download FFmpeg from the official website and add it to your system PATH.


Step 2: Obtain API Credentials

2.1 OpenAI API Key

Sign up for an OpenAI account and obtain your API key. Add it to your .env file:

OPENAI_API_KEY=your_openai_api_key_here

2.2 Social Media API Credentials

For each platform, obtain the necessary API credentials and add them to your .env file.

Instagram (Facebook Graph API)
INSTAGRAM_APP_ID=your_instagram_app_id
INSTAGRAM_APP_SECRET=your_instagram_app_secret
INSTAGRAM_ACCESS_TOKEN=your_instagram_access_token
Reddit
REDDIT_CLIENT_ID=your_reddit_client_id
REDDIT_CLIENT_SECRET=your_reddit_client_secret
REDDIT_USERNAME=your_reddit_username
REDDIT_PASSWORD=your_reddit_password
REDDIT_USER_AGENT=your_reddit_user_agent
Twitter
TWITTER_API_KEY=your_twitter_api_key
TWITTER_API_SECRET=your_twitter_api_secret
TWITTER_ACCESS_TOKEN=your_twitter_access_token
TWITTER_ACCESS_TOKEN_SECRET=your_twitter_access_token_secret
Facebook
FACEBOOK_APP_ID=your_facebook_app_id
FACEBOOK_APP_SECRET=your_facebook_app_secret
FACEBOOK_ACCESS_TOKEN=your_facebook_access_token

Step 3: Implement the Input Listener Agent

3.1 Create the agents Directory

mkdir agents

3.2 Implement input_listener.py

Create a file agents/input_listener.py:

# agents/input_listener.py

import time
import os
import praw
import tweepy
from dotenv import load_dotenv

load_dotenv()

class InputListener:
    def __init__(self):
        self.init_reddit_client()
        self.init_twitter_client()
        # Add other platforms as needed

        # Load last seen IDs
        self.last_seen = {'reddit': None, 'twitter': None}

    def init_reddit_client(self):
        self.reddit = praw.Reddit(
            client_id=os.getenv("REDDIT_CLIENT_ID"),
            client_secret=os.getenv("REDDIT_CLIENT_SECRET"),
            user_agent=os.getenv("REDDIT_USER_AGENT"),
            username=os.getenv("REDDIT_USERNAME"),
            password=os.getenv("REDDIT_PASSWORD")
        )
        self.reddit_user = self.reddit.user.me()

    def init_twitter_client(self):
        auth = tweepy.OAuth1UserHandler(
            os.getenv("TWITTER_API_KEY"),
            os.getenv("TWITTER_API_SECRET"),
            os.getenv("TWITTER_ACCESS_TOKEN"),
            os.getenv("TWITTER_ACCESS_TOKEN_SECRET")
        )
        self.twitter_api = tweepy.API(auth)
        self.twitter_username = self.twitter_api.me().screen_name

    def monitor_reddit(self):
        new_posts = []
        submissions = list(self.reddit_user.submissions.new(limit=5))
        for submission in submissions:
            if submission.id == self.last_seen.get('reddit'):
                break
            post_data = {
                'platform': 'reddit',
                'content_type': 'text',
                'content': submission.selftext,
                'title': submission.title,
                'url': submission.url,
                'id': submission.id
            }
            new_posts.append(post_data)
        if submissions:
            self.last_seen['reddit'] = submissions[0].id
        return new_posts

    def monitor_twitter(self):
        new_posts = []
        tweets = self.twitter_api.user_timeline(screen_name=self.twitter_username, count=5, tweet_mode='extended')
        for tweet in tweets:
            if str(tweet.id) == self.last_seen.get('twitter'):
                break
            post_data = {
                'platform': 'twitter',
                'content_type': 'text',
                'content': tweet.full_text,
                'id': str(tweet.id)
            }
            new_posts.append(post_data)
        if tweets:
            self.last_seen['twitter'] = str(tweets[0].id)
        return new_posts

    def monitor_platforms(self):
        new_posts = []
        new_posts.extend(self.monitor_reddit())
        new_posts.extend(self.monitor_twitter())
        # Add other platforms as needed
        return new_posts

Step 4: Implement the Content Analysis Agent

4.1 Implement content_analysis.py

Create a file agents/content_analysis.py:

# agents/content_analysis.py

import openai
import os
from dotenv import load_dotenv

load_dotenv()

class ContentAnalysisAgent:
    def __init__(self):
        openai.api_key = os.getenv("OPENAI_API_KEY")

    def analyze_content(self, content):
        prompt = f"Analyze the following content and provide key themes, tone, and intent:\n\n{content}"
        response = openai.ChatCompletion.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}]
        )
        analysis = response.choices[0].message.content.strip()
        return analysis

Step 5: Implement the Content Generation Agents

5.1 Implement Text Generation Agent

Create a file agents/text_generation_agent.py:

# agents/text_generation_agent.py

import openai
import os
from dotenv import load_dotenv

load_dotenv()

class TextGenerationAgent:
    def __init__(self):
        openai.api_key = os.getenv("OPENAI_API_KEY")

    def generate_text(self, analysis, platform):
        prompt = f"Based on the analysis:\n\n{analysis}\n\nCreate a {platform}-appropriate post that is engaging and follows the platform's style."
        response = openai.ChatCompletion.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}]
        )
        text_content = response.choices[0].message.content.strip()
        return text_content

5.2 Implement Image Generation Agent

Create a file agents/image_generation_agent.py:

# agents/image_generation_agent.py

import openai
import os
from dotenv import load_dotenv

load_dotenv()

class ImageGenerationAgent:
    def __init__(self):
        openai.api_key = os.getenv("OPENAI_API_KEY")

    def generate_image(self, prompt):
        response = openai.Image.create(
            prompt=prompt,
            n=1,
            size="1024x1024"
        )
        image_url = response['data'][0]['url']
        return image_url

Step 6: Implement the Publishing Agents

6.1 Implement publishing_agent.py

Create a file agents/publishing_agent.py:

# agents/publishing_agent.py

import os
import requests
import tweepy
from dotenv import load_dotenv

load_dotenv()

class PublishingAgent:
    def __init__(self):
        self.init_twitter_client()
        # Initialize other platforms as needed

    def init_twitter_client(self):
        auth = tweepy.OAuth1UserHandler(
            os.getenv("TWITTER_API_KEY"),
            os.getenv("TWITTER_API_SECRET"),
            os.getenv("TWITTER_ACCESS_TOKEN"),
            os.getenv("TWITTER_ACCESS_TOKEN_SECRET")
        )
        self.twitter_api = tweepy.API(auth)

    def post_to_twitter(self, text):
        try:
            self.twitter_api.update_status(status=text)
            print("Posted to Twitter.")
        except Exception as e:
            print(f"Error posting to Twitter: {e}")

    def post_to_instagram(self, image_path, caption):
        # Implement Instagram posting logic
        pass

    def post_to_facebook(self, message):
        # Implement Facebook posting logic
        pass

    # Add methods for other platforms

Step 7: Implement the Agent Coordinator

7.1 Implement coordinator.py

Create a file coordinator.py:

# coordinator.py

from agents.input_listener import InputListener
from agents.content_analysis import ContentAnalysisAgent
from agents.text_generation_agent import TextGenerationAgent
from agents.image_generation_agent import ImageGenerationAgent
from agents.publishing_agent import PublishingAgent

class AgentCoordinator:
    def __init__(self):
        self.input_listener = InputListener()
        self.content_analysis_agent = ContentAnalysisAgent()
        self.text_generation_agent = TextGenerationAgent()
        self.image_generation_agent = ImageGenerationAgent()
        self.publishing_agent = PublishingAgent()

    def coordinate(self):
        new_posts = self.input_listener.monitor_platforms()
        for post in new_posts:
            analysis = self.content_analysis_agent.analyze_content(post['content'])
            if post['platform'] == 'reddit':
                # Generate image for Instagram
                image_prompt = f"Create an image that represents the following:\n\n{analysis}"
                image_url = self.image_generation_agent.generate_image(image_prompt)
                # Download the image
                image_data = requests.get(image_url).content
                image_path = f"temp_images/{post['id']}.png"
                with open(image_path, 'wb') as handler:
                    handler.write(image_data)
                caption = post.get('title', '')
                self.publishing_agent.post_to_instagram(image_path, caption)
            elif post['platform'] == 'twitter':
                # Generate text for Facebook
                text = self.text_generation_agent.generate_text(analysis, 'Facebook')
                self.publishing_agent.post_to_facebook(text)
            # Add other platform logic as needed

if __name__ == "__main__":
    coordinator = AgentCoordinator()
    coordinator.coordinate()

7.2 Create Temporary Directory for Images

mkdir temp_images

Step 8: Automate the Workflow

8.1 Install Celery and Redis

pip install celery redis

Ensure Redis is installed and running:

  • On Linux:

    sudo apt-get install redis-server
    sudo service redis-server start
    
  • On macOS (using Homebrew):

    brew install redis
    brew services start redis
    

8.2 Set Up Celery Tasks

Create a file tasks.py:

# tasks.py

from celery import Celery
from coordinator import AgentCoordinator

app = Celery('tasks', broker='redis://localhost:6379/0')

@app.task
def run_coordinator():
    coordinator = AgentCoordinator()
    coordinator.coordinate()

8.3 Schedule the Task

Create a file celeryconfig.py:

# celeryconfig.py

from celery.schedules import crontab

beat_schedule = {
    'run-every-5-minutes': {
        'task': 'tasks.run_coordinator',
        'schedule': crontab(minute='*/5'),  # Every 5 minutes
    },
}

timezone = 'UTC'

Update your tasks.py to include the configuration:

app.config_from_object('celeryconfig')

8.4 Start Celery Worker and Beat Scheduler

In separate terminal windows, run:

Start the Celery worker:

celery -A tasks worker --loglevel=info

Start the Celery beat scheduler:

celery -A tasks beat --loglevel=info

Step 9: Implement Webhooks for Real-Time Triggers (Optional)

9.1 Install Flask

pip install flask

9.2 Create webhook_server.py

# webhook_server.py

from flask import Flask, request
from tasks import run_coordinator

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    # Process the webhook data if necessary
    run_coordinator.delay()
    return '', 200

if __name__ == "__main__":
    app.run(port=5000)

9.3 Expose Your Server (During Development)

Use ngrok to expose your local server to the internet:

ngrok http 5000

Set up the webhook URL in your platform's developer settings to point to the ngrok URL.


Step 10: Additional Notes and Considerations

  • API Limitations: Be aware of the rate limits and usage policies of each platform's API.
  • Content Moderation: Implement checks to ensure generated content complies with platform policies.
  • Error Handling: Add robust error handling and logging to your application.
  • Security: Secure your API keys and credentials. Do not expose them in your code or logs.
  • Cleanup: Delete temporary files (like downloaded images) after use to save space.

Conclusion

By following the terminal commands, instructions, and code provided in this guide, you can build an AI agent-based application that automates content creation and distribution across multiple social media platforms. This system will help you maintain an active presence online without the need to manually create and post content on each platform.


Note: This guide assumes familiarity with Python programming and working with APIs. Some steps may require adaptation based on updates to APIs or libraries. Always refer to the official documentation of the APIs and libraries used.

Happy Coding!

Related research

Nov 27, 2024

Complete Guide: Building Enhanced AI Persona Generator with Python & OpenAI - From Analysis to Response

Comprehensive tutorial for creating an intelligent AI persona generator and responder using Python, OpenAI API, and modular agent architecture for advanced writer styling and content generation.

Oct 9, 2024

Building AI Persona-Based Content Generator: Complete Python Tutorial with Jekyll Integration

Step-by-step guide to creating an intelligent blog post generator using Python, LLMs, and Jekyll. Learn to analyze writing styles, extract personas, and automate content creation with complete code examples.

Jul 6, 2025

Unlocking AI-Powered Productivity: Two Essential Guides for Building Custom AI Personas and Agentic Knowledge Graphs

Discover two groundbreaking e-books that teach you how to create custom AI personas for content creation and build agentic knowledge graphs with local LLMs for advanced AI productivity.

Feb 3, 2025

Automated Reddit Content Analytics Pipeline: Transforming Social Media Insights into Structured Blog Posts with AI Agents and Local LLMs

Comprehensive guide to building an automated content analysis pipeline that transforms Reddit posts and comments into structured blog articles using multi-agent AI systems with Ollama local LLMs.

Jan 22, 2025

Building an Advanced AI Image-to-Book Pipeline: Multimodal Storytelling with LLaVA, ChromaDB, and Recursive Narrative Generation Using Ollama

Complete technical guide to creating an AI-powered narrative generation system that transforms static images into complete books using multimodal analysis, vector databases, and recursive storytelling with LangChain and Ollama.

Dec 9, 2024

Complete Guide: Building Persona-Aware RAG Systems with Pydantic AI Agents & Tools - Custom Retrieval & Generation

Comprehensive tutorial for implementing persona-driven Retrieval-Augmented Generation systems using Pydantic AI's Agent and Tools APIs, integrating custom user personas with dynamic document retrieval for highly tailored AI responses.

More from the archive