···106106107107## Installation
108108109109+### Automated Installation (Recommended)
110110+111111+#### Linux (Ubuntu, Fedora, Arch)
112112+Run the automated installation script:
113113+114114+```bash
115115+./install.sh
116116+```
117117+118118+This script will:
119119+- Detect your Linux distribution and install required dependencies
120120+- Set up your `.env` configuration interactively
121121+- Install Node.js dependencies
122122+- Create necessary cache directories
123123+- Optionally set up a systemd service for automatic startup
124124+125125+#### Windows
126126+Run the automated installation batch file:
127127+128128+```
129129+install.bat
130130+```
131131+132132+This script will:
133133+- Check for required dependencies (Node.js, Git, FFmpeg)
134134+- Install missing dependencies using Chocolatey or winget if available
135135+- Guide you through an interactive configuration process
136136+- Install Node.js dependencies
137137+- Create necessary cache directories
138138+- Optionally set up a Windows service using PM2
139139+140140+### Manual Installation
141141+142142+If you prefer to install manually:
143143+1091441. Clone this repository
1101452. Install dependencies:
111146 ```
···121156 npm install -g pm2
122157 ```
123158159159+### Installation Script Details
160160+161161+The automated installation scripts provide several advantages:
162162+163163+1. **Dependency Management**
164164+ - Automatically installs Node.js, Git, FFmpeg, and PM2 if missing
165165+ - Uses native package managers (apt, dnf, pacman) on Linux
166166+ - Utilizes Chocolatey or winget on Windows if available
167167+168168+2. **Interactive Configuration**
169169+ - Guides you through setting up all required environment variables
170170+ - Provides sensible defaults for optional settings
171171+ - Validates that required fields like bot token are entered
172172+173173+3. **System Integration**
174174+ - Configures systemd service on Linux
175175+ - Sets up Windows service with PM2
176176+ - Creates proper directory structure for media caching
177177+124178## Environment Variables
125179180180+The bot is configured via environment variables in a `.env` file. The installation scripts will help you create this file interactively, but here's a reference of available settings:
181181+182182+### Required Settings
126183- `BOT_TOKEN`: Your Telegram bot token from BotFather
127184- `CHANNEL_ID`: Your Telegram channel ID or username (e.g., @mychannel)
185185+186186+### User Access Control
128187- `AUTHORIZED_USERS`: Comma-separated list of Telegram user IDs that are allowed to use the bot
188188+- `OWNER_ID`: (Optional) User ID of the bot owner for admin-level commands
189189+190190+### Integration Options
191191+- `WEASYL_API_KEY`: (Optional) API key for Weasyl integration
192192+- `DISCORD_WEBHOOK_URL`: (Optional) For Discord integration
193193+- `DISCORD_ENABLED`: Set to 'true' to enable Discord posting
194194+195195+### Queue Configuration
196196+- `DEFAULT_CRON_SCHEDULE`: When to post images (cron format, default: '0 */1 * * *' - every hour)
197197+- `IMAGES_PER_INTERVAL`: Number of images to post each time (default: 1)
198198+199199+### Queue Monitoring
200200+- `QUEUE_LOW_THRESHOLD`: Alert when queue has this many items or fewer (default: 10)
201201+- `QUEUE_EMPTY_THRESHOLD`: Alert when queue hits this level (default: 0)
202202+- `QUEUE_ALERTS_ENABLED`: Enable/disable queue monitoring (default: true)
203203+- `QUEUE_ALERT_COOLDOWN_HOURS`: Hours between repeated alerts (default: 24)
204204+205205+### Media Cache
206206+- `MAX_CACHE_AGE_DAYS`: Days to keep cached media files (default: 15)
129207130208## Running the Bot
131209
+329
install.bat
···11+@echo off
22+setlocal enabledelayedexpansion
33+:: Stagehand Telegram Bot Installation Script for Windows
44+:: This batch file will install the Stagehand Telegram Bot on a Windows system
55+66+echo [INFO] Starting Stagehand Telegram Bot installation...
77+88+:: Check if script is run from the correct directory
99+if not exist "package.json" (
1010+ echo [ERROR] Please run this script from the root directory of the Stagehand bot project
1111+ exit /b 1
1212+)
1313+1414+:: Detect available package managers
1515+set HAVE_WINGET=0
1616+set HAVE_CHOCO=0
1717+1818+where winget >nul 2>nul
1919+if %ERRORLEVEL% equ 0 (
2020+ set HAVE_WINGET=1
2121+ echo [INFO] Detected Windows Package Manager (winget)
2222+)
2323+2424+where choco >nul 2>nul
2525+if %ERRORLEVEL% equ 0 (
2626+ set HAVE_CHOCO=1
2727+ echo [INFO] Detected Chocolatey Package Manager
2828+)
2929+3030+echo [INFO] Installing dependencies...
3131+3232+:: Check if Node.js is installed and install if missing
3333+where node >nul 2>nul
3434+if %ERRORLEVEL% neq 0 (
3535+ echo [WARNING] Node.js is not installed.
3636+3737+ if %HAVE_WINGET% equ 1 (
3838+ echo [INFO] Installing Node.js using winget...
3939+ winget install OpenJS.NodeJS.LTS
4040+ if %ERRORLEVEL% equ 0 (
4141+ echo [SUCCESS] Node.js installed successfully using winget.
4242+ echo [INFO] You may need to restart this script to use the newly installed Node.js.
4343+ pause
4444+ exit /b 0
4545+ ) else (
4646+ echo [ERROR] Failed to install Node.js using winget.
4747+ )
4848+ ) else if %HAVE_CHOCO% equ 1 (
4949+ echo [INFO] Installing Node.js using Chocolatey...
5050+ choco install nodejs-lts -y
5151+ if %ERRORLEVEL% equ 0 (
5252+ echo [SUCCESS] Node.js installed successfully using Chocolatey.
5353+ echo [INFO] You may need to restart this script to use the newly installed Node.js.
5454+ pause
5555+ exit /b 0
5656+ ) else (
5757+ echo [ERROR] Failed to install Node.js using Chocolatey.
5858+ )
5959+ ) else (
6060+ echo [ERROR] No package manager available. Please install Node.js manually from https://nodejs.org/ and run this script again.
6161+ exit /b 1
6262+ )
6363+)
6464+6565+:: Check if Git is installed and install if missing
6666+where git >nul 2>nul
6767+if %ERRORLEVEL% neq 0 (
6868+ echo [WARNING] Git is not installed. It's recommended for updates.
6969+7070+ if %HAVE_WINGET% equ 1 (
7171+ echo [INFO] Would you like to install Git using winget? (y/n):
7272+ set /p install_git=
7373+ if /i "!install_git!"=="y" (
7474+ echo [INFO] Installing Git using winget...
7575+ winget install Git.Git
7676+ if %ERRORLEVEL% equ 0 (
7777+ echo [SUCCESS] Git installed successfully.
7878+ ) else (
7979+ echo [WARNING] Failed to install Git. Continuing anyway...
8080+ )
8181+ ) else (
8282+ echo [INFO] Skipping Git installation.
8383+ )
8484+ ) else if %HAVE_CHOCO% equ 1 (
8585+ echo [INFO] Would you like to install Git using Chocolatey? (y/n):
8686+ set /p install_git=
8787+ if /i "!install_git!"=="y" (
8888+ echo [INFO] Installing Git using Chocolatey...
8989+ choco install git -y
9090+ if %ERRORLEVEL% equ 0 (
9191+ echo [SUCCESS] Git installed successfully.
9292+ ) else (
9393+ echo [WARNING] Failed to install Git. Continuing anyway...
9494+ )
9595+ ) else (
9696+ echo [INFO] Skipping Git installation.
9797+ )
9898+ ) else (
9999+ echo [INFO] You can install Git from https://git-scm.com/downloads
100100+ )
101101+)
102102+103103+:: Check if FFmpeg is installed and install if missing
104104+where ffmpeg >nul 2>nul
105105+if %ERRORLEVEL% neq 0 (
106106+ echo [WARNING] FFmpeg is not found in PATH.
107107+108108+ if %HAVE_WINGET% equ 1 (
109109+ echo [INFO] Would you like to install FFmpeg using winget? (y/n):
110110+ set /p install_ffmpeg=
111111+ if /i "!install_ffmpeg!"=="y" (
112112+ echo [INFO] Installing FFmpeg using winget...
113113+ winget install Gyan.FFmpeg
114114+ if %ERRORLEVEL% equ 0 (
115115+ echo [SUCCESS] FFmpeg installed successfully.
116116+ ) else (
117117+ echo [WARNING] Failed to install FFmpeg. The bot will use built-in ffmpeg package.
118118+ )
119119+ ) else (
120120+ echo [INFO] The bot will use built-in ffmpeg package. If you encounter media issues,
121121+ echo [INFO] consider installing FFmpeg from https://ffmpeg.org/download.html
122122+ )
123123+ ) else if %HAVE_CHOCO% equ 1 (
124124+ echo [INFO] Would you like to install FFmpeg using Chocolatey? (y/n):
125125+ set /p install_ffmpeg=
126126+ if /i "!install_ffmpeg!"=="y" (
127127+ echo [INFO] Installing FFmpeg using Chocolatey...
128128+ choco install ffmpeg -y
129129+ if %ERRORLEVEL% equ 0 (
130130+ echo [SUCCESS] FFmpeg installed successfully.
131131+ ) else (
132132+ echo [WARNING] Failed to install FFmpeg. The bot will use built-in ffmpeg package.
133133+ )
134134+ ) else (
135135+ echo [INFO] The bot will use built-in ffmpeg package. If you encounter media issues,
136136+ echo [INFO] consider installing FFmpeg from https://ffmpeg.org/download.html
137137+ )
138138+ ) else (
139139+ echo [INFO] The bot will use built-in ffmpeg package. If you encounter media issues,
140140+ echo [INFO] consider installing FFmpeg from https://ffmpeg.org/download.html
141141+ )
142142+)
143143+144144+:: Install PM2 globally
145145+echo [INFO] Installing PM2 process manager...
146146+call npm install -g pm2
147147+if %ERRORLEVEL% neq 0 (
148148+ echo [WARNING] Failed to install PM2 using npm. Trying alternative methods...
149149+150150+ if %HAVE_CHOCO% equ 1 (
151151+ echo [INFO] Attempting to install PM2 using Chocolatey...
152152+ choco install pm2 -y
153153+ if %ERRORLEVEL% equ 0 (
154154+ echo [SUCCESS] PM2 installed successfully using Chocolatey.
155155+ ) else (
156156+ echo [ERROR] Failed to install PM2. Please install it manually with 'npm install -g pm2'.
157157+ exit /b 1
158158+ )
159159+ ) else (
160160+ echo [ERROR] Failed to install PM2. Please install it manually with 'npm install -g pm2'.
161161+ exit /b 1
162162+ )
163163+)
164164+165165+:: Check if .env file exists and decide whether to configure it
166166+if exist ".env" (
167167+ echo [INFO] .env file already exists. Reconfigure it? (y/n):
168168+ set /p RECONFIGURE=
169169+ if /i "!RECONFIGURE!"=="y" (
170170+ echo [INFO] Backing up existing .env to .env.backup
171171+ copy .env .env.backup
172172+ goto :ConfigureEnv
173173+ ) else (
174174+ echo [INFO] Using existing .env configuration.
175175+ goto :ContinueInstall
176176+ )
177177+) else (
178178+ goto :ConfigureEnv
179179+)
180180+181181+:ConfigureEnv
182182+:: Create .env file interactively
183183+echo [INFO] Setting up .env configuration file...
184184+echo [INFO] Please enter the following information (or press Enter for default values):
185185+echo.
186186+187187+:: Telegram Bot Configuration
188188+echo [CONFIG] === Telegram Bot Configuration ===
189189+set /p BOT_TOKEN="Enter Telegram Bot Token (required): "
190190+191191+if "!BOT_TOKEN!"=="" (
192192+ echo [ERROR] Bot token is required to run the bot. Continuing but you'll need to edit .env later.
193193+)
194194+195195+set /p CHANNEL_ID="Enter Telegram Channel ID (required, e.g. @yourchannel): "
196196+if "!CHANNEL_ID!"=="" (
197197+ echo [ERROR] Channel ID is required to run the bot. Continuing but you'll need to edit .env later.
198198+)
199199+200200+set /p AUTHORIZED_USERS="Enter Authorized User IDs (comma-separated, e.g. 123456,789012): "
201201+set /p OWNER_ID="Enter Owner User ID (optional): "
202202+203203+:: Discord Integration
204204+echo.
205205+echo [CONFIG] === Discord Integration (Optional) ===
206206+set /p DISCORD_INTEGRATION="Enable Discord integration? (y/n, default: n): "
207207+208208+set DISCORD_ENABLED=false
209209+set DISCORD_WEBHOOK_URL=
210210+211211+if /i "!DISCORD_INTEGRATION!"=="y" (
212212+ set DISCORD_ENABLED=true
213213+ set /p DISCORD_WEBHOOK_URL="Enter Discord Webhook URL: "
214214+)
215215+216216+:: Weasyl API Key
217217+echo.
218218+echo [CONFIG] === Weasyl API Integration (Optional) ===
219219+set /p WEASYL_API_KEY="Enter Weasyl API Key (optional): "
220220+221221+:: Queue Configuration
222222+echo.
223223+echo [CONFIG] === Queue Configuration ===
224224+set DEFAULT_CRON_SCHEDULE=0 */1 * * *
225225+set /p CRON_SCHEDULE="Enter posting schedule in cron format (default: 0 */1 * * * - every hour): "
226226+if not "!CRON_SCHEDULE!"=="" set DEFAULT_CRON_SCHEDULE=!CRON_SCHEDULE!
227227+228228+set IMAGES_PER_INTERVAL=1
229229+set /p IMG_COUNT="Enter number of images to post per interval (default: 1): "
230230+if not "!IMG_COUNT!"=="" set IMAGES_PER_INTERVAL=!IMG_COUNT!
231231+232232+:: Queue Alert Configuration
233233+echo.
234234+echo [CONFIG] === Queue Monitoring ===
235235+set QUEUE_LOW_THRESHOLD=10
236236+set /p LOW_THRESHOLD="Enter queue low threshold for alerts (default: 10): "
237237+if not "!LOW_THRESHOLD!"=="" set QUEUE_LOW_THRESHOLD=!LOW_THRESHOLD!
238238+239239+set QUEUE_EMPTY_THRESHOLD=0
240240+set /p EMPTY_THRESHOLD="Enter queue empty threshold (default: 0): "
241241+if not "!EMPTY_THRESHOLD!"=="" set QUEUE_EMPTY_THRESHOLD=!EMPTY_THRESHOLD!
242242+243243+set /p ALERTS_ENABLED="Enable queue alerts? (y/n, default: y): "
244244+set QUEUE_ALERTS_ENABLED=true
245245+if /i "!ALERTS_ENABLED!"=="n" set QUEUE_ALERTS_ENABLED=false
246246+247247+set QUEUE_ALERT_COOLDOWN_HOURS=24
248248+set /p ALERT_COOLDOWN="Enter hours between repeated alerts (default: 24): "
249249+if not "!ALERT_COOLDOWN!"=="" set QUEUE_ALERT_COOLDOWN_HOURS=!ALERT_COOLDOWN!
250250+251251+:: Cache Configuration
252252+echo.
253253+echo [CONFIG] === Cache Configuration ===
254254+set MAX_CACHE_AGE_DAYS=15
255255+set /p CACHE_AGE="Enter maximum cache age in days (default: 15): "
256256+if not "!CACHE_AGE!"=="" set MAX_CACHE_AGE_DAYS=!CACHE_AGE!
257257+258258+:: Write the configuration to .env file
259259+echo [INFO] Creating .env file with your configuration...
260260+(
261261+ echo # Telegram Bot Configuration
262262+ echo BOT_TOKEN=!BOT_TOKEN!
263263+ echo CHANNEL_ID=!CHANNEL_ID!
264264+ echo AUTHORIZED_USERS=!AUTHORIZED_USERS!
265265+ echo OWNER_ID=!OWNER_ID!
266266+ echo.
267267+ echo # Discord Integration ^(Optional^)
268268+ echo DISCORD_WEBHOOK_URL=!DISCORD_WEBHOOK_URL!
269269+ echo DISCORD_ENABLED=!DISCORD_ENABLED!
270270+ echo.
271271+ echo # Weasyl API Configuration
272272+ echo WEASYL_API_KEY=!WEASYL_API_KEY!
273273+ echo.
274274+ echo # Queue Configuration
275275+ echo DEFAULT_CRON_SCHEDULE=!DEFAULT_CRON_SCHEDULE!
276276+ echo IMAGES_PER_INTERVAL=!IMAGES_PER_INTERVAL!
277277+ echo.
278278+ echo # Queue Alert Configuration
279279+ echo QUEUE_LOW_THRESHOLD=!QUEUE_LOW_THRESHOLD!
280280+ echo QUEUE_EMPTY_THRESHOLD=!QUEUE_EMPTY_THRESHOLD!
281281+ echo QUEUE_ALERTS_ENABLED=!QUEUE_ALERTS_ENABLED!
282282+ echo QUEUE_ALERT_COOLDOWN_HOURS=!QUEUE_ALERT_COOLDOWN_HOURS!
283283+ echo.
284284+ echo # Cache Configuration
285285+ echo MAX_CACHE_AGE_DAYS=!MAX_CACHE_AGE_DAYS!
286286+) > .env
287287+288288+echo [SUCCESS] Configuration file created successfully.
289289+290290+:ContinueInstall
291291+292292+:: Install Node.js dependencies
293293+echo [INFO] Installing Node.js dependencies...
294294+call npm install
295295+if %ERRORLEVEL% neq 0 (
296296+ echo [ERROR] Failed to install Node.js dependencies
297297+ exit /b 1
298298+)
299299+300300+:: Create cache directories
301301+echo [INFO] Creating cache directories...
302302+if not exist "cache\images" mkdir cache\images
303303+if not exist "cache\videos" mkdir cache\videos
304304+if not exist "cache\transcoded" mkdir cache\transcoded
305305+306306+:: Create Windows service (using PM2)
307307+echo [INFO] Setting up Windows service...
308308+set /p install_service="Do you want to install Stagehand as a Windows service? (y/n): "
309309+if /i "%install_service%"=="y" (
310310+ echo [INFO] Installing as Windows service using PM2...
311311+ call pm2 start ecosystem.config.js
312312+ call pm2 save
313313+ call pm2 startup
314314+ echo [INFO] Please run the above command with administrator privileges if needed.
315315+) else (
316316+ echo [INFO] Skipping Windows service installation.
317317+)
318318+319319+echo.
320320+echo [SUCCESS] ===============================================================
321321+echo [SUCCESS] Stagehand Telegram Bot installation completed successfully!
322322+echo [SUCCESS] ===============================================================
323323+echo [INFO] To start the bot manually: npm start
324324+echo [INFO] To view logs: npm run logs
325325+echo [INFO] To stop the bot: npm run stop
326326+echo [INFO] Don't forget to edit the .env file with your credentials!
327327+echo [INFO] For more information, refer to the README.md file.
328328+329329+pause
+321
install.sh
···11+#!/bin/bash
22+# Stagehand Telegram Bot Installation Script for Linux
33+# Supports: Ubuntu/Debian, Fedora, and Arch Linux
44+55+# Color codes for output
66+GREEN='\033[0;32m'
77+YELLOW='\033[1;33m'
88+RED='\033[0;31m'
99+BLUE='\033[0;34m'
1010+NC='\033[0m' # No Color
1111+1212+# Function to display colored messages
1313+print_message() {
1414+ echo -e "${BLUE}[STAGEHAND]${NC} $1"
1515+}
1616+1717+print_success() {
1818+ echo -e "${GREEN}[SUCCESS]${NC} $1"
1919+}
2020+2121+print_warning() {
2222+ echo -e "${YELLOW}[WARNING]${NC} $1"
2323+}
2424+2525+print_error() {
2626+ echo -e "${RED}[ERROR]${NC} $1"
2727+}
2828+2929+# Function to detect the Linux distribution
3030+detect_distro() {
3131+ if [ -f /etc/os-release ]; then
3232+ . /etc/os-release
3333+ DISTRO=$ID
3434+ elif [ -f /etc/debian_version ]; then
3535+ DISTRO="debian"
3636+ elif [ -f /etc/fedora-release ]; then
3737+ DISTRO="fedora"
3838+ elif [ -f /etc/arch-release ]; then
3939+ DISTRO="arch"
4040+ else
4141+ DISTRO="unknown"
4242+ fi
4343+ print_message "Detected distribution: ${DISTRO}"
4444+ return 0
4545+}
4646+4747+# Function to install dependencies based on the Linux distribution
4848+install_dependencies() {
4949+ print_message "Installing dependencies..."
5050+5151+ case $DISTRO in
5252+ "ubuntu"|"debian"|"linuxmint")
5353+ print_message "Installing for Ubuntu/Debian..."
5454+ sudo apt update
5555+ sudo apt install -y git nodejs npm ffmpeg
5656+ ;;
5757+ "fedora")
5858+ print_message "Installing for Fedora..."
5959+ sudo dnf update -y
6060+ sudo dnf install -y git nodejs npm ffmpeg
6161+ ;;
6262+ "arch"|"manjaro")
6363+ print_message "Installing for Arch Linux..."
6464+ sudo pacman -Sy --noconfirm git nodejs npm ffmpeg
6565+ ;;
6666+ *)
6767+ print_error "Unsupported distribution. Please install the following manually: git, nodejs, npm, ffmpeg"
6868+ return 1
6969+ ;;
7070+ esac
7171+7272+ # Install PM2 globally
7373+ print_message "Installing PM2 process manager..."
7474+ sudo npm install -g pm2
7575+7676+ return 0
7777+}
7878+7979+# Function to create a .env file interactively
8080+create_env_file() {
8181+ print_message "Setting up environment configuration..."
8282+8383+ if [ -f .env ]; then
8484+ print_warning ".env file already exists."
8585+ read -p "Do you want to reconfigure it? (y/n): " reconfigure
8686+ if [[ $reconfigure != "y" && $reconfigure != "Y" ]]; then
8787+ print_message "Using existing .env configuration."
8888+ return 0
8989+ fi
9090+9191+ # Backup existing .env
9292+ print_message "Backing up existing .env to .env.backup"
9393+ cp .env .env.backup
9494+ fi
9595+9696+ print_message "Setting up .env configuration file interactively..."
9797+ echo ""
9898+9999+ # Telegram Bot Configuration
100100+ echo -e "${BLUE}[CONFIG]${NC} === Telegram Bot Configuration ==="
101101+ read -p "Enter Telegram Bot Token (required): " BOT_TOKEN
102102+103103+ if [ -z "$BOT_TOKEN" ]; then
104104+ print_error "Bot token is required to run the bot. Continuing but you'll need to edit .env later."
105105+ fi
106106+107107+ read -p "Enter Telegram Channel ID (required, e.g. @yourchannel): " CHANNEL_ID
108108+ if [ -z "$CHANNEL_ID" ]; then
109109+ print_error "Channel ID is required to run the bot. Continuing but you'll need to edit .env later."
110110+ fi
111111+112112+ read -p "Enter Authorized User IDs (comma-separated, e.g. 123456,789012): " AUTHORIZED_USERS
113113+ read -p "Enter Owner User ID (optional): " OWNER_ID
114114+115115+ # Discord Integration
116116+ echo ""
117117+ echo -e "${BLUE}[CONFIG]${NC} === Discord Integration (Optional) ==="
118118+ read -p "Enable Discord integration? (y/n, default: n): " DISCORD_INTEGRATION
119119+120120+ DISCORD_ENABLED="false"
121121+ DISCORD_WEBHOOK_URL=""
122122+123123+ if [[ $DISCORD_INTEGRATION == "y" || $DISCORD_INTEGRATION == "Y" ]]; then
124124+ DISCORD_ENABLED="true"
125125+ read -p "Enter Discord Webhook URL: " DISCORD_WEBHOOK_URL
126126+ fi
127127+128128+ # Weasyl API Key
129129+ echo ""
130130+ echo -e "${BLUE}[CONFIG]${NC} === Weasyl API Integration (Optional) ==="
131131+ read -p "Enter Weasyl API Key (optional): " WEASYL_API_KEY
132132+133133+ # Queue Configuration
134134+ echo ""
135135+ echo -e "${BLUE}[CONFIG]${NC} === Queue Configuration ==="
136136+ DEFAULT_CRON_SCHEDULE="0 */1 * * *"
137137+ read -p "Enter posting schedule in cron format (default: 0 */1 * * * - every hour): " CRON_SCHEDULE
138138+ if [ ! -z "$CRON_SCHEDULE" ]; then
139139+ DEFAULT_CRON_SCHEDULE="$CRON_SCHEDULE"
140140+ fi
141141+142142+ IMAGES_PER_INTERVAL="1"
143143+ read -p "Enter number of images to post per interval (default: 1): " IMG_COUNT
144144+ if [ ! -z "$IMG_COUNT" ]; then
145145+ IMAGES_PER_INTERVAL="$IMG_COUNT"
146146+ fi
147147+148148+ # Queue Alert Configuration
149149+ echo ""
150150+ echo -e "${BLUE}[CONFIG]${NC} === Queue Monitoring ==="
151151+ QUEUE_LOW_THRESHOLD="10"
152152+ read -p "Enter queue low threshold for alerts (default: 10): " LOW_THRESHOLD
153153+ if [ ! -z "$LOW_THRESHOLD" ]; then
154154+ QUEUE_LOW_THRESHOLD="$LOW_THRESHOLD"
155155+ fi
156156+157157+ QUEUE_EMPTY_THRESHOLD="0"
158158+ read -p "Enter queue empty threshold (default: 0): " EMPTY_THRESHOLD
159159+ if [ ! -z "$EMPTY_THRESHOLD" ]; then
160160+ QUEUE_EMPTY_THRESHOLD="$EMPTY_THRESHOLD"
161161+ fi
162162+163163+ QUEUE_ALERTS_ENABLED="true"
164164+ read -p "Enable queue alerts? (y/n, default: y): " ALERTS_ENABLED
165165+ if [[ $ALERTS_ENABLED == "n" || $ALERTS_ENABLED == "N" ]]; then
166166+ QUEUE_ALERTS_ENABLED="false"
167167+ fi
168168+169169+ QUEUE_ALERT_COOLDOWN_HOURS="24"
170170+ read -p "Enter hours between repeated alerts (default: 24): " ALERT_COOLDOWN
171171+ if [ ! -z "$ALERT_COOLDOWN" ]; then
172172+ QUEUE_ALERT_COOLDOWN_HOURS="$ALERT_COOLDOWN"
173173+ fi
174174+175175+ # Cache Configuration
176176+ echo ""
177177+ echo -e "${BLUE}[CONFIG]${NC} === Cache Configuration ==="
178178+ MAX_CACHE_AGE_DAYS="15"
179179+ read -p "Enter maximum cache age in days (default: 15): " CACHE_AGE
180180+ if [ ! -z "$CACHE_AGE" ]; then
181181+ MAX_CACHE_AGE_DAYS="$CACHE_AGE"
182182+ fi
183183+184184+ # Write the configuration to .env file
185185+ print_message "Creating .env file with your configuration..."
186186+ cat > .env << EOF
187187+# Telegram Bot Configuration
188188+BOT_TOKEN=$BOT_TOKEN
189189+CHANNEL_ID=$CHANNEL_ID
190190+AUTHORIZED_USERS=$AUTHORIZED_USERS
191191+OWNER_ID=$OWNER_ID
192192+193193+# Discord Integration (Optional)
194194+DISCORD_WEBHOOK_URL=$DISCORD_WEBHOOK_URL
195195+DISCORD_ENABLED=$DISCORD_ENABLED
196196+197197+# Weasyl API Configuration
198198+WEASYL_API_KEY=$WEASYL_API_KEY
199199+200200+# Queue Configuration
201201+DEFAULT_CRON_SCHEDULE=$DEFAULT_CRON_SCHEDULE
202202+IMAGES_PER_INTERVAL=$IMAGES_PER_INTERVAL
203203+204204+# Queue Alert Configuration
205205+QUEUE_LOW_THRESHOLD=$QUEUE_LOW_THRESHOLD
206206+QUEUE_EMPTY_THRESHOLD=$QUEUE_EMPTY_THRESHOLD
207207+QUEUE_ALERTS_ENABLED=$QUEUE_ALERTS_ENABLED
208208+QUEUE_ALERT_COOLDOWN_HOURS=$QUEUE_ALERT_COOLDOWN_HOURS
209209+210210+# Cache Configuration
211211+MAX_CACHE_AGE_DAYS=$MAX_CACHE_AGE_DAYS
212212+EOF
213213+214214+ print_success "Configuration file created successfully."
215215+ return 0
216216+}
217217+218218+# Function to install the application
219219+install_app() {
220220+ print_message "Installing Stagehand bot..."
221221+222222+ # Install Node.js dependencies
223223+ print_message "Installing Node.js dependencies..."
224224+ npm install
225225+226226+ if [ $? -ne 0 ]; then
227227+ print_error "Failed to install Node.js dependencies"
228228+ return 1
229229+ fi
230230+231231+ print_success "Stagehand bot installed successfully!"
232232+ return 0
233233+}
234234+235235+# Function to set up systemd service
236236+setup_systemd_service() {
237237+ print_message "Setting up systemd service for automatic startup..."
238238+239239+ # Check if user wants to install the service
240240+ read -p "Do you want to install Stagehand as a systemd service? (y/n): " install_service
241241+242242+ if [[ $install_service != "y" && $install_service != "Y" ]]; then
243243+ print_message "Skipping systemd service installation."
244244+ return 0
245245+ fi
246246+247247+ # Get current username for service configuration
248248+ CURRENT_USER=$(whoami)
249249+ CURRENT_DIR=$(pwd)
250250+251251+ # Update the username in the service file
252252+ print_message "Updating service file with current user: $CURRENT_USER"
253253+ sed -i "s/User=henrick/User=$CURRENT_USER/g" stagehand.service
254254+255255+ # Update working directory in service file
256256+ print_message "Updating service file with current directory: $CURRENT_DIR"
257257+ sed -i "s|WorkingDirectory=.*|WorkingDirectory=$CURRENT_DIR|g" stagehand.service
258258+259259+ # Run the service installation script
260260+ print_message "Installing systemd service..."
261261+ sudo ./install-service.sh
262262+263263+ if [ $? -ne 0 ]; then
264264+ print_error "Failed to install systemd service"
265265+ return 1
266266+ fi
267267+268268+ print_success "Systemd service installed successfully!"
269269+ return 0
270270+}
271271+272272+# Main installation function
273273+main() {
274274+ print_message "Starting Stagehand Telegram Bot installation..."
275275+276276+ # Check if script is run from the correct directory
277277+ if [ ! -f "package.json" ] || [ ! -f "index.js" ]; then
278278+ print_error "Please run this script from the root directory of the Stagehand bot project"
279279+ exit 1
280280+ fi
281281+282282+ # Detect the Linux distribution
283283+ detect_distro
284284+285285+ # Install dependencies
286286+ install_dependencies
287287+ if [ $? -ne 0 ]; then
288288+ print_error "Failed to install dependencies"
289289+ exit 1
290290+ fi
291291+292292+ # Create .env file
293293+ create_env_file
294294+295295+ # Install the application
296296+ install_app
297297+ if [ $? -ne 0 ]; then
298298+ print_error "Failed to install application"
299299+ exit 1
300300+ fi
301301+302302+ # Set up systemd service
303303+ setup_systemd_service
304304+305305+ print_message "Creating cache directories..."
306306+ mkdir -p cache/images
307307+ mkdir -p cache/videos
308308+ mkdir -p cache/transcoded
309309+310310+ print_success "==============================================================="
311311+ print_success "Stagehand Telegram Bot installation completed successfully!"
312312+ print_success "==============================================================="
313313+ print_message "To start the bot manually: npm start"
314314+ print_message "To view logs: npm run logs"
315315+ print_message "To stop the bot: npm run stop"
316316+ print_message "Don't forget to edit the .env file with your credentials!"
317317+ print_message "For more information, refer to the README.md file."
318318+}
319319+320320+# Execute main function
321321+main