installer and systemd service added

This commit is contained in:
Hash Borgir 2024-02-13 16:39:09 -07:00
parent 8c23deb13b
commit 976f806397
6 changed files with 173 additions and 52 deletions

5
.gitignore vendored
View File

@ -6,4 +6,7 @@ session.txt
socks5.txt
venv/
plugins/__pycache__/
simplematrixbotlib/
simplematrixbotlib*/
chromedriver
store
funguybot.service

View File

@ -1,17 +1,17 @@
From 4aa5b913f75e32f6ed06fc6e691daa856824b26a Mon Sep 17 00:00:00 2001
From 7b3421cf893ef8ea36978ae1343f7c8d5d353412 Mon Sep 17 00:00:00 2001
From: Hash Borgir <atirjavid@gmail.com>
Date: Mon, 12 Feb 2024 19:12:05 -0700
Subject: [PATCH] Fixed stability issue in api.py
Date: Tue, 13 Feb 2024 15:48:35 -0700
Subject: [PATCH] api.py patch
---
simplematrixbotlib/api.py | 2 ++
1 file changed, 2 insertions(+)
diff --git a/simplematrixbotlib/api.py b/simplematrixbotlib/api.py
index 1d87b3d..13a78cf 100644
index 6d51b38..3af7e7e 100644
--- a/simplematrixbotlib/api.py
+++ b/simplematrixbotlib/api.py
@@ -295,6 +295,7 @@ class Api:
@@ -347,6 +347,7 @@ class Api:
pass # Successful upload
else:
print(f"Failed Upload Response: {resp}")
@ -19,7 +19,7 @@ index 1d87b3d..13a78cf 100644
content = {
"body": os.path.basename(image_filepath),
@@ -342,6 +343,7 @@ class Api:
@@ -394,6 +395,7 @@ class Api:
pass # Successful upload
else:
print(f"Failed Upload Response: {resp}")

145
install-funguy.sh Executable file
View File

@ -0,0 +1,145 @@
#!/bin/bash
# Function to handle errors
handle_error() {
echo "Error: $1"
exit 1
}
# Function to handle sudo errors
handle_sudo_error() {
echo -e "\e[31mError: Failed to execute sudo command: $1\e[0m"
exit 1
}
# Function to execute sudo commands with error handling
sudo_execute() {
echo -e "\e[31m$ sudo $@\e[0m"
sudo "$@" || handle_sudo_error "$*"
}
# Function to create .env file with provided content
create_env() {
echo -e "MATRIX_URL = \"$1\"\nMATRIX_USER = \"$2\"\nMATRIX_PASS = \"$3\"" > .env || handle_error "Failed to create .env file"
}
# Function to prompt user for input
prompt_user() {
read -p "$1: " input
echo "$input"
}
# Store current directory
current_dir=$(pwd)
# Setup python virtual environment
python3 -m venv venv
source venv/bin/activate
# Check if simplematrixbotlib directory already exists
if [ -d "simplematrixbotlib" ] && [ "$(ls -A simplematrixbotlib)" ]; then
echo "simplematrixbotlib directory already exists."
else
# Clone the simplematrixbotlib repository
git clone https://codeberg.org/imbev/simplematrixbotlib.git || handle_error "Failed to clone simplematrixbotlib repository"
fi
# Copy the patch file to the simplematrixbotlib directory
cp api.py.patch "$current_dir/simplematrixbotlib/" || handle_error "Failed to copy patch file"
# Change directory to simplematrixbotlib
cd "$current_dir/simplematrixbotlib/" || handle_error "Failed to change directory to simplematrixbotlib"
# Apply the patch
git apply api.py.patch || handle_error "Failed to apply patch"
# Install simplematrixbotlib
pip install . || handle_error "Failed to install simplematrixbotlib"
# Change back to the original directory
cd "$current_dir"
# Install requirements
pip install -r requirements.txt || handle_error "Failed to install requirements"
# Prompt for Matrix homeserver
read -p "Do you want to use the default homeserver https://matrix.org? (Y/n): " use_default_hs
if [[ $use_default_hs =~ ^[Nn]$ ]]; then
homeserver=$(prompt_user "Enter Matrix homeserver URL (e.g., example.com): ")
homeserver="https://$homeserver"
else
homeserver="https://matrix.org"
fi
# Prompt for username
username=$(prompt_user "Enter Matrix username: ")
# Prompt for password
password=$(prompt_user "Enter Matrix password: ")
# Create .env file
create_env "$homeserver" "$username" "$password"
# Print ASCII art
cat << "EOF"
_____ ____ _
| ___| _ _ __ __ _ _ _ _ _ | __ ) ___ | |_
| |_ | | | | '_ \ / _\` | | | | | | | | _ \ / _ \| __|
| _|| |_| | | | | (_| | |_| | |_| | | |_) | (_) | |_
|_| \__,_|_| |_|\__, |\__,_|\__, | |____/ \___/ \__|
|___/ |___/
By HB (@hashborgir@mozilla.org)
--------------------------------------------------------
EOF
# Echo setup completion message
echo "Setting up funguy bot..."
echo "Setting up systemd service..."
working_directory=$current_dir
# Prompt for user and group
user=$(prompt_user "Enter user")
same_group=$(prompt_user "Is group same as user? (Y/n): ")
if [[ $same_group =~ ^[Yy]$ ]]; then
group=$user
else
group=$(prompt_user "Enter group")
fi
# Create systemd service file
cat <<EOF > funguybot.service
[Unit]
Description=Funguy Bot Service
After=network.target
[Service]
Type=simple
User=$user
Group=$group
WorkingDirectory=$working_directory
ExecStart=$working_directory/start-funguy.sh
Restart=on-failure
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=funguybot
[Install]
WantedBy=multi-user.target
EOF
# Copy systemd service file
sudo_execute cp funguybot.service /etc/systemd/system/ || handle_sudo_error "Failed to copy systemd service file"
# Enable and start the service
sudo_execute systemctl daemon-reload || handle_sudo_error "Failed to reload systemd daemon"
sudo_execute systemctl enable funguybot || handle_sudo_error "Failed to enable funguybot service"
sudo_execute systemctl start funguybot || handle_sudo_error "Failed to start funguybot service"
echo "Funguy bot has been successfully installed and the service has been enabled."
echo "Before starting the service, make sure to edit the .env file and add your bot account's homeserver URL, username, and password."
echo ""
echo "Once done, start the service by running the following command:"
echo ""
echo ""
echo "sudo systemctl start funguybot"

View File

@ -1,45 +0,0 @@
#!/bin/bash
# Store current directory
current_dir=$(pwd)
# Load venv
source venv/bin/activate
# Clone the simplematrixbotlib repository
git clone https://codeberg.org/imbev/simplematrixbotlib.git
# Change directory to simplematrixbotlib
cd $current_dir/simplematrixbotlib/
# Install simplematrixbotlib
pip install .
# Change back to the original directory
cd $current_dir
# Install requirements
pip install -r requirements.txt
# Create .env file with required content
echo -e "MATRIX_URL = \"https://matrix.org\"\nMATRIX_USER = \"\"\nMATRIX_PASS = \"\"" > .env
# Print ASCII art
echo " _____ ____ _ "
echo "| ___| _ _ __ __ _ _ _ _ _ | __ ) ___ | |_ "
echo "| |_ | | | | '_ \ / _\` | | | | | | | | _ \ / _ \| __| "
echo "| _|| |_| | | | | (_| | |_| | |_| | | |_) | (_) | |_ "
echo "|_| \__,_|_| |_|\__, |\__,_|\__, | |____/ \___/ \__| "
echo " |___/ |___/ "
# Echo setup completion message
echo "Bot setup completed."
# Prompt user to modify funguy.py to set ADMIN_USER
echo "Modify .env file and set your credentials and homeserver"
echo "Please open funguy.py and set the ADMIN_USER variable to your admin username."
# Launch the bot
echo "Launch the bot with 'python funguy.py'"

View File

@ -2,3 +2,4 @@ python-dotenv
requests
pytube
duckduckgo_search
nio

17
start-funguy.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
# Function to handle errors
handle_error() {
echo "Error: $1"
exit 1
}
# Load Python virtual environment
source venv/bin/activate || { echo "Error: Failed to load Python virtual environment."; exit 1; }
# Get python path
python=$(which python)
# Run funguy.py
$python funguy.py || { echo "Error: Failed to execute funguy.py."; exit 1; }