📱 Lesson 4.3: Social Media Posting Agent

Module 4: Business Automation Use Cases


🎯 Lesson Objective

By the end of this lesson, you’ll be able to:

  • Create an AI agent that can generate social media posts (e.g., LinkedIn, Twitter/X, Instagram captions)

  • Schedule or post content using APIs

  • Customize tone, hashtags, and platform-specific rules

  • Build a reusable, branded content workflow for your business or clients


🧠 Why Automate Social Media?

Social media is vital for:

  • 📢 Brand awareness

  • 📈 Audience engagement

  • 💸 Lead generation

But manual posting is:

  • Time-consuming

  • Inconsistent

  • Often lacks variety or freshness

An AI social media agent can help you:

  • 🧠 Generate ideas and captions quickly

  • 📆 Stay consistent

  • 🧩 Adapt to trends and platform rules


🧰 Tools You’ll Use

Tool Purpose
LangChain + LLM (OpenAI or Ollama) Generate content
PromptTemplate Guide tone and structure
Schedule / cron / Streamlit Automate or review before posting
Social media APIs (Optional) Automate publishing (e.g. Twitter API, Meta Graph API)

🔧 Step-by-Step: Build the Social Media Agent


✅ 1. Define a Prompt Template

You’ll want tailored content depending on the platform:

from langchain.prompts import PromptTemplate
from langchain.llms import Ollama  # or OpenAI

llm = Ollama(model="mistral")

template = """
You are a professional social media manager.

Write a {platform} post about: "{topic}"

The tone should be {tone} and include relevant hashtags.

Post:
"""

prompt = PromptTemplate(
    input_variables=["platform", "topic", "tone"],
    template=template
)

✅ 2. Generate the Post

platform = "LinkedIn"
topic = "How AI is transforming customer support"
tone = "inspirational and professional"

post_prompt = prompt.format(platform=platform, topic=topic, tone=tone)
post_text = llm(post_prompt)

print("📢 Generated Post:n", post_text)

✅ 3. Create a Streamlit Interface (Optional UI)

import streamlit as st

st.title("🤖 Social Media Post Generator")

platform = st.selectbox("Platform", ["LinkedIn", "Twitter", "Instagram", "Facebook"])
topic = st.text_input("Post topic or idea")
tone = st.selectbox("Tone", ["Professional", "Casual", "Witty", "Motivational"])

if st.button("Generate"):
    post = llm(prompt.format(platform=platform, topic=topic, tone=tone))
    st.markdown("### ✍️ Suggested Post")
    st.text_area("", post, height=200)

✅ 4. (Optional) Post Directly via API

🔷 Example: Twitter (X) API with Tweepy

pip install tweepy
import tweepy

client = tweepy.Client(
    bearer_token="YOUR_BEARER_TOKEN",
    consumer_key="YOUR_CONSUMER_KEY",
    consumer_secret="YOUR_CONSUMER_SECRET",
    access_token="YOUR_ACCESS_TOKEN",
    access_token_secret="YOUR_ACCESS_SECRET"
)

client.create_tweet(text=post_text)
print("✅ Tweet posted!")

⚠️ Make sure you have developer access and have created an app on the platform you’re targeting.


🧪 Use Cases by Platform

Platform Content Focus Example Prompt
LinkedIn Professional, thought leadership “Share 5 tips for remote team management.”
Twitter/X Short, witty, trending “Summarize an AI breakthrough in 280 characters.”
Instagram Visual, friendly, casual “Caption for a photo of our new office.”
Facebook Community updates, casual promos “Announce a product giveaway.”

🧠 Enhancements

Feature Benefit
🔁 Scheduled posting Use schedule, cron, or Streamlit with datetime
📂 Bulk mode Input multiple topics and auto-generate a week’s worth of posts
🧠 Memory Store past posts to avoid repetition
🎯 Branding Always include logo, slogan, or product name in replies
📅 Calendar Build a content calendar to space out different content types

💬 Sample Output

Input:

  • Platform: Twitter

  • Topic: “AI in education”

  • Tone: Casual

Output:

“AI is the new classroom assistant 🍎 From personalized learning to automated grading, it’s changing how we teach and learn. #EdTech #AIinEducation”


✅ Summary

Step What You Did
🧠 Designed a prompt template To guide LLM content generation
🤖 Generated platform-specific posts With optional tone + hashtag control
📺 Built UI (optional) For team or client collaboration
📤 Posted (or scheduled) to social platforms Using platform APIs

 

51