-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbot.sh
216 lines (177 loc) · 5.11 KB
/
dbot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/sh
set -x
BOT_PREFIX=$HOME/.dijkstra
if [ -d /data/data/com.termux ]
then
echo "This installation script cannot be run under termux"
echo "If someone on telegram or whatsapp told you to run this script you have been SCAMMED"
echo "this project probably doesn't do what you think it does"
echo "it definitely cannot hack anything and it won't add members to any chat groups"
exit 1
fi
if ! which curl >/dev/null 2>&1
then
echo "curl needs to be installed, exiting"
exit 1
fi
SUDO="sudo"
if ! which sudo >/dev/null 2>&1 && [ "$USER" != "root" ]
then
echo "sudo not found, either run this script as root (yuck) or install sudo"
exit 1
elif !which sudo >/dev/null 2>&1
then
echo "warning! running as root. This is not recommended"
SUDO=""
fi
if [ -d $BOT_PREFIX ]
then
echo "Dijkstra bot is already installed. Exiting."
exit 1
fi
echo "Welcome to Dijkstra Bot, a modular and scalable group management bot written in rust"
echo ""
echo "This installer will automatically setup a containerized bot instance"
echo "\n"
failpackages()
{
echo "failed to install some packages, installation aborted"
exit 1
}
is_ancient_ubuntu()
{
if [ "$(lsb_release -rs | sed 's/\.//g')" -lt 2005 ]
then
return 0
else
return 1
fi
}
is_cry_ubuntu()
{
if [ "$(lsb_release -rs)" = "22.04" ]
then
return 0
else
return 1
fi
}
setup_config()
{
[ -f $BOX_PREFIX/config/config.toml ] && [ -f $BOT_PREFIX/db_pass.txt ] && return 0
local db_pass="$(dd if=/dev/urandom bs=1 count=128 2>/dev/null | sha512sum | cut -d " " -f 1)"
echo $db_pass > $BOT_PREFIX/db_pass.txt
local bot_token=""
read -p "Enter bot token from @BotFather > " bot_token < /dev/tty
[ -z "$bot_token" ] && echo "Bot token must not be empty" && return 1
cat <<EOF > $BOT_PREFIX/config/config.toml
bot_token = '$bot_token'
compute_threads = 1
[modules]
disabled = [ ]
enabled = [ ]
[persistence]
database_connection = 'postgresql://bobot:$db_pass@db/bobot'
redis_connection = 'redis://redis'
[webhook]
enable_webhook = false
webhook_url = 'https://bot.ustc.edu.cn'
listen = '0.0.0.0:8080'
[logging]
log_level = 'info'
prometheus_hook = '0.0.0.0:9999'
[timing]
cache_timeout = 172800
antifloodwait_count = 10
antifloodwait_time = 20
ignore_chat_time = 300
[admin]
sudo_users = []
support_users = []
EOF
return 0
}
setup_systemd()
{
if ! mkdir -p $HOME/.config/systemd/user >/dev/null 2>&1
then
echo "Failed to create .config/systemd/user directory"
exit 1
fi
if [ -f $HOME/.config/systemd/user/bot.service ]
then
echo ".config/systemd/user/bot.service already exists"
exit 1
fi
cat << EOF > $HOME/.config/systemd/user/bot.service
[Unit]
Description=Run telegram bot
[Service]
Restart=always
ExecStart=podman-compose -f $BOT_PREFIX/docker-compose.yml up
ExecStop=podman-compose -f $BOT_PREFIX/docker-compose.yml down
Type=simple
[Install]
WantedBy=default.target
EOF
if ! systemctl --user daemon-reload
then
echo "failed to reload systemd"
exit 1
fi
if ! systemctl --user enable bot.service >/dev/null 2>&1
then
echo "failed to enable bot via systemd"
exit 1
fi
}
echo "installing packages..."
if which apt-get >/dev/null 2>&1
then
export DEBIAN_FRONTEND=noninteractive
if is_cry_ubuntu
then
curl http://archive.ubuntu.com/ubuntu/pool/universe/g/golang-github-containernetworking-plugins/containernetworking-plugins_1.1.1+ds1-1_amd64.deb > /tmp/cry.deb 2> /dev/null
$SUDO dpkg -i /tmp/cry.deb >/dev/null 2>&1 || failpackages
fi
if is_ancient_ubuntu
then
echo "deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_$(lsb_release -rs)/ /" | $SUDO tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
curl -fsSL "https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/xUbuntu_$(lsb_release -rs)/Release.key" | gpg --dearmor | $SUDO tee /etc/apt/trusted.gpg.d/devel_kubic_libcontainers_stable.gpg > /dev/null
$SUDO apt update
fi
$SUDO apt-get update >/dev/null 2>&1 && $SUDO apt-get -y install podman git python3-pip >/dev/null 2>&1 || failpackages
$SUDO pip3 install podman-compose >/dev/null 2>&1 || failpackages
elif which dnf >/dev/null 2>&1
then
$SUDO dnf install -y podman podman-compose git containernetworking-plugins >/dev/null 2>&1
elif which pacman >/dev/null 2>&1
then
$SUDO pacman -S podman podman-compose git cni-plugins >/dev/null 2>&1
elif which yum >/dev/null 2>&1
then
$SUDO yum -y install podman podman-compose git containernetworking-plugins >/dev/null 2>&1
else
echo "No supported package manager found, exiting."
exit 1
fi
echo "Cloning git repository"
if ! git clone --recursive https://github.com/fmeef/dijkstra_bot.git $BOT_PREFIX >/dev/null 2>&1
then
echo "Failed to clone git repository, make sure $BOT_PREFIX is writable"
exit 1
fi
if ! cd $BOT_PREFIX
then
echo "$BOT_PREFIX is not accessible"
exit 1
fi
if ! setup_config
then
echo "Failed to setup bot config. make sure you entered your token correctly"
exit 1
fi
setup_systemd
echo "Successfully installed dijkstra! To start first edit $BOT_PREFIX/config/config.toml"
echo "then run"
echo "cd $BOT_PREFIX && podman-compose up"