Async, locale support, kill function changed

This commit is contained in:
2022-09-15 11:07:39 +02:00
parent bd47790b0d
commit a238c31f8a
6 changed files with 90 additions and 48 deletions

75
bot.py
View File

@@ -1,14 +1,14 @@
# -*- coding: utf-8 -*-
import subprocess
from sys import exit
from os import getpid, listdir, sep, system
from os import getpid, listdir, sep
from modules.functions import *
from modules.functions_bot import *
from pyrogram import Client, filters, idle
from pyrogram.types import ForceReply, BotCommand, BotCommandScopeChat
from pyrogram.enums.chat_action import ChatAction
if configGet("bot_token") != "12345678:asdfghjklzxcvbnm":
pid = getpid()
app = Client("auto_zoom_public_bot", api_id=configGet("api_id"), api_hash=configGet("api_hash"), bot_token=configGet("bot_token"))
@@ -16,17 +16,13 @@ else:
logWrite("Could not start the bot. Please, configure token in config.json")
exit()
def botSend(userid, message="Test message"):
app = Client("auto_zoom_public_bot")
app.send_message(userid, message)
@app.on_message(~ filters.scheduled & filters.command(["link", "start"], prefixes="/"))
def start(app, msg):
async def start(app, msg):
logWrite(f'Got command start/link from {msg.from_user.id}')
app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
user_locale = msg.from_user.language_code
@@ -35,22 +31,22 @@ def start(app, msg):
jsonSave( f"data{sep}users{sep}{msg.from_user.id}.json", {"api_key": None, "linked": False, "context": {"action": None, "data": None}} )
if not userGet(msg.from_user.id, "linked"):
msg.reply_text(locale("link_input", "msg"), reply_markup=ForceReply(placeholder=locale("link", "fry")))
await msg.reply_text(locale("link_input", "msg", locale=user_locale), reply_markup=ForceReply(placeholder=locale("link", "fry", locale=user_locale)))
userSet(msg.chat.id, "context", "link_key")
else:
msg.reply_text(locale("already_linked", "msg"))
await msg.reply_text(locale("already_linked", "msg", locale=user_locale))
@app.on_message(~ filters.scheduled & filters.command(["unlink"], prefixes="/"))
def unlink(app, msg):
async def unlink(app, msg):
logWrite(f'Got command ulink from {msg.from_user.id}')
app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
user_locale = msg.from_user.language_code
if not userGet(msg.from_user.id, "linked"):
msg.reply_text(locale("not_linked", "msg"))
await msg.reply_text(locale("not_linked", "msg", locale=user_locale))
else:
try:
keys_storage = jsonLoad(f"data{sep}keys_storage.json")
@@ -60,40 +56,40 @@ def unlink(app, msg):
pass
userClear(msg.from_user.id, "api_key")
userSet(msg.chat.id, "linked", False)
msg.reply_text(locale("unlinked", "msg"))
await msg.reply_text(locale("unlinked", "msg", locale=user_locale))
@app.on_message(~ filters.scheduled & filters.command(["cancel"], prefixes="/"))
def cancel(app, msg):
async def cancel(app, msg):
app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
user_locale = msg.from_user.language_code
if userGet(msg.from_user.id, "context") is not None:
userClear(msg.from_user.id, "context")
userClear(msg.from_user.id, "context_content")
msg.reply_text(locale("cancel", "msg"))
await msg.reply_text(locale("cancel", "msg", locale=user_locale))
else:
msg.reply_text(locale("cancel_empty", "msg"))
await msg.reply_text(locale("cancel_empty", "msg", locale=user_locale))
@app.on_message(filters.command(["kill", "die", "shutdown", "reboot"], prefixes="/"))
def kill(app, msg):
async def kill(app, msg):
if msg.from_user.id == configGet("admin"):
msg.reply_text(f"Shutting down bot with pid `{pid}`")
system('kill -9 '+str(pid)) # REFACTOR
await msg.reply_text(f"Shutting down bot with pid `{pid}`")
killProc(pid)
@app.on_message(~ filters.scheduled)
def any_message_handler(app, msg):
async def any_message_handler(app, msg):
if userGet(msg.from_user.id, "context") == "link_key":
user_locale = msg.from_user.language_code
if msg.text in jsonLoad(configGet("api_keys"))["autozoom"]:
msg.reply_text(locale("key_correct", "msg"))
await msg.reply_text(locale("key_correct", "msg", locale=user_locale))
userSet(msg.from_user.id, "api_key", msg.text)
userSet(msg.from_user.id, "linked", True)
keys_storage = jsonLoad(f"data{sep}keys_storage.json")
@@ -102,7 +98,7 @@ def any_message_handler(app, msg):
logWrite(f"Added apikey {msg.text} for user {msg.from_user.id}")
else:
logWrite(f"User {msg.from_user.id} tried to pair with invalid apikey {msg.text}")
msg.reply_text(locale("key_wrong", "msg"))
await msg.reply_text(locale("key_wrong", "msg", locale=user_locale))
userClear(msg.from_user.id, "context")
userClear(msg.from_user.id, "context_content")
@@ -115,24 +111,29 @@ if __name__ == "__main__":
app.start()
app.send_message(configGet("admin"), f"Starting bot with pid `{pid}`")
all_commands = locale("cmd")
for entry in listdir("locale"):
commands_list = []
for command in all_commands["general"]:
commands_list.append(BotCommand(command, all_commands["general"][command]))
app.set_bot_commands(commands_list)
if entry.endswith(".json"):
# Registering admin commands
commands_admin_list = []
for command in all_commands["general"]:
commands_admin_list.append(BotCommand(command, all_commands["general"][command]))
for command in all_commands["admin"]:
commands_admin_list.append(BotCommand(command, all_commands["admin"][command]))
app.set_bot_commands(commands_admin_list, scope=BotCommandScopeChat(chat_id=configGet("admin")))
all_commands = locale("cmd", locale=entry.replace(".json", ""))
# Registering user commands
commands_list = []
for command in all_commands["general"]:
commands_list.append(BotCommand(command, all_commands["general"][command]))
app.set_bot_commands(commands_list, language_code=entry.replace(".json", ""))
# Registering admin commands
commands_admin_list = []
for command in all_commands["general"]:
commands_admin_list.append(BotCommand(command, all_commands["general"][command]))
for command in all_commands["admin"]:
commands_admin_list.append(BotCommand(command, all_commands["admin"][command]))
app.set_bot_commands(commands_admin_list, language_code=entry.replace(".json", ""), scope=BotCommandScopeChat(chat_id=configGet("admin")))
idle()
app.send_message(configGet("admin"), f"Shutting down bot with pid `{pid}`")
logWrite(f'Shutting down with PID {pid}')
subprocess.call(f'kill -9 {pid}', shell=True)
killProc(pid)