4.2 KiB
YouTube Chat Webhook Listener (Version 2)
This project aims to create a more robust and quota-friendly YouTube Live Chat monitor by leveraging third-party streaming services (like StreamElements) that provide webhook notifications for new chat messages. This approach avoids constant polling of the YouTube Data API, thus eliminating quota issues and providing near real-time chat updates.
Project Goal
To build a Python application that listens for chat message webhooks from a third-party service, processes them, and displays them in the terminal, effectively creating an event-driven chat monitor.
Detailed Implementation Plan
Phase 1: Setup Local Webhook Server
-
Choose a Python Web Framework: We will use
Flaskfor its simplicity in setting up a basic web server.- Action: Install Flask:
pip install Flask
- Action: Install Flask:
-
Create
webhook_server.py: This file will contain the Flask application.- Action: Create
webhook_server.pywith a basic Flask app structure.
- Action: Create
-
Define Webhook Endpoint: Implement a POST endpoint (e.g.,
/chat-webhook) that will receive JSON payloads from the third-party service.- Action: Add
@app.route('/chat-webhook', methods=['POST'])towebhook_server.py.
- Action: Add
-
Parse Incoming Data: The Flask app will need to parse the JSON data received from the webhook. The structure of this data will depend on the chosen third-party service (e.g., StreamElements).
- Action: Add logic to
handle_webhook()to extractusernameandmessagefromrequest.json.
- Action: Add logic to
-
Queue Messages: Instead of directly printing, the Flask app will put received messages into a Python
queue.Queueobject. This queue will be shared with the main display thread.- Action: Implement a shared
queue.Queueand add messages to it.
- Action: Implement a shared
-
Run Flask App in a Separate Thread: The Flask development server should run in a separate thread to avoid blocking the main application.
- Action: Implement
run_flask_app()function and start it in athreading.Thread.
- Action: Implement
Phase 2: Integrate with Main Display Logic
-
Modify
main.py: The existingmain.pywill be adapted to become the display client.- Action: Remove all YouTube Data API polling logic (
fetch_live_chat_messages,get_live_chat_id).
- Action: Remove all YouTube Data API polling logic (
-
Start Webhook Server:
main.pywill initiate thewebhook_server.pyin a separate thread.- Action: Import
webhook_serverand start its Flask thread inmain().
- Action: Import
-
Retrieve and Display Messages:
main.pywill continuously check the shared queue for new messages from the webhook server and display them usingrichstyling.- Action: Implement a loop in
main()toqueue.get_nowait()messages andconsole.print()them.
- Action: Implement a loop in
-
Maintain Terminal Display: Ensure
richstyling (colors, alternating backgrounds) is applied to webhook-received messages.- Action: Adapt existing
richstyling logic for new message source.
- Action: Adapt existing
Phase 3: Third-Party Service Configuration (Manual Steps)
-
Choose a Service: Select a streaming service that provides webhook functionality (e.g., StreamElements, Streamlabs).
- Action: User to sign up/log in to chosen service.
-
Link YouTube Channel: Connect your YouTube channel to the chosen service.
- Action: User to follow service-specific instructions.
-
Configure Webhook: Find the webhook settings within the service's dashboard.
- Action: User to configure a webhook to send POST requests on new chat messages.
-
Expose Local Server (ngrok): Since webhook services need a public URL, you'll likely need a tunneling service like
ngrokduring development.- Action: User to install
ngrokand runngrok http 5000(if Flask runs on port 5000). Use the provided publichttpsURL in the webhook configuration.
- Action: User to install
-
Test: Send a message in your YouTube live chat and verify it appears in your terminal via the webhook.
Dependencies
Flaskrichngrok(for development/testing with public webhooks)
Future Enhancements
- Interactive message sending via webhook (if supported by the third-party service).
- More advanced terminal UI (e.g.,
prompt_toolkitfor input). - Web overlay integration.