Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Your Telegram bot token
- BOT_TOKEN="in here"
- # The group chat ID
- CHAT_ID="in here"
- # Lock file
- lock_file="/home/mfbot/mfbot-webinterface/MFBot_Konsole_ARM64.lock"
- # Path to ngrok executable
- NGROK_PATH="/home/mfbot/mfbot-webinterface/ngrok"
- # Flag to indicate if MFBot has started
- mfbot_started=false
- # Last processed message ID and timestamp
- LAST_PROCESSED_ID=0
- LAST_PROCESSED_TIMESTAMP=0
- # Script start time
- SCRIPT_START_TIME=$(date +%s)
- # File to store processed update IDs
- processed_updates_file="/home/mfbot/processed_updates.txt"
- # Check if processed_updates_file exists, if not, create it
- [ -e "$processed_updates_file" ] || touch "$processed_updates_file"
- # Function to log messages
- log() {
- echo "$(date +"%Y-%m-%d %H:%M:%S") $1" >> /home/mfbot/script.log
- }
- # Function to send a message
- send_message() {
- message_text="$1"
- formatted_message=$(echo -e "$message_text")
- curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" -d chat_id="$CHAT_ID" -d text="$formatted_message" -d parse_mode="HTML"
- sleep 1 # Add a sleep duration to avoid spamming
- }
- # Welcome message
- send_message "Welcome to MFBot Curator! Type /help to see available commands."
- # Function to check internet connectivity
- check_internet() {
- if ping -q -c 1 -W 1 google.com >/dev/null; then
- return 0 # Internet is available
- else
- return 1 # Internet is not available
- fi
- }
- # Function to get current temperature
- get_cpu_temperature() {
- temperature=$(sensors | awk '/temp1/ {print $2; exit}')
- if [ -n "$temperature" ]; then
- echo "$temperature"
- else
- echo "Failed to retrieve CPU temperature."
- fi
- }
- # Function to start ngrok with retries
- start_ngrok() {
- retries=3 # Number of retry attempts
- attempt=0
- while [ "$attempt" -lt "$retries" ]; do
- attempt=$((attempt + 1))
- echo " Starting ngrok (Attempt $attempt)..."
- $NGROK_PATH http 8050 > /dev/null &
- # Wait for ngrok to be available on port 8050
- while ! nc -z localhost 4040; do
- sleep 0.2
- done
- # Get ngrok dynamic URL
- NGROK_RESPONSE="$(curl -s http://localhost:4040/api/tunnels)"
- NGROK_REMOTE_URL="$(echo "${NGROK_RESPONSE}" | jq -r ".tunnels[0].public_url // empty")"
- if [ -z "${NGROK_REMOTE_URL}" ] || [ "${NGROK_REMOTE_URL}" = "null" ]; then
- echo "ERROR: ngrok doesn't seem to return a valid URL (${NGROK_REMOTE_URL})."
- if [ "$attempt" -lt "$retries" ]; then
- echo "Retrying..."
- sleep 5 # Wait before retrying
- continue
- else
- echo "Exceeded maximum retry attempts. Exiting..."
- exit 1
- fi
- fi
- # Trim double quotes from variable
- NGROK_REMOTE_URL="${NGROK_REMOTE_URL//\"}"
- # If http protocol is returned, replace with https
- NGROK_REMOTE_URL=${NGROK_REMOTE_URL/http:\/\//https:\/\/}
- echo "ngrok started successfully!"
- echo "Your ngrok URL is: $NGROK_REMOTE_URL"
- send_message "ngrok started successfully! Your ngrok URL is: $NGROK_REMOTE_URL"
- break # Exit the loop if ngrok starts successfully
- done
- }
- # Function to start MFBot
- start_mfbot() {
- log "Checking and starting MFBot_Konsole_ARM64 and MainProgram.py."
- if [ -e "$lock_file" ]; then
- old_pid=$(cat "$lock_file")
- if ps -p "$old_pid" > /dev/null; then
- log "Error: Lock file $lock_file exists. Another instance is already running with PID $old_pid. Continuing anyway."
- send_message "Warning: Another instance of MFBot is already running. Continuing anyway."
- else
- log "Warning: Stale lock file detected. The previous process with PID $old_pid is not running. Removing the lock file."
- rm "$lock_file"
- fi
- else
- touch "$lock_file"
- fi
- # Start MFBot_Konsole_ARM64
- if ! pgrep -f "MFBot_Konsole_ARM64" > /dev/null; then
- log "Starting MFBot_Konsole_ARM64..."
- cd /home/mfbot && ./MFBot_Konsole_ARM64 &
- sleep 10
- else
- log "MFBot_Konsole_ARM64 is already running."
- send_message "MFBot_Konsole_ARM64 is already running."
- fi
- # Start MainProgram.py
- if ! pgrep -f "python3.6" > /dev/null; then
- log "Starting MainProgram.py..."
- python3.6 /home/mfbot/mfbot-webinterface/MainProgram.py -a http://127.0.0.1:4711/ --remoteU=root --remoteP=orangepi --webU=mfbot --webP=mfbot &
- sleep 10
- else
- log "python3.6 is already running."
- send_message "python3.6 is already running."
- fi
- log "MFBot_Konsole_ARM64 and MainProgram.py started successfully."
- send_message "MFBot_Konsole_ARM64 and MainProgram.py started successfully."
- # Set the flag to indicate that MFBot has started
- mfbot_started=true
- }
- # Function to check internet connectivity and perform actions
- check_and_handle_internet() {
- if $mfbot_started; then
- # Check internet only if MFBot has started
- if check_internet; then
- log "Internet is available."
- # Kill MFBot, Ngrok, and Python3.6
- killall MFBot_Konsole_ARM64 ngrok python3.6
- cd /home/mfbot && rm "$lock_file"
- # Give some time for components to restart
- sleep 30
- # Start ngrok again on port 8050
- start_ngrok
- # Reset the flag after processing
- mfbot_started=false
- else
- log "Internet is not available. Waiting for reconnection..."
- send_message "Internet is not available. Waiting for reconnection..."
- fi
- fi
- }
- # Function to check the status of a component
- check_component_status() {
- component_name="$1"
- if [ "$component_name" = "mf_web" ]; then
- if nc -z localhost 8050; then
- echo "up"
- else
- echo "down"
- fi
- else
- if pgrep -x "MFBot_Konsole_ARM64" > /dev/null; then
- echo "up"
- else
- echo "down"
- fi
- fi
- }
- # Function to retrieve a random fun fact
- get_random_fun_fact() {
- # API endpoint for a random fun fact
- api_endpoint="https://uselessfacts.jsph.pl/api/v2/facts/random"
- # Use curl to fetch the data
- api_response=$(curl -s "$api_endpoint")
- # Extract the text from the response
- fun_fact_text=$(echo "$api_response" | jq -r '.text // empty')
- if [ -n "$fun_fact_text" ]; then
- echo "$fun_fact_text"
- else
- echo "Failed to retrieve a fun fact."
- fi
- }
- # Process Telegram commands
- while true; do
- # Retrieve Telegram updates
- updates=$(curl -s "https://api.telegram.org/bot$BOT_TOKEN/getUpdates?offset=$((LAST_PROCESSED_ID + 1))")
- # Check if updates is not empty
- if [ -n "$updates" ]; then
- # Extract relevant information from updates
- for update in $(echo "$updates" | jq -c '.result[] // empty'); do
- update_id=$(echo "$update" | jq -r '.update_id // empty')
- # Check if the update has been processed
- if grep -q "$update_id" "$processed_updates_file"; then
- continue # Skip processing this update
- fi
- message=$(echo "$update" | jq -r '.message.text // empty')
- message_timestamp=$(echo "$update" | jq -r '.message.date // empty')
- # Ignore messages received before the script start time or null values
- if [ -n "$message_timestamp" ] && [ "$message_timestamp" -lt "$SCRIPT_START_TIME" ]; then
- continue
- fi
- # Ignore old messages
- if [ "$message_timestamp" -lt "$LAST_PROCESSED_TIMESTAMP" ]; then
- continue
- fi
- if [ -n "$message" ]; then
- if [[ "$message" == "/"* ]]; then
- case "$message" in
- "/start")
- if [ "$mfbot_started" = true ]; then
- send_message "MFBot is already started. If you want to reset, use /softreset."
- else
- send_message "Starting, please wait. This is going to take a while..."
- fun_fact=$(get_random_fun_fact)
- send_message "Here's a fun fact while you wait:\n$fun_fact"
- # Set the flag to indicate that MFBot has started
- mfbot_started=true
- # Start autostart.sh
- start_mfbot
- # Start ngrok and send the URL
- start_ngrok
- fi
- ;;
- "/status")
- # Check the status of MFBot_Konsole_ARM64
- mfbot_konsole_status=$(check_component_status "MFBot_Konsole_ARM64")
- # Check the status of ngrok
- ngrok_status=$(check_component_status "ngrok")
- # Check the status of mf_web (port 8050)
- mf_web_status=$(check_component_status 8050)
- send_message "MFBot_Konsole_ARM64: $mfbot_konsole_status\nngrok: $ngrok_status\nmf_web: $mf_web_status"
- ;;
- "/temp")
- # Get current temperature
- cpu_temperature=$(sensors | awk '/temp1/ {print $2; exit}')
- if [ -n "$cpu_temperature" ]; then
- temperature_message="CPU Temperature: $cpu_temperature"
- else
- temperature_message="Failed to retrieve CPU temperature."
- fi
- # Send the temperature as a reply
- send_message "$temperature_message"
- ;;
- "/stop")
- # Stop MFBot, Ngrok, and Python3.6
- send_message "Stopping MFBot, Ngrok, and Python3.6..."
- killall MFBot_Konsole_ARM64 ngrok python3.6
- cd /home/mfbot && rm "$lock_file"
- # Set the flag to indicate that MFBot has started
- mfbot_started=false
- send_message "MFBot, Ngrok, and Python3.6 stopped."
- ;;
- "/softreset")
- # Soft reset (kill MFBot, Ngrok, and Python3.6, restart using autostart.sh)
- send_message "Soft reset in progress. This is going to take a while..."
- fun_fact=$(get_random_fun_fact)
- send_message "Here's a fun fact while you wait:\n$fun_fact"
- killall MFBot_Konsole_ARM64 ngrok python3.6
- cd /home/mfbot && rm "$lock_file"
- start_mfbot
- start_ngrok
- ;;
- "/url")
- # Get the last sent ngrok URL
- send_message "Your ngrok current URL is: $NGROK_REMOTE_URL"
- ;;
- "/fun_fact")
- # Get and send a random fun fact
- fun_fact=$(get_random_fun_fact)
- send_message "Here's a fun fact for you:\n$fun_fact"
- ;;
- "/help")
- # Display help message
- send_message "Available commands:\n/start - Start the bot\n/stop - Stop MFBot, Ngrok, and Python3.6\n/status - Get server status\n/temp - Get current temperature\n/softreset - Soft reset (kill MFBot, Ngrok, and Python3.6, restart using autostart.sh)\n/url - Get the last sent ngrok URL\n/fun_fact - Get a random fun fact\n/help - Display this help message"
- ;;
- *)
- send_message "Unknown command: $message"
- ;;
- esac
- fi
- fi
- # Update the last processed message timestamp
- LAST_PROCESSED_TIMESTAMP=$message_timestamp
- # Update the last processed message ID
- LAST_PROCESSED_ID=$update_id
- # Store the processed update ID
- echo "$update_id" >> "$processed_updates_file"
- done
- fi
- # Sleep for a while before checking for updates again
- sleep 1
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement