GuidesBuild an applicationBuilding an AI-Powered YouTube Comment Manager Bot

Building an AI-Powered YouTube Comment Manager Bot

A guide by

Introduction #

This template and Guide will help you build your own AI YouTube comment manager to engage with your YouTube audience automatically and intelligently. This bot analyzes your video's content and responds to comments with contextually relevant replies, taking into account your video's title, description, and the viewer's comment.

To begin, fork this template below:

You will also need this Google Sheet template.

Setting up API Keys

Google Cloud Platform Setup

  • Create a new project in Google Cloud Console
  • Enable these APIs:
    • YouTube Data API v3
    • Google Sheets API
  • Set up OAuth 2.0 credentials:
  • Set up authorized redirect URIs:
    • https://developers.google.com/oauthplayground
    • Your Replit app URLs:
      • Your development url ends in replit.dev and can be found in the Networking tool
      • Your production url will end in replit.app and will be revealed once you deploy your app.
  • Save your CLIENT_ID and CLIENT_SECRET

OpenAI API Key:

OpenAI "Create new secret key" modal UI
  • Log in to the OpenAI developer platform.
  • Navigate to API keys and create a new secret key.
  • Save your key in Replit Secrets as OPEN_AI_API_KEY

Setting up OAuth Tokens

  • Visit OAuth 2.0 Playground
  • Configure OAuth credentials:
    • Click Settings (⚙️)
    • Check "Use your own OAuth credentials"
    • Enter your Client ID and Client Secret
  • Select these scopes:

1https://www.googleapis.com/auth/youtube.force-ssl 2https://www.googleapis.com/auth/spreadsheets
  • Click "Authorize APIs"
  • Exchange authorization code for tokens
  • Save your ACCESS_TOKEN and REFRESH_TOKEN

Breaking Down the Code #

Credential Management

The bot uses a robust token refresh system:

python

1def get_credentials(): 2 creds = Credentials( 3 token=config.ACCESS_TOKEN, 4 refresh_token=config.REFRESH_TOKEN, 5 token_uri="https://oauth2.googleapis.com/token", 6 client_id=config.CLIENT_ID, 7 client_secret=config.CLIENT_SECRET, 8 scopes=config.SCOPES 9 ) 10 11 if not creds.valid: 12 if creds.expired and creds.refresh_token: 13 creds.refresh(Request()) 14 config.update_access_token(creds.token)

Context-Aware Comment Processing

The bot fetches video context for more relevant responses:

python

1def get_new_comments(channel_id, youtube, last_checked): 2 video_info_cache = {} 3 4 # Fetch video metadata for context 5 video_response = youtube.videos().list( 6 part="snippet", 7 id=video_id 8 ).execute() 9 10 video_snippet = video_response['items'][0]['snippet'] 11 video_title = video_snippet.get('title', 'Unknown Title') 12 video_description = video_snippet.get('description', '')[:500]

AI Response Generation

The bot creates contextual prompts for the AI:

python

1prompt = ( 2 f"Video Title: {video_title}\n" 3 f"Video Description: {video_description}\n" 4 f"Comment: {original_text}\n\n" 5 "As the video creator, respond briefly and relevantly to this YouTube comment." 6) 7 8response_text = get_openai_response(prompt)

Automated Scheduling

The bot uses BackgroundScheduler for regular checks:

python

1scheduler = BackgroundScheduler(executors=executors) 2scheduler.add_job(scheduled_fetch, 'interval', minutes=30) 3scheduler.add_job(refresh_token_job, 'interval', hours=1)

Comprehensive Logging

Every interaction is tracked in Google Sheets:

python

1def log_comment_response(sheet, comment_id, video_id, video_title, 2 comment_text, response_text, video_description=None): 3 timestamp = datetime.now().isoformat() 4 row = [comment_id_str, video_id, video_title, video_description, 5 comment_text, response_text, timestamp] 6 sheet.append_row(row)

What's Next #

Take your YouTube comment bot further by:

  • Adding sentiment analysis to tailor response tones
  • Implementing multi-language support using translation APIs
  • Creating a dashboard to monitor response quality and engagement metrics
  • Adding custom response templates for different types of comments (questions, praise, feedback)

If you’d like to bring this project and similar templates into your team, set some time here with the Replit team for a quick demo of Replit Teams.

Happy coding!