Advertisement
Guest User

mf_Curator.sh

a guest
Dec 20th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.09 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Your Telegram bot token
  4. BOT_TOKEN="in here"
  5.  
  6. # The group chat ID
  7. CHAT_ID="in here"
  8.  
  9. # Lock file
  10. lock_file="/home/mfbot/mfbot-webinterface/MFBot_Konsole_ARM64.lock"
  11.  
  12. # Path to ngrok executable
  13. NGROK_PATH="/home/mfbot/mfbot-webinterface/ngrok"
  14.  
  15. # Flag to indicate if MFBot has started
  16. mfbot_started=false
  17.  
  18. # Last processed message ID and timestamp
  19. LAST_PROCESSED_ID=0
  20. LAST_PROCESSED_TIMESTAMP=0
  21.  
  22. # Script start time
  23. SCRIPT_START_TIME=$(date +%s)
  24.  
  25. # File to store processed update IDs
  26. processed_updates_file="/home/mfbot/processed_updates.txt"
  27.  
  28. # Check if processed_updates_file exists, if not, create it
  29. [ -e "$processed_updates_file" ] || touch "$processed_updates_file"
  30.  
  31. # Function to log messages
  32. log() {
  33. echo "$(date +"%Y-%m-%d %H:%M:%S") $1" >> /home/mfbot/script.log
  34. }
  35.  
  36. # Function to send a message
  37. send_message() {
  38. message_text="$1"
  39. formatted_message=$(echo -e "$message_text")
  40. 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"
  41. sleep 1 # Add a sleep duration to avoid spamming
  42. }
  43.  
  44. # Welcome message
  45. send_message "Welcome to MFBot Curator! Type /help to see available commands."
  46.  
  47. # Function to check internet connectivity
  48. check_internet() {
  49. if ping -q -c 1 -W 1 google.com >/dev/null; then
  50. return 0 # Internet is available
  51. else
  52. return 1 # Internet is not available
  53. fi
  54. }
  55.  
  56. # Function to get current temperature
  57. get_cpu_temperature() {
  58. temperature=$(sensors | awk '/temp1/ {print $2; exit}')
  59. if [ -n "$temperature" ]; then
  60. echo "$temperature"
  61. else
  62. echo "Failed to retrieve CPU temperature."
  63. fi
  64. }
  65.  
  66. # Function to start ngrok with retries
  67. start_ngrok() {
  68. retries=3 # Number of retry attempts
  69. attempt=0
  70.  
  71. while [ "$attempt" -lt "$retries" ]; do
  72. attempt=$((attempt + 1))
  73. echo " Starting ngrok (Attempt $attempt)..."
  74. $NGROK_PATH http 8050 > /dev/null &
  75.  
  76. # Wait for ngrok to be available on port 8050
  77. while ! nc -z localhost 4040; do
  78. sleep 0.2
  79. done
  80.  
  81. # Get ngrok dynamic URL
  82. NGROK_RESPONSE="$(curl -s http://localhost:4040/api/tunnels)"
  83. NGROK_REMOTE_URL="$(echo "${NGROK_RESPONSE}" | jq -r ".tunnels[0].public_url // empty")"
  84.  
  85. if [ -z "${NGROK_REMOTE_URL}" ] || [ "${NGROK_REMOTE_URL}" = "null" ]; then
  86. echo "ERROR: ngrok doesn't seem to return a valid URL (${NGROK_REMOTE_URL})."
  87. if [ "$attempt" -lt "$retries" ]; then
  88. echo "Retrying..."
  89. sleep 5 # Wait before retrying
  90. continue
  91. else
  92. echo "Exceeded maximum retry attempts. Exiting..."
  93. exit 1
  94. fi
  95. fi
  96.  
  97. # Trim double quotes from variable
  98. NGROK_REMOTE_URL="${NGROK_REMOTE_URL//\"}"
  99.  
  100. # If http protocol is returned, replace with https
  101. NGROK_REMOTE_URL=${NGROK_REMOTE_URL/http:\/\//https:\/\/}
  102.  
  103. echo "ngrok started successfully!"
  104. echo "Your ngrok URL is: $NGROK_REMOTE_URL"
  105. send_message "ngrok started successfully! Your ngrok URL is: $NGROK_REMOTE_URL"
  106. break # Exit the loop if ngrok starts successfully
  107. done
  108. }
  109.  
  110. # Function to start MFBot
  111. start_mfbot() {
  112. log "Checking and starting MFBot_Konsole_ARM64 and MainProgram.py."
  113.  
  114. if [ -e "$lock_file" ]; then
  115. old_pid=$(cat "$lock_file")
  116.  
  117. if ps -p "$old_pid" > /dev/null; then
  118. log "Error: Lock file $lock_file exists. Another instance is already running with PID $old_pid. Continuing anyway."
  119. send_message "Warning: Another instance of MFBot is already running. Continuing anyway."
  120. else
  121. log "Warning: Stale lock file detected. The previous process with PID $old_pid is not running. Removing the lock file."
  122. rm "$lock_file"
  123. fi
  124. else
  125. touch "$lock_file"
  126. fi
  127.  
  128. # Start MFBot_Konsole_ARM64
  129. if ! pgrep -f "MFBot_Konsole_ARM64" > /dev/null; then
  130. log "Starting MFBot_Konsole_ARM64..."
  131. cd /home/mfbot && ./MFBot_Konsole_ARM64 &
  132. sleep 10
  133. else
  134. log "MFBot_Konsole_ARM64 is already running."
  135. send_message "MFBot_Konsole_ARM64 is already running."
  136. fi
  137.  
  138. # Start MainProgram.py
  139. if ! pgrep -f "python3.6" > /dev/null; then
  140. log "Starting MainProgram.py..."
  141. python3.6 /home/mfbot/mfbot-webinterface/MainProgram.py -a http://127.0.0.1:4711/ --remoteU=root --remoteP=orangepi --webU=mfbot --webP=mfbot &
  142. sleep 10
  143. else
  144. log "python3.6 is already running."
  145. send_message "python3.6 is already running."
  146. fi
  147.  
  148.  
  149. log "MFBot_Konsole_ARM64 and MainProgram.py started successfully."
  150. send_message "MFBot_Konsole_ARM64 and MainProgram.py started successfully."
  151.  
  152. # Set the flag to indicate that MFBot has started
  153. mfbot_started=true
  154. }
  155.  
  156. # Function to check internet connectivity and perform actions
  157. check_and_handle_internet() {
  158. if $mfbot_started; then
  159. # Check internet only if MFBot has started
  160. if check_internet; then
  161. log "Internet is available."
  162.  
  163. # Kill MFBot, Ngrok, and Python3.6
  164. killall MFBot_Konsole_ARM64 ngrok python3.6
  165. cd /home/mfbot && rm "$lock_file"
  166.  
  167. # Give some time for components to restart
  168. sleep 30
  169.  
  170. # Start ngrok again on port 8050
  171. start_ngrok
  172.  
  173. # Reset the flag after processing
  174. mfbot_started=false
  175. else
  176. log "Internet is not available. Waiting for reconnection..."
  177. send_message "Internet is not available. Waiting for reconnection..."
  178. fi
  179. fi
  180. }
  181.  
  182. # Function to check the status of a component
  183. check_component_status() {
  184. component_name="$1"
  185.  
  186. if [ "$component_name" = "mf_web" ]; then
  187. if nc -z localhost 8050; then
  188. echo "up"
  189. else
  190. echo "down"
  191. fi
  192. else
  193. if pgrep -x "MFBot_Konsole_ARM64" > /dev/null; then
  194. echo "up"
  195. else
  196. echo "down"
  197. fi
  198. fi
  199. }
  200.  
  201. # Function to retrieve a random fun fact
  202. get_random_fun_fact() {
  203. # API endpoint for a random fun fact
  204. api_endpoint="https://uselessfacts.jsph.pl/api/v2/facts/random"
  205.  
  206. # Use curl to fetch the data
  207. api_response=$(curl -s "$api_endpoint")
  208.  
  209. # Extract the text from the response
  210. fun_fact_text=$(echo "$api_response" | jq -r '.text // empty')
  211.  
  212. if [ -n "$fun_fact_text" ]; then
  213. echo "$fun_fact_text"
  214. else
  215. echo "Failed to retrieve a fun fact."
  216. fi
  217. }
  218.  
  219. # Process Telegram commands
  220. while true; do
  221. # Retrieve Telegram updates
  222. updates=$(curl -s "https://api.telegram.org/bot$BOT_TOKEN/getUpdates?offset=$((LAST_PROCESSED_ID + 1))")
  223.  
  224. # Check if updates is not empty
  225. if [ -n "$updates" ]; then
  226. # Extract relevant information from updates
  227. for update in $(echo "$updates" | jq -c '.result[] // empty'); do
  228. update_id=$(echo "$update" | jq -r '.update_id // empty')
  229.  
  230. # Check if the update has been processed
  231. if grep -q "$update_id" "$processed_updates_file"; then
  232. continue # Skip processing this update
  233. fi
  234.  
  235. message=$(echo "$update" | jq -r '.message.text // empty')
  236. message_timestamp=$(echo "$update" | jq -r '.message.date // empty')
  237.  
  238. # Ignore messages received before the script start time or null values
  239. if [ -n "$message_timestamp" ] && [ "$message_timestamp" -lt "$SCRIPT_START_TIME" ]; then
  240. continue
  241. fi
  242.  
  243. # Ignore old messages
  244. if [ "$message_timestamp" -lt "$LAST_PROCESSED_TIMESTAMP" ]; then
  245. continue
  246. fi
  247.  
  248. if [ -n "$message" ]; then
  249. if [[ "$message" == "/"* ]]; then
  250. case "$message" in
  251. "/start")
  252. if [ "$mfbot_started" = true ]; then
  253. send_message "MFBot is already started. If you want to reset, use /softreset."
  254. else
  255. send_message "Starting, please wait. This is going to take a while..."
  256.  
  257. fun_fact=$(get_random_fun_fact)
  258. send_message "Here's a fun fact while you wait:\n$fun_fact"
  259.  
  260. # Set the flag to indicate that MFBot has started
  261. mfbot_started=true
  262.  
  263. # Start autostart.sh
  264. start_mfbot
  265.  
  266. # Start ngrok and send the URL
  267. start_ngrok
  268. fi
  269. ;;
  270. "/status")
  271. # Check the status of MFBot_Konsole_ARM64
  272. mfbot_konsole_status=$(check_component_status "MFBot_Konsole_ARM64")
  273.  
  274. # Check the status of ngrok
  275. ngrok_status=$(check_component_status "ngrok")
  276.  
  277. # Check the status of mf_web (port 8050)
  278. mf_web_status=$(check_component_status 8050)
  279.  
  280. send_message "MFBot_Konsole_ARM64: $mfbot_konsole_status\nngrok: $ngrok_status\nmf_web: $mf_web_status"
  281. ;;
  282. "/temp")
  283. # Get current temperature
  284. cpu_temperature=$(sensors | awk '/temp1/ {print $2; exit}')
  285.  
  286. if [ -n "$cpu_temperature" ]; then
  287. temperature_message="CPU Temperature: $cpu_temperature"
  288. else
  289. temperature_message="Failed to retrieve CPU temperature."
  290. fi
  291.  
  292. # Send the temperature as a reply
  293. send_message "$temperature_message"
  294. ;;
  295. "/stop")
  296. # Stop MFBot, Ngrok, and Python3.6
  297. send_message "Stopping MFBot, Ngrok, and Python3.6..."
  298. killall MFBot_Konsole_ARM64 ngrok python3.6
  299. cd /home/mfbot && rm "$lock_file"
  300.  
  301. # Set the flag to indicate that MFBot has started
  302. mfbot_started=false
  303.  
  304. send_message "MFBot, Ngrok, and Python3.6 stopped."
  305. ;;
  306. "/softreset")
  307. # Soft reset (kill MFBot, Ngrok, and Python3.6, restart using autostart.sh)
  308. send_message "Soft reset in progress. This is going to take a while..."
  309.  
  310. fun_fact=$(get_random_fun_fact)
  311. send_message "Here's a fun fact while you wait:\n$fun_fact"
  312.  
  313. killall MFBot_Konsole_ARM64 ngrok python3.6
  314. cd /home/mfbot && rm "$lock_file"
  315.  
  316. start_mfbot
  317. start_ngrok
  318. ;;
  319. "/url")
  320. # Get the last sent ngrok URL
  321. send_message "Your ngrok current URL is: $NGROK_REMOTE_URL"
  322. ;;
  323. "/fun_fact")
  324. # Get and send a random fun fact
  325. fun_fact=$(get_random_fun_fact)
  326. send_message "Here's a fun fact for you:\n$fun_fact"
  327. ;;
  328. "/help")
  329. # Display help message
  330. 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"
  331. ;;
  332. *)
  333. send_message "Unknown command: $message"
  334. ;;
  335. esac
  336. fi
  337. fi
  338.  
  339. # Update the last processed message timestamp
  340. LAST_PROCESSED_TIMESTAMP=$message_timestamp
  341.  
  342. # Update the last processed message ID
  343. LAST_PROCESSED_ID=$update_id
  344.  
  345. # Store the processed update ID
  346. echo "$update_id" >> "$processed_updates_file"
  347. done
  348. fi
  349.  
  350. # Sleep for a while before checking for updates again
  351. sleep 1
  352. done
  353.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement