A Python script that polls the Twitch API to detect when a channel goes live and sends customized notifications to a Discord webhook.
- Automatic notifications when a stream goes live
- Customizable Discord embed notifications
- Game change notifications
- Viewer milestone notifications (e.g., when reaching 50, 100, 500 viewers)
- Configurable polling intervals
- Test notification mode
- State persistence between runs
- Robust error handling with retry logic
- Network connectivity checking
- Watchdog service to auto-restart on failures
-
Install the required dependencies:
pip install -r requirements.txt
-
Configure your settings using either environment variables or a config file:
Option 1 (Recommended for Security): Using Environment Variables
You can set the essential environment variables directly in your terminal or create a
.env
file in the project directory:TWITCH_CLIENT_ID=your_client_id TWITCH_CLIENT_SECRET=your_client_secret TWITCH_CHANNEL_NAME=channel_to_monitor DISCORD_WEBHOOK_URL=your_webhook_url
Option 2: Using Config File
If you prefer using a config file:
cp config.template.json config.json
Then edit
config.json
with your favorite text editor and add your credentials.IMPORTANT: Never commit files with real credentials to public repositories
-
Run the script:
python twitch_discord_notifier.py
You can customize the notifier using environment variables (recommended) or a config file. Environment variables take precedence over the config file if both are present.
The following environment variables are essential for using the application:
Required Settings:
TWITCH_CLIENT_ID=your_client_id
TWITCH_CLIENT_SECRET=your_client_secret
TWITCH_CHANNEL_NAME=channel_to_monitor
DISCORD_WEBHOOK_URL=your_webhook_url
Optional Settings:
NOTIFICATION_MESSAGE_TEMPLATE=🔴 **LIVE NOW!** {streamer} is streaming {game}
NOTIFICATION_INCLUDE_TITLE=true
NOTIFICATION_INCLUDE_GAME=true
NOTIFICATION_INCLUDE_VIEWER_COUNT=true
NOTIFICATION_INCLUDE_THUMBNAIL=true
NOTIFICATION_EMBED_COLOR=FF0000
POLLING_INTERVAL_SECONDS=60
POLLING_OFFLINE_CHECK_MULTIPLIER=3
POLLING_NOTIFICATION_COOLDOWN_MINUTES=15
ADVANCED_VIEWER_MILESTONE_NOTIFICATIONS=50,100,500,1000
ADVANCED_SILENT_MODE=false
To avoid setting environment variables each time you open a new terminal:
-
Using .env file (Recommended): Create a
.env
file in the project directory with your variables. -
macOS/Linux: Add to your
~/.zshrc
or~/.bash_profile
:export TWITCH_CLIENT_ID=your_client_id export TWITCH_CLIENT_SECRET=your_client_secret export TWITCH_CHANNEL_NAME=channel_to_monitor export DISCORD_WEBHOOK_URL=your_webhook_url
Alternatively, you can use a config.json
file with this structure:
{
"twitch": {
"client_id": "YOUR_TWITCH_CLIENT_ID",
"client_secret": "YOUR_TWITCH_CLIENT_SECRET",
"channel_name": "CHANNEL_TO_MONITOR"
},
"discord": {
"webhook_url": "YOUR_DISCORD_WEBHOOK_URL"
},
"notification": {
"message_template": "🔴 **LIVE NOW!** {streamer} is streaming {game}",
"include_title": true,
"include_game": true,
"include_viewer_count": true,
"include_thumbnail": true,
"notify_on_game_change": false,
"embed_color": "FF0000"
},
"polling": {
"interval_seconds": 60,
"offline_check_multiplier": 3,
"notification_cooldown_minutes": 15
},
"advanced": {
"viewer_milestone_notifications": [50, 100, 500, 1000],
"silent_mode": false
}
}
--config PATH
: Specify an alternative config file path--test
: Send a test notification--verbose
: Enable verbose logging--debug-api
: Print API request details for debugging
To ensure the script keeps running even if it encounters errors, you can use the included watchdog service:
-
Make the watchdog script executable:
chmod +x watchdog.sh
-
Start the watchdog:
./watchdog.sh
The watchdog will monitor the main script and restart it automatically if it crashes.
For a more permanent solution on macOS, you can set up the script as a LaunchAgent:
-
Copy the provided plist file to your LaunchAgents directory:
cp com.user.twitch-discord-notifier.plist ~/Library/LaunchAgents/
-
Load the LaunchAgent:
launchctl load ~/Library/LaunchAgents/com.user.twitch-discord-notifier.plist
The service will now start automatically when you log in and will be restarted if it crashes.
- Create a Twitch Developer account at dev.twitch.tv
- Register a new application to obtain a client ID and client secret
- Make sure to use valid credentials - invalid credentials will result in authentication errors like:
Failed to authenticate with Twitch: 400 Client Error: Bad Request
If you see errors like:
Error fetching stream info: HTTPSConnectionPool(host='api.twitch.tv', port=443): Max retries exceeded with url: /helix/streams?user_login=channelname (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x...>: Failed to resolve 'api.twitch.tv' ([Errno 8] nodename nor servname provided, or not known)"))
This indicates DNS resolution or network connectivity issues. The script now includes:
- Network connectivity checks before making API calls
- Automatic retry logic with exponential backoff
- A watchdog service that restarts the script if it crashes
If you encounter errors with Twitch authentication:
- Verify your client ID and client secret are correct
- Ensure your Twitch Developer application has the correct scopes
- Check that your application is approved by Twitch
The script generates log files that can help with troubleshooting:
output.log
: Contains standard output messageserror.log
: Contains error messageswatchdog.log
: Contains watchdog service logs
This project is licensed under the BSD 2-Clause License - see the LICENSE file for details.