Async, locale support, kill function changed
This commit is contained in:
parent
bd47790b0d
commit
a238c31f8a
57
bot.py
57
bot.py
@ -1,14 +1,14 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import subprocess
|
|
||||||
|
|
||||||
from sys import exit
|
from sys import exit
|
||||||
from os import getpid, listdir, sep, system
|
from os import getpid, listdir, sep
|
||||||
from modules.functions import *
|
from modules.functions import *
|
||||||
from modules.functions_bot import *
|
from modules.functions_bot import *
|
||||||
from pyrogram import Client, filters, idle
|
from pyrogram import Client, filters, idle
|
||||||
from pyrogram.types import ForceReply, BotCommand, BotCommandScopeChat
|
from pyrogram.types import ForceReply, BotCommand, BotCommandScopeChat
|
||||||
from pyrogram.enums.chat_action import ChatAction
|
from pyrogram.enums.chat_action import ChatAction
|
||||||
|
|
||||||
|
|
||||||
if configGet("bot_token") != "12345678:asdfghjklzxcvbnm":
|
if configGet("bot_token") != "12345678:asdfghjklzxcvbnm":
|
||||||
pid = getpid()
|
pid = getpid()
|
||||||
app = Client("auto_zoom_public_bot", api_id=configGet("api_id"), api_hash=configGet("api_hash"), bot_token=configGet("bot_token"))
|
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")
|
logWrite("Could not start the bot. Please, configure token in config.json")
|
||||||
exit()
|
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="/"))
|
@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}')
|
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
|
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}} )
|
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"):
|
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")
|
userSet(msg.chat.id, "context", "link_key")
|
||||||
else:
|
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="/"))
|
@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}')
|
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
|
user_locale = msg.from_user.language_code
|
||||||
|
|
||||||
if not userGet(msg.from_user.id, "linked"):
|
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:
|
else:
|
||||||
try:
|
try:
|
||||||
keys_storage = jsonLoad(f"data{sep}keys_storage.json")
|
keys_storage = jsonLoad(f"data{sep}keys_storage.json")
|
||||||
@ -60,40 +56,40 @@ def unlink(app, msg):
|
|||||||
pass
|
pass
|
||||||
userClear(msg.from_user.id, "api_key")
|
userClear(msg.from_user.id, "api_key")
|
||||||
userSet(msg.chat.id, "linked", False)
|
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="/"))
|
@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
|
user_locale = msg.from_user.language_code
|
||||||
|
|
||||||
if userGet(msg.from_user.id, "context") is not None:
|
if userGet(msg.from_user.id, "context") is not None:
|
||||||
userClear(msg.from_user.id, "context")
|
userClear(msg.from_user.id, "context")
|
||||||
userClear(msg.from_user.id, "context_content")
|
userClear(msg.from_user.id, "context_content")
|
||||||
msg.reply_text(locale("cancel", "msg"))
|
await msg.reply_text(locale("cancel", "msg", locale=user_locale))
|
||||||
else:
|
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="/"))
|
@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"):
|
if msg.from_user.id == configGet("admin"):
|
||||||
msg.reply_text(f"Shutting down bot with pid `{pid}`")
|
await msg.reply_text(f"Shutting down bot with pid `{pid}`")
|
||||||
system('kill -9 '+str(pid)) # REFACTOR
|
killProc(pid)
|
||||||
|
|
||||||
|
|
||||||
@app.on_message(~ filters.scheduled)
|
@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":
|
if userGet(msg.from_user.id, "context") == "link_key":
|
||||||
|
|
||||||
user_locale = msg.from_user.language_code
|
user_locale = msg.from_user.language_code
|
||||||
|
|
||||||
if msg.text in jsonLoad(configGet("api_keys"))["autozoom"]:
|
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, "api_key", msg.text)
|
||||||
userSet(msg.from_user.id, "linked", True)
|
userSet(msg.from_user.id, "linked", True)
|
||||||
keys_storage = jsonLoad(f"data{sep}keys_storage.json")
|
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}")
|
logWrite(f"Added apikey {msg.text} for user {msg.from_user.id}")
|
||||||
else:
|
else:
|
||||||
logWrite(f"User {msg.from_user.id} tried to pair with invalid apikey {msg.text}")
|
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")
|
||||||
userClear(msg.from_user.id, "context_content")
|
userClear(msg.from_user.id, "context_content")
|
||||||
@ -115,12 +111,17 @@ if __name__ == "__main__":
|
|||||||
app.start()
|
app.start()
|
||||||
app.send_message(configGet("admin"), f"Starting bot with pid `{pid}`")
|
app.send_message(configGet("admin"), f"Starting bot with pid `{pid}`")
|
||||||
|
|
||||||
all_commands = locale("cmd")
|
for entry in listdir("locale"):
|
||||||
|
|
||||||
|
if entry.endswith(".json"):
|
||||||
|
|
||||||
|
all_commands = locale("cmd", locale=entry.replace(".json", ""))
|
||||||
|
|
||||||
|
# Registering user commands
|
||||||
commands_list = []
|
commands_list = []
|
||||||
for command in all_commands["general"]:
|
for command in all_commands["general"]:
|
||||||
commands_list.append(BotCommand(command, all_commands["general"][command]))
|
commands_list.append(BotCommand(command, all_commands["general"][command]))
|
||||||
app.set_bot_commands(commands_list)
|
app.set_bot_commands(commands_list, language_code=entry.replace(".json", ""))
|
||||||
|
|
||||||
# Registering admin commands
|
# Registering admin commands
|
||||||
commands_admin_list = []
|
commands_admin_list = []
|
||||||
@ -128,11 +129,11 @@ if __name__ == "__main__":
|
|||||||
commands_admin_list.append(BotCommand(command, all_commands["general"][command]))
|
commands_admin_list.append(BotCommand(command, all_commands["general"][command]))
|
||||||
for command in all_commands["admin"]:
|
for command in all_commands["admin"]:
|
||||||
commands_admin_list.append(BotCommand(command, all_commands["admin"][command]))
|
commands_admin_list.append(BotCommand(command, all_commands["admin"][command]))
|
||||||
app.set_bot_commands(commands_admin_list, scope=BotCommandScopeChat(chat_id=configGet("admin")))
|
app.set_bot_commands(commands_admin_list, language_code=entry.replace(".json", ""), scope=BotCommandScopeChat(chat_id=configGet("admin")))
|
||||||
|
|
||||||
idle()
|
idle()
|
||||||
|
|
||||||
app.send_message(configGet("admin"), f"Shutting down bot with pid `{pid}`")
|
app.send_message(configGet("admin"), f"Shutting down bot with pid `{pid}`")
|
||||||
logWrite(f'Shutting down with PID {pid}')
|
logWrite(f'Shutting down with PID {pid}')
|
||||||
|
|
||||||
subprocess.call(f'kill -9 {pid}', shell=True)
|
killProc(pid)
|
@ -1,8 +1,7 @@
|
|||||||
{
|
{
|
||||||
"locale": "en",
|
|
||||||
"admin": 123456789,
|
"admin": 123456789,
|
||||||
"api_id": 1234567,
|
"api_id": 1234567,
|
||||||
"api_hash": "01234567890qwertyuiop",
|
"api_hash": "01234567890qwertyuiop",
|
||||||
"bot_token": "12345678:asdfghjklzxcvbnm",
|
"bot_token": "12345678:asdfghjklzxcvbnm",
|
||||||
"api_keys": "data/api_keys.json"
|
"api_keys": "/home/user/AutoZoomAPI/data/api_keys.json"
|
||||||
}
|
}
|
28
locale/uk.json
Normal file
28
locale/uk.json
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"btn": {},
|
||||||
|
"msg": {
|
||||||
|
"link_input": "Будь ласка, надішліть свій особистий ключ, отриманий із програми AutoZoom.\n\n**Потрібна допомога?**\nДізнайтесь, як працює посилання, на [нашому веб-сайті](https://www.end-play.xyz/autozoom/bot) або зверніться до [нашої служби підтримки](https://support.end-play.xyz), якщо сторінка посібника вам не допомогла.",
|
||||||
|
"cancel": "Операцію відмінено.",
|
||||||
|
"cancel_empty": "Немає що відміняти.",
|
||||||
|
"key_correct": "✅ **Акаунт успішно відв’язано**\nТепер ви отримуватимете сповіщення про всі зустрічі. Ви також можете додавати нові зустрічі до AutoZoom за допомогою команди /meeting.",
|
||||||
|
"key_wrong": "❌ **Персональний ключ недійсний**\nОтримайте ключ зв’язування за допомогою програми AutoZoom і повторіть спробу за допомогою команди бота /link.\n\n**Потрібна допомога?**\nДізнайтесь, як працює зв’язування, на [нашому веб-сайті](https://www.end-play.xyz/autozoom/bot) або зверніться до [нашої служби підтримки](https://support.end-play.xyz), якщо сторінка посібника вам не допомогла.",
|
||||||
|
"already_linked": "❌ **Акаунт вже прив’язаний**\nЯкщо ви хочете змінити свій особистий ключ, вам потрібно спочатку /unlink свій обліковий запис і спробувати /пов’язати його ще раз.",
|
||||||
|
"not_linked": "❌ **Акаунт не прив’язаний**\nСпершу потрібно /link свій обліковий запис із програмою AutoZoom.",
|
||||||
|
"unlinked": "✅ **Акаунт успішно прив’язано**\nТепер ви можете /link його з іншою програмою AutoZoom, якщо хочете."
|
||||||
|
},
|
||||||
|
"clb": {},
|
||||||
|
"fry": {
|
||||||
|
"link": "Ключ отриманий в AutoZoom"
|
||||||
|
},
|
||||||
|
"cmd": {
|
||||||
|
"general": {
|
||||||
|
"link": "Прив’язати до AutoZoom",
|
||||||
|
"unlink": "Відв’язати від AutoZoom",
|
||||||
|
"meeting": "Додати нову Zoom конференцію",
|
||||||
|
"cancel": "Відмінити поточну операцію"
|
||||||
|
},
|
||||||
|
"admin": {
|
||||||
|
"reboot": "Вимкнути бота"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,13 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""Some set of functions needed for discord/telegram bots and other types of apps"""
|
"""Some set of functions needed for discord/telegram bots and other types of apps"""
|
||||||
|
|
||||||
from os import sep, stat, makedirs
|
from os import sep, stat, makedirs, kill
|
||||||
|
from os import name as osname
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from ujson import loads, dumps
|
from ujson import loads, dumps
|
||||||
from shutil import copyfileobj
|
from shutil import copyfileobj
|
||||||
from gzip import open as gzipopen
|
from gzip import open as gzipopen
|
||||||
|
from psutil import Process
|
||||||
|
|
||||||
|
|
||||||
def nowtimeGet(format="%H:%M:%S | %d.%m.%Y"):
|
def nowtimeGet(format="%H:%M:%S | %d.%m.%Y"):
|
||||||
@ -104,3 +106,17 @@ def jsonLoad(filename):
|
|||||||
value = loads(f.read())
|
value = loads(f.read())
|
||||||
f.close()
|
f.close()
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def killProc(pid: int):
|
||||||
|
"""Kill the process by its PID
|
||||||
|
|
||||||
|
Args:
|
||||||
|
* pid (int): Process ID to be killed
|
||||||
|
"""
|
||||||
|
if osname == "posix":
|
||||||
|
from signal import SIGKILL
|
||||||
|
kill(pid, SIGKILL)
|
||||||
|
else:
|
||||||
|
p = Process(pid)
|
||||||
|
p.kill()
|
@ -56,14 +56,11 @@ def locale(key: str, *args: str, locale=configGet("locale")):
|
|||||||
locale = configGet("locale")
|
locale = configGet("locale")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{locale}.json')
|
this_dict = jsonLoad(f'locale{sep}{locale}.json')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
try:
|
try:
|
||||||
this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{configGet("locale")}.json')
|
this_dict = jsonLoad(f'locale{sep}{configGet("locale")}.json')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
try:
|
|
||||||
this_dict = jsonLoad(f'{configGet("locale_fallback", "locations")}{sep}{configGet("locale")}.json')
|
|
||||||
except:
|
|
||||||
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
|
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
|
||||||
|
|
||||||
this_key = this_dict
|
this_key = this_dict
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
Pyrogram>=2.0.0
|
Pyrogram>=2.0.0
|
||||||
ujson
|
ujson
|
||||||
|
psutil
|
Reference in New Issue
Block a user