ubot/install

77 lines
1.9 KiB
Bash
Executable File

#!/bin/sh
# Preliminary sanity checks
[ $(id -u) != 0 ] && echo "Please run this installer as root." && exit 1
[ $0 != ./install ] && echo "Not launched from repository root." && exit 1
# Functions
mkconfig(){
echo "Please get your credentials from https://my.telegram.org."
read -p "App API ID (api_id): " api_id
read -p "App API hash (api_hash): " api_hash
read -p "Your user ID: " myid
read -p "Do you wish to use a log chat? (y/n): " logrpl
if [ "$logrpl" = "y" ]
then
read -p "Log chat ID (logchat): " logchat
else
logchat="'me'"
fi
echo "api_id = $api_id" >> config.py
echo "api_hash = '$api_hash'" >> config.py
echo "myid = $myid" >> config.py
echo "logchat = $logchat" >> config.py
}
mkuser(){
# Create user account, distro mess awaits here...
# Debian has both useradd and adduser
if [ -e /sbin/useradd ]
then
useradd -r bot
return 0
fi
# Alpine only has adduser (Busybox)
if [ -e /sbin/adduser ]
then
adduser -S bot
return 0
fi
}
addfiles(){
# Install executable and config
install -dm770 -o root -g bot /var/ubot
install -m640 -o root -g bot config.py /var/ubot/config.py
install -m755 -o root -g root bin/ubot /usr/bin/ubot
# Install service files on installed inits
if [ -e /sbin/openrc ]
then
install -m755 init/openrc /etc/init.d/ubot
ln -s /sbin/openrc /etc/periodic/15min/openrc
elif [ -e /lib/systemd/systemd ]
then
install -m644 init/systemd /etc/systemd/system/ubot.service
fi
}
# Install dependencies
[ $(which apt) ] && apt install python3-pip
[ $(which apk) ] && apk add py3-pip
pip3 install -U telethon
# Generate config if not built yet
[ ! -f config.py ] && mkconfig
# Create a new user
mkuser
# Install files if config is (now) present
[ -f config.py ] && addfiles
# Run Telethon for the first time
echo "Please log in to Telegram now."
echo "Afterwards you can hit Ctrl-C and start the ubot service."
umask 0077
su bot -s /usr/bin/ubot
return 0