diff options
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Dockerfile | 14 | ||||
| -rw-r--r-- | FunnyPineappleBot.sample.service | 18 | ||||
| -rw-r--r-- | config.py | 1 | ||||
| -rw-r--r-- | docker-compose.yaml | 10 | ||||
| -rw-r--r-- | main.py | 20 | ||||
| -rw-r--r-- | requirements.txt (renamed from dependencies) | 0 | ||||
| -rw-r--r-- | samples.txt | 0 | ||||
| -rw-r--r-- | setup.py | 243 | ||||
| -rw-r--r-- | shared/config.sample.py | 3 | ||||
| -rw-r--r-- | shared/instances.py | 3 |
11 files changed, 29 insertions, 288 deletions
| @@ -133,6 +133,5 @@ dmypy.json | |||
| 133 | .vscode | 133 | .vscode |
| 134 | 134 | ||
| 135 | FunnyPineappleBot.service | 135 | FunnyPineappleBot.service |
| 136 | config.py | 136 | tmp/* |
| 137 | samples.txt | 137 | data/* |
| 138 | tmp/* \ No newline at end of file | ||
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f66d997 --- /dev/null +++ b/Dockerfile | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | FROM python:bullseye | ||
| 2 | |||
| 3 | RUN apt-get update | ||
| 4 | RUN apt-get upgrade | ||
| 5 | RUN pip install pip -U | ||
| 6 | |||
| 7 | WORKDIR /app | ||
| 8 | COPY requirements.txt . | ||
| 9 | RUN pip install -r requirements.txt | ||
| 10 | |||
| 11 | COPY . . | ||
| 12 | COPY data/config.py . | ||
| 13 | |||
| 14 | CMD ["python", "main.py"] | ||
diff --git a/FunnyPineappleBot.sample.service b/FunnyPineappleBot.sample.service deleted file mode 100644 index 210b267..0000000 --- a/FunnyPineappleBot.sample.service +++ /dev/null | |||
| @@ -1,18 +0,0 @@ | |||
| 1 | [Unit] | ||
| 2 | Description=Telegram bot from FunnyPineappleChat | ||
| 3 | After=syslog.target | ||
| 4 | After=network.target | ||
| 5 | |||
| 6 | [Service] | ||
| 7 | Type=simple | ||
| 8 | User={username} | ||
| 9 | WorkingDirectory={path} | ||
| 10 | ExecStart={py_path} main.py -m | ||
| 11 | KillMode=control-group | ||
| 12 | KillSignal=15 | ||
| 13 | SendSIGKILL={send_kill} | ||
| 14 | RestartSec=5 | ||
| 15 | Restart=always | ||
| 16 | |||
| 17 | [Install] | ||
| 18 | WantedBy=multi-user.target \ No newline at end of file | ||
diff --git a/config.py b/config.py new file mode 100644 index 0000000..5608add --- /dev/null +++ b/config.py | |||
| @@ -0,0 +1 @@ | |||
| token="{}" | |||
diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..7383133 --- /dev/null +++ b/docker-compose.yaml | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | version: "3" | ||
| 2 | |||
| 3 | services: | ||
| 4 | pineapple-bot: | ||
| 5 | build: "." | ||
| 6 | restart: "always" | ||
| 7 | volumes: | ||
| 8 | - "./requirements.txt:/app/requirements.txt" | ||
| 9 | - "./data/samples.txt:/app/samples.txt" | ||
| 10 | |||
| @@ -2,29 +2,9 @@ import optparse | |||
| 2 | 2 | ||
| 3 | from aiogram import executor, types as t, Dispatcher | 3 | from aiogram import executor, types as t, Dispatcher |
| 4 | import logging | 4 | import logging |
| 5 | from shared import config | ||
| 6 | 5 | ||
| 7 | logging.basicConfig(level=logging.INFO) | 6 | logging.basicConfig(level=logging.INFO) |
| 8 | 7 | ||
| 9 | parser = optparse.OptionParser(conflict_handler="resolve") # Делает прикол с аргументами | ||
| 10 | parser.add_option('-t', '--test', | ||
| 11 | action="store_true", | ||
| 12 | dest='test', | ||
| 13 | help='test token') | ||
| 14 | parser.add_option('-m', '--main', | ||
| 15 | action="store_true", | ||
| 16 | dest='main', | ||
| 17 | help='main token') | ||
| 18 | values, args = parser.parse_args() # Либа хуйня | ||
| 19 | |||
| 20 | if values.test: | ||
| 21 | config.token = config.test_token | ||
| 22 | elif values.main: | ||
| 23 | config.token = config.main_token | ||
| 24 | else: | ||
| 25 | config.token = config.test_token | ||
| 26 | |||
| 27 | |||
| 28 | async def on_start(dp: Dispatcher): | 8 | async def on_start(dp: Dispatcher): |
| 29 | from shared.commands import commands | 9 | from shared.commands import commands |
| 30 | for scope, cmd in commands.items(): | 10 | for scope, cmd in commands.items(): |
diff --git a/dependencies b/requirements.txt index 888ef8a..888ef8a 100644 --- a/dependencies +++ b/requirements.txt | |||
diff --git a/samples.txt b/samples.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/samples.txt | |||
diff --git a/setup.py b/setup.py deleted file mode 100644 index b6fdfc1..0000000 --- a/setup.py +++ /dev/null | |||
| @@ -1,243 +0,0 @@ | |||
| 1 | import os | ||
| 2 | import platform | ||
| 3 | import shutil | ||
| 4 | import sys | ||
| 5 | import traceback | ||
| 6 | import typing as p | ||
| 7 | |||
| 8 | term = shutil.get_terminal_size() | ||
| 9 | sep = "=" * term.columns | ||
| 10 | |||
| 11 | venv = False | ||
| 12 | default = 1 | ||
| 13 | |||
| 14 | if os.name == "nt": | ||
| 15 | print(f"Setup not supported on {platform.system()}") | ||
| 16 | exit() | ||
| 17 | |||
| 18 | |||
| 19 | def _opt_selector(options: p.Dict[str, p.Any], default: p.Optional[int] = None, pmt: str = "") -> p.Tuple[int, p.Any]: | ||
| 20 | _options = {} | ||
| 21 | print("Choose variant") | ||
| 22 | print(sep) | ||
| 23 | |||
| 24 | num = 1 | ||
| 25 | for opt in options: | ||
| 26 | if options[opt] is None: | ||
| 27 | print() | ||
| 28 | continue | ||
| 29 | |||
| 30 | d = "*" if default == num else " " | ||
| 31 | |||
| 32 | print(f" {d}{num} - {opt}") | ||
| 33 | _options[num] = options[opt] | ||
| 34 | num += 1 | ||
| 35 | print(sep) | ||
| 36 | |||
| 37 | while True: | ||
| 38 | opt = input(f"{pmt}-> ") | ||
| 39 | if opt.isdigit(): | ||
| 40 | opt = int(opt) | ||
| 41 | if opt in _options: | ||
| 42 | return opt, _options[opt] | ||
| 43 | if opt == "" and default is not None: | ||
| 44 | return default, _options[default] | ||
| 45 | |||
| 46 | print("Incorrect answer") | ||
| 47 | pmt = "" | ||
| 48 | |||
| 49 | |||
| 50 | def _yes(default: bool = True) -> bool: | ||
| 51 | while True: | ||
| 52 | if default: | ||
| 53 | it = "[Y|n]" | ||
| 54 | else: | ||
| 55 | it = "[y|N]" | ||
| 56 | |||
| 57 | i = input(f"{it} -> ") | ||
| 58 | if i.lower() in ["y", "n"]: | ||
| 59 | return i.lower() == "y" | ||
| 60 | if i == "": | ||
| 61 | return default | ||
| 62 | |||
| 63 | print("Incorrect answer") | ||
| 64 | |||
| 65 | |||
| 66 | def _input(prompt: str, default: str = None, required: bool = True) -> str: | ||
| 67 | if default: | ||
| 68 | print(f"Default {default}") | ||
| 69 | while True: | ||
| 70 | result = input(f"{prompt} -> ") | ||
| 71 | if not result: | ||
| 72 | if default: | ||
| 73 | result = default | ||
| 74 | break | ||
| 75 | elif required: | ||
| 76 | print("Parameter required") | ||
| 77 | else: | ||
| 78 | break | ||
| 79 | |||
| 80 | return result | ||
| 81 | |||
| 82 | |||
| 83 | def _cmd(cmd: str, show_cmd: bool = True) -> bool: | ||
| 84 | if show_cmd: | ||
| 85 | input(f"Press enter for execute {cmd}") | ||
| 86 | result = os.system(cmd) | ||
| 87 | return True if result == 0 else False | ||
| 88 | |||
| 89 | |||
| 90 | def _clear(): | ||
| 91 | print() | ||
| 92 | print('\033c', end="") | ||
| 93 | |||
| 94 | |||
| 95 | def _enter(): | ||
| 96 | input("Press enter, to continue ...") | ||
| 97 | |||
| 98 | |||
| 99 | def create_samples_txt(): | ||
| 100 | if not os.path.isfile("samples.txt"): | ||
| 101 | with open("samples.txt", "w"): | ||
| 102 | print("File created") | ||
| 103 | else: | ||
| 104 | print("File exists") | ||
| 105 | _enter() | ||
| 106 | return True | ||
| 107 | |||
| 108 | |||
| 109 | def systemd_unit_generator(): | ||
| 110 | while True: | ||
| 111 | username = _input("Your username", os.environ["USER"]) | ||
| 112 | path = _input("Path to root of project", os.path.dirname(os.path.abspath(__file__))) | ||
| 113 | py_path = _input("Path to python 3.9", sys.executable) | ||
| 114 | |||
| 115 | print("Send kill (if SIGTERM Timeout)") | ||
| 116 | send_kill = "off" | ||
| 117 | if _yes(True): | ||
| 118 | send_kill = "on" | ||
| 119 | |||
| 120 | with open("FunnyPineappleBot.sample.service", "r") as file: | ||
| 121 | sample = file.read().format(username=username, path=path, py_path=py_path, send_kill=send_kill) | ||
| 122 | |||
| 123 | _clear() | ||
| 124 | print( | ||
| 125 | f"{sep}", | ||
| 126 | f"{sample}", | ||
| 127 | f"{sep}", | ||
| 128 | f"Your username - {username}", | ||
| 129 | f"Path to root of project - {path}", | ||
| 130 | f"Path to python 3.9 - {py_path}", | ||
| 131 | f"Send kill - {send_kill}", | ||
| 132 | f"{sep}", | ||
| 133 | "", | ||
| 134 | "All correct ?", | ||
| 135 | sep="\n" | ||
| 136 | ) | ||
| 137 | if _yes(False): | ||
| 138 | with open("FunnyPineappleBot.service", "w") as file: | ||
| 139 | file.write(sample) | ||
| 140 | _clear() | ||
| 141 | |||
| 142 | print("Link unit from /etc/systemd/system/ ?") | ||
| 143 | if _yes(): | ||
| 144 | _cmd("sudo mv FunnyPineappleBot.service /etc/systemd/system/") | ||
| 145 | _cmd("sudo ln /etc/systemd/system/FunnyPineappleBot.service ./ -s") | ||
| 146 | break | ||
| 147 | else: | ||
| 148 | _clear() | ||
| 149 | print(sep) | ||
| 150 | _enter() | ||
| 151 | return True | ||
| 152 | |||
| 153 | |||
| 154 | def config_generator(): | ||
| 155 | try: | ||
| 156 | import config | ||
| 157 | except ImportError: | ||
| 158 | config = object() | ||
| 159 | while True: | ||
| 160 | main_token = _input("Main bot token", getattr(config, "main_token", None)) | ||
| 161 | test_token = _input("Test bot token", getattr(config, "test_token", None)) | ||
| 162 | |||
| 163 | with open("shared/config.sample.py", "r") as file: | ||
| 164 | sample = file.read().format(main_token=main_token, test_token=test_token) | ||
| 165 | |||
| 166 | print( | ||
| 167 | f"{sep}", | ||
| 168 | f"{sample}", | ||
| 169 | f"{sep}", | ||
| 170 | "All correct ?", | ||
| 171 | sep="\n" | ||
| 172 | ) | ||
| 173 | |||
| 174 | if _yes(False): | ||
| 175 | with open("shared/config.py", "w") as file: | ||
| 176 | file.write(sample) | ||
| 177 | break | ||
| 178 | else: | ||
| 179 | _clear() | ||
| 180 | return True | ||
| 181 | |||
| 182 | |||
| 183 | def install_dependencies(): | ||
| 184 | with open("dependencies", "r") as file: | ||
| 185 | dependencies = file.read() | ||
| 186 | print( | ||
| 187 | "Install this ?", | ||
| 188 | f"{sep}", | ||
| 189 | f"{dependencies}", | ||
| 190 | f"{sep}", | ||
| 191 | sep="\n" | ||
| 192 | ) | ||
| 193 | _cmd("pip install -U -r dependencies", False) | ||
| 194 | print(f"{sep}\nSuccessfully installed") | ||
| 195 | _enter() | ||
| 196 | return True | ||
| 197 | |||
| 198 | |||
| 199 | if __name__ == '__main__': | ||
| 200 | opts = { | ||
| 201 | "Create samples.txt file": create_samples_txt, | ||
| 202 | "Setup systemd unit": systemd_unit_generator, | ||
| 203 | "Setup config.py": config_generator, | ||
| 204 | "Install or Update dependencies": install_dependencies, | ||
| 205 | "Exit": exit, | ||
| 206 | } | ||
| 207 | exit_index = list(opts.keys()).index("Exit") + 1 | ||
| 208 | default = 1 | ||
| 209 | pmt = "" | ||
| 210 | |||
| 211 | _clear() | ||
| 212 | while True: | ||
| 213 | if default >= exit_index: | ||
| 214 | default = exit_index | ||
| 215 | term = shutil.get_terminal_size() | ||
| 216 | sep = "=" * term.columns | ||
| 217 | try: | ||
| 218 | _clear() | ||
| 219 | num, opt = _opt_selector(opts, default, pmt) | ||
| 220 | _clear() | ||
| 221 | res = opt() | ||
| 222 | _clear() | ||
| 223 | |||
| 224 | if num != default: | ||
| 225 | default = num + 1 | ||
| 226 | elif res is True: | ||
| 227 | default += 1 | ||
| 228 | |||
| 229 | if res is not True: | ||
| 230 | pmt = f"{res!r} " | ||
| 231 | if num == default: | ||
| 232 | default = num + 1 | ||
| 233 | else: | ||
| 234 | pmt = "" | ||
| 235 | |||
| 236 | except KeyboardInterrupt: | ||
| 237 | break | ||
| 238 | except Exception as e: | ||
| 239 | pmt = f"An error has occurred ({e.__class__.__name__}:{e.args[0]})" | ||
| 240 | trc = traceback.format_exc() | ||
| 241 | print(trc) | ||
| 242 | _enter() | ||
| 243 | _clear() | ||
diff --git a/shared/config.sample.py b/shared/config.sample.py deleted file mode 100644 index f4a3f09..0000000 --- a/shared/config.sample.py +++ /dev/null | |||
| @@ -1,3 +0,0 @@ | |||
| 1 | TOKEN = "" | ||
| 2 | test_token = "{test_token}" | ||
| 3 | main_token = "{main_token}" | ||
diff --git a/shared/instances.py b/shared/instances.py index 25de59f..5351f9c 100644 --- a/shared/instances.py +++ b/shared/instances.py | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | from aiogram import Bot, Dispatcher | 1 | from aiogram import Bot, Dispatcher |
| 2 | from .config import token | 2 | from config import token |
| 3 | 3 | ||
| 4 | print(token) | ||
| 4 | bot = Bot(token=token) | 5 | bot = Bot(token=token) |
| 5 | dp = Dispatcher(bot) | 6 | dp = Dispatcher(bot) |
| 6 | gen_chance = 10 | 7 | gen_chance = 10 |
