This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Telegram/main.py

975 lines
55 KiB
Python
Raw Normal View History

import asyncio
2022-10-24 15:34:18 +03:00
from datetime import datetime
2022-10-25 15:18:51 +03:00
from threading import Thread
2022-10-24 15:34:18 +03:00
from dateutil.relativedelta import relativedelta
2022-10-20 16:08:46 +03:00
from time import time
2022-10-23 12:48:34 +03:00
from os import getpid, path
2022-10-25 15:18:51 +03:00
from modules.birthdays import check_birthdays
2022-10-17 00:30:07 +03:00
from modules.utils import *
2022-10-26 15:54:55 +03:00
from modules.inline import *
2022-10-25 15:18:51 +03:00
from schedule import run_pending, every
2022-10-26 15:54:55 +03:00
from app import app
2022-10-17 00:30:07 +03:00
from pyrogram import filters
2022-10-23 01:27:54 +03:00
from pyrogram.enums.parse_mode import ParseMode
2022-10-24 15:34:18 +03:00
from pyrogram.enums.chat_action import ChatAction
2022-10-23 20:20:33 +03:00
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, BotCommand, BotCommandScopeChat, ReplyKeyboardMarkup, ForceReply, ReplyKeyboardRemove, ChatPermissions
2022-10-17 00:30:07 +03:00
from pyrogram import idle # type: ignore
from pyrogram.errors.exceptions import bad_request_400
2022-10-21 15:00:03 +03:00
pid = getpid()
2022-10-17 00:30:07 +03:00
2022-10-21 15:00:03 +03:00
2022-11-13 14:40:49 +02:00
for entry in [f"{configGet('data', 'locations')}{sep}applications.json", f"{configGet('data', 'locations')}{sep}warnings.json"]:
2022-10-26 13:54:55 +03:00
mode = 'r' if path.exists(entry) else 'w'
with open(entry, mode) as f:
try:
f.write("{}")
except:
pass
2022-12-05 14:09:29 +02:00
def should_quote(msg):
return True if msg.chat.type is not ChatType.PRIVATE else False
2022-10-21 15:00:03 +03:00
# Start command ================================================================================================================
@app.on_message(~ filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]))
2022-10-20 13:24:32 +03:00
async def cmd_start(app, msg):
try:
2022-10-20 15:43:13 +03:00
user_stage = configGet("stage", file=str(msg.from_user.id))
if user_stage != 0:
2022-10-20 13:24:32 +03:00
return
except FileNotFoundError:
2022-10-22 10:45:02 +03:00
jsonSave(jsonLoad(f"{configGet('data', 'locations')}{sep}user_default.json"), f"{configGet('data', 'locations')}{sep}users{sep}{msg.from_user.id}.json")
2022-10-20 15:43:13 +03:00
user_stage = configGet("stage", file=str(msg.from_user.id))
configSet(["telegram_id"], str(msg.from_user.username), file=str(msg.from_user.id))
configSet(["telegram_name"], f"{msg.from_user.first_name} {msg.from_user.last_name}", file=str(msg.from_user.id))
configSet(["telegram_phone"], str(msg.from_user.phone_number), file=str(msg.from_user.id))
configSet(["telegram_locale"], str(msg.from_user.language_code), file=str(msg.from_user.id))
2022-10-20 13:24:32 +03:00
2022-10-23 12:56:28 +03:00
logWrite(f"User {msg.from_user.id} started bot interaction")
2022-10-22 10:41:58 +03:00
await msg.reply_text(locale("start", "message"), reply_markup=ReplyKeyboardMarkup(locale("welcome", "keyboard"), resize_keyboard=True)) # type: ignore
2022-10-21 15:00:03 +03:00
# ==============================================================================================================================
2022-10-20 13:24:32 +03:00
2022-10-21 15:00:03 +03:00
# Shutdown command =============================================================================================================
2022-11-13 14:40:49 +02:00
@app.on_message(~ filters.scheduled & filters.private & filters.command(["kill", "die", "reboot"], prefixes=["/"]))
2022-10-17 00:30:07 +03:00
async def cmd_kill(app, msg):
2022-11-13 14:40:49 +02:00
if msg.chat.id == configGet("admin_group") or await isAnAdmin(msg.from_user.id):
2022-10-17 00:30:07 +03:00
logWrite(f"Shutting down bot with pid {pid}")
2022-12-05 14:09:29 +02:00
await msg.reply_text(f"Вимкнення бота з підом `{pid}`", quote=should_quote(msg))
2022-10-17 00:30:07 +03:00
killProc(pid)
2022-10-21 15:00:03 +03:00
# ==============================================================================================================================
2022-10-17 00:30:07 +03:00
2022-10-27 16:13:58 +03:00
# Rules command =============================================================================================================
2022-12-01 12:27:33 +02:00
default_rules_markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(locale("rules_home", "button"), callback_data="rules_home"),
InlineKeyboardButton(locale("rules_additional", "button"), callback_data="rules_additional")
],
[
InlineKeyboardButton("1", callback_data="rule_1"),
InlineKeyboardButton("2", callback_data="rule_2"),
InlineKeyboardButton("3", callback_data="rule_3")
],
[
InlineKeyboardButton("4", callback_data="rule_4"),
InlineKeyboardButton("5", callback_data="rule_5"),
InlineKeyboardButton("6", callback_data="rule_6")
],
[
InlineKeyboardButton("7", callback_data="rule_7"),
InlineKeyboardButton("8", callback_data="rule_8"),
InlineKeyboardButton("9", callback_data="rule_9")
]
]
)
2022-11-13 14:40:49 +02:00
@app.on_message(~ filters.scheduled & filters.private & filters.command(["rules"], prefixes=["/"]))
2022-10-27 16:13:58 +03:00
async def cmd_rules(app, msg):
2022-12-01 12:27:33 +02:00
await msg.reply_text(locale("rules_msg"), disable_web_page_preview=True, reply_markup=default_rules_markup)
2022-11-30 14:02:07 +02:00
# for rule_msg in locale("rules"):
# await msg.reply_text(rule_msg)
2022-10-27 16:13:58 +03:00
# ==============================================================================================================================
2022-12-01 16:23:46 +02:00
# Warn command =================================================================================================================
2022-11-13 14:40:49 +02:00
@app.on_message(~ filters.scheduled & filters.command(["warn"], prefixes=["/"]))
async def cmd_warn(app, msg):
if msg.chat.id == configGet("destination_group"):
if msg.reply_to_message_id != None:
if isAnAdmin(msg.from_user.id):
warnings = jsonLoad(f"{configGet('data', 'locations')}{sep}warnings.json")
if str(msg.reply_to_message.from_user.id) not in warnings:
warnings[str(msg.reply_to_message.from_user.id)] = 1
else:
warnings[str(msg.reply_to_message.from_user.id)] += 1
jsonSave(warnings, f"{configGet('data', 'locations')}{sep}warnings.json")
await msg.reply_text(locale("warned", "message").format(msg.reply_to_message.from_user.first_name, msg.reply_to_message.from_user.id)) # type: ignore
# ==============================================================================================================================
2022-12-04 22:10:38 +02:00
# Message command ==============================================================================================================
@app.on_message(~ filters.scheduled & filters.command(["message"], prefixes=["/"]))
async def cmd_message(app, msg):
2022-12-05 14:09:29 +02:00
if msg.chat.id == configGet("admin_group") or await isAnAdmin(msg.from_user.id):
try:
destination = int(msg.command[1])
void = msg.command[2]
message = " ".join(msg.command[2:])
try:
await app.send_message(destination, message)
await msg.reply_text(locale("message_sent", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} sent message '{' '.join(msg.command[2:])}' to {destination}")
except bad_request_400.PeerIdInvalid:
await msg.reply_text(locale("message_no_user", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} tried to send message '{' '.join(msg.command[2:])}' to {destination} but 'PeerIdInvalid'")
except IndexError:
await msg.reply_text(locale("message_invalid_syntax", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} tried to send message but 'IndexError'")
except ValueError:
await msg.reply_text(locale("message_invalid_syntax", "message"), quote=should_quote(msg))
logWrite(f"Admin {msg.from_user.id} tried to send message but 'ValueError'")
2022-12-04 22:10:38 +02:00
# ==============================================================================================================================
2022-11-13 14:40:49 +02:00
# Warnings command =============================================================================================================
@app.on_message(~ filters.scheduled & filters.command(["warnings"], prefixes=["/"]))
async def cmd_warnings(app, msg):
if msg.chat.id == configGet("admin_group") or await isAnAdmin(msg.from_user.id):
warnings = jsonLoad(f"{configGet('data', 'locations')}{sep}warnings.json")
if len(msg.command) <= 1:
2022-12-05 14:09:29 +02:00
await msg.reply_text(locale("syntax_warnings", "message"), quote=should_quote(msg))
2022-11-13 14:40:49 +02:00
if path.exists(f"{configGet('data', 'locations')}{sep}users{sep}{msg.command[1]}.json"):
target_id = str(int(msg.command[1]))
target_name = "N/A"
else:
list_of_users = []
async for m in app.get_chat_members(configGet("destination_group"), filter=ChatMembersFilter.SEARCH, query=msg.command[1]):
list_of_users.append(m)
if len(list_of_users) != 0:
target = list_of_users[0].user
target_name = target.first_name
target_id = str(target.id)
else:
await msg.reply_text(locale("no_user_warnings", "message").format(msg.command[1])) # type: ignore
return
if target_id not in warnings:
2022-12-05 14:09:29 +02:00
await msg.reply_text(locale("no_warnings", "message").format(target_name, target_id), quote=should_quote(msg)) # type: ignore
2022-11-13 14:40:49 +02:00
else:
if warnings[target_id] <= 5:
2022-12-05 14:09:29 +02:00
await msg.reply_text(locale("warnings_1", "message").format(target_name, target_id, warnings[target_id]), quote=should_quote(msg)) # type: ignore
2022-11-13 14:40:49 +02:00
else:
2022-12-05 14:09:29 +02:00
await msg.reply_text(locale("warnings_2", "message").format(target_name, target_id, warnings[target_id]), quote=should_quote(msg)) # type: ignore
2022-11-13 14:40:49 +02:00
# ==============================================================================================================================
2022-10-24 15:34:18 +03:00
# Applications command =========================================================================================================
2022-11-13 14:40:49 +02:00
@app.on_message(~ filters.scheduled & filters.command(["applications"], prefixes=["/"]))
2022-10-24 15:34:18 +03:00
async def cmd_applications(app, msg):
2022-11-13 14:40:49 +02:00
if (await isAnAdmin(msg.from_user.id)) or (msg.chat.id == configGet("admin_group")):
2022-10-24 15:34:18 +03:00
await app.send_chat_action(msg.chat.id, ChatAction.UPLOAD_DOCUMENT)
2022-12-05 14:09:29 +02:00
await msg.reply_document(document=f"{configGet('data', 'locations')}{sep}applications.json", quote=should_quote(msg))
2022-10-24 15:34:18 +03:00
# ==============================================================================================================================
2022-10-28 15:49:52 +03:00
# Applications command =========================================================================================================
2022-11-13 14:40:49 +02:00
@app.on_message(~ filters.scheduled & filters.command(["application"], prefixes=["/"]))
2022-10-28 15:49:52 +03:00
async def cmd_application(app, msg):
2022-11-13 14:40:49 +02:00
if (await isAnAdmin(msg.from_user.id)) or (msg.chat.id == configGet("admin_group")):
2022-10-28 15:49:52 +03:00
try:
if (path.exists(f"{configGet('data', 'locations')}{sep}users{sep}{msg.command[1]}.json") and jsonLoad(f"{configGet('data', 'locations')}{sep}users{sep}{msg.command[1]}.json")["approved"]):
user_id = int(msg.command[1])
else:
list_of_users = []
async for m in app.get_chat_members(configGet("destination_group"), filter=ChatMembersFilter.SEARCH, query=msg.command[1]):
list_of_users.append(m)
user_id = list_of_users[0].user.id
try:
2022-11-13 14:40:49 +02:00
user_data = jsonLoad(f"{configGet('data', 'locations')}{sep}users{sep}{user_id}.json")
2022-12-04 12:37:45 +02:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")[str(user_id)]
2022-10-28 15:49:52 +03:00
application_content = []
i = 1
for question in configGet("application", file=str(msg.from_user.id)):
if i == 2:
2022-12-04 12:37:45 +02:00
age = relativedelta(datetime.now(), datetime.strptime(application['application']['2'], '%d.%m.%Y'))
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {application['application']['2']} ({age.years} р.)")
2022-10-28 15:49:52 +03:00
else:
2022-12-04 12:37:45 +02:00
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {application['application'][question]}")
2022-10-28 15:49:52 +03:00
i += 1
if user_data["sent"]:
if user_data["approved"]:
application_status = locale("application_status_accepted", "message").format((await app.get_users(application["approved_by"])).first_name, datetime.fromtimestamp(application["approval_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
elif application["refused"]:
application_status = locale("application_status_refused", "message").format((await app.get_users(application["refused_by"])).first_name, datetime.fromtimestamp(application["refusal_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
else:
application_status = locale("application_status_on_hold", "message")
else:
if user_data["approved"]:
application_status = locale("application_status_accepted", "message").format((await app.get_users(application["approved_by"])).first_name, datetime.fromtimestamp(application["approval_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
elif application["refused"]:
application_status = locale("application_status_refused", "message").format((await app.get_users(application["refused_by"])).first_name, datetime.fromtimestamp(application["refusal_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
else:
application_status = locale("application_status_not_send", "message")
2022-11-13 14:40:49 +02:00
logWrite(f"User {msg.from_user.id} requested application of {user_id}")
2022-12-05 14:09:29 +02:00
await msg.reply_text(locale("contact", "message").format(str(user_id), "\n".join(application_content), application_status), quote=should_quote(msg)) # type: ignore
2022-10-28 15:49:52 +03:00
except FileNotFoundError:
2022-11-13 14:40:49 +02:00
logWrite(f"User {msg.from_user.id} requested application of {user_id} but user does not exists")
2022-12-05 14:09:29 +02:00
await msg.reply_text(locale("contact_invalid", "message"), quote=should_quote(msg))
2022-10-28 15:49:52 +03:00
except IndexError:
2022-12-05 14:09:29 +02:00
await msg.reply_text(locale("application_invalid_syntax", "message"), quote=should_quote(msg))
2022-10-28 15:49:52 +03:00
# ==============================================================================================================================
2022-10-25 14:36:16 +03:00
# Reapply command ==============================================================================================================
2022-11-13 14:40:49 +02:00
@app.on_message(~ filters.scheduled & filters.private & filters.command(["reapply"], prefixes=["/"]))
2022-10-25 14:36:16 +03:00
async def cmd_reapply(app, msg):
if configGet("approved", file=str(msg.from_user.id)) or configGet("refused", file=str(msg.from_user.id)):
if (configGet("stage", file=str(msg.from_user.id)) == 10) and not (configGet("sent", file=str(msg.from_user.id))):
2022-12-05 15:38:18 +02:00
left_chat = True
async for member in app.get_chat_members(configGet("destination_group")):
if member.user.id == msg.from_user.id:
left_chat = False
if not left_chat:
configSet(["reapply"], True, file=str(msg.from_user.id))
configSet(["confirmed"], False, file=str(msg.from_user.id))
await welcome_pass(app, msg, once_again=True)
else:
await msg.reply_text(locale("reapply_left_chat", "message"), reply_markup=InlineKeyboardMarkup([
[
InlineKeyboardButton(locale("reapply_old_one", "button"), f"reapply_old_{msg.id}")
2022-12-05 15:38:18 +02:00
],
[
InlineKeyboardButton(locale("reapply_new_one", "button"), f"reapply_new_{msg.id}")
2022-12-05 15:38:18 +02:00
]
]))
2022-10-25 14:36:16 +03:00
else:
2022-12-05 15:38:18 +02:00
await msg.reply_text(locale("reapply_in_progress", "message").format(locale("confirm", "keyboard")[1][0]), reply_markup=InlineKeyboardMarkup([
[
InlineKeyboardButton(locale("applying_stop", "button"), f"reapply_stop_{msg.id}")
]
])) # type: ignore
2022-10-25 14:36:16 +03:00
else:
if configGet("sent", file=str(msg.from_user.id)):
await msg.reply_text(locale("reapply_forbidden", "message"))
else:
2022-12-05 15:38:18 +02:00
await msg.reply_text(locale("reapply_in_progress", "message").format(locale("confirm", "keyboard")[1][0]), reply_markup=InlineKeyboardMarkup([
[
InlineKeyboardButton(locale("applying_stop", "button"), f"reapply_stop_{msg.id}")
]
])) # type: ignore
2022-10-25 14:36:16 +03:00
# ==============================================================================================================================
2022-10-21 15:00:03 +03:00
# Welcome check ================================================================================================================
@app.on_message(~ filters.scheduled & filters.private & (filters.regex(locale("welcome", "keyboard")[0][0]) | filters.regex(locale("return", "keyboard")[0][0])))
2022-12-05 15:38:18 +02:00
async def welcome_pass(app, msg, once_again: bool = True) -> None:
"""Set user's stage to 1 and start a fresh application
### Args:
* app (app): Pyrogram Client to use
* msg (Message): Message with .from_user.id attribute equal to the end-user ID whose application will be started
* once_again (bool, optional): Set to False if it's the first time as user applies. Defaults to True.
"""
2022-10-20 13:24:32 +03:00
if not once_again:
await msg.reply_text(locale("privacy_notice", "message"))
2022-10-23 12:56:28 +03:00
logWrite(f"User {msg.from_user.id} confirmed starting the application")
2022-10-22 10:41:58 +03:00
await msg.reply_text(locale("question1", "message"), reply_markup=ForceReply(placeholder=locale("question1", "force_reply"))) # type: ignore
configSet(["stage"], 1, file=str(msg.from_user.id))
configSet(["sent"], False, file=str(msg.from_user.id))
2022-10-20 15:43:13 +03:00
2022-10-21 15:00:03 +03:00
@app.on_message(~ filters.scheduled & filters.private & (filters.regex(locale("welcome", "keyboard")[1][0])))
2022-10-20 15:43:13 +03:00
async def welcome_reject(app, msg):
2022-10-23 12:56:28 +03:00
logWrite(f"User {msg.from_user.id} refused to start the application")
2022-10-22 10:41:58 +03:00
await msg.reply_text(locale("goodbye", "message"), reply_markup=ReplyKeyboardMarkup(locale("return", "keyboard"), resize_keyboard=True)) # type: ignore
2022-10-21 15:00:03 +03:00
# ==============================================================================================================================
2022-10-20 16:08:46 +03:00
2022-10-21 15:00:03 +03:00
# Confirmation =================================================================================================================
@app.on_message(~ filters.scheduled & filters.private & (filters.regex(locale("confirm", "keyboard")[0][0])))
async def confirm_yes(app, msg):
user_stage = configGet("stage", file=str(msg.from_user.id))
if user_stage == 10:
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
if not configGet("sent", file=str(msg.from_user.id)):
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
await msg.reply_text(locale("application_sent", "message"), reply_markup=ReplyKeyboardRemove())
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
applications = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
applications[str(msg.from_user.id)] = {
"approved": False,
"approved_by": None,
"approval_date": None,
"refused": False,
"refused_by": False,
"refusal_date": False,
"application_date": int(time()),
"application": configGet("application", file=str(msg.from_user.id))
}
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
jsonSave(applications, f"{configGet('data', 'locations')}{sep}applications.json")
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
application_content = []
i = 1
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
for question in configGet("application", file=str(msg.from_user.id)):
2022-10-24 15:34:18 +03:00
if i == 2:
age = relativedelta(datetime.now(), datetime.strptime(configGet('application', file=str(msg.from_user.id))['2'], '%d.%m.%Y'))
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {configGet('application', file=str(msg.from_user.id))['2']} ({age.years} р.)")
else:
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {configGet('application', file=str(msg.from_user.id))[question]}")
2022-10-23 13:28:15 +03:00
i += 1
2022-10-25 14:36:16 +03:00
if configGet("reapply", file=str(msg.from_user.id)):
await app.send_message(chat_id=configGet("admin_group"), text=(locale("reapply_got", "message")).format(str(msg.from_user.id), msg.from_user.first_name, msg.from_user.last_name, msg.from_user.username, "\n".join(application_content)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( # type: ignore
2022-10-23 13:28:15 +03:00
[
2022-10-25 14:36:16 +03:00
[
InlineKeyboardButton(text=str(locale("reapply_yes", "button")), callback_data=f"reapply_yes_{msg.from_user.id}")
],
[
InlineKeyboardButton(text=str(locale("reapply_no", "button")), callback_data=f"reapply_no_{msg.from_user.id}")
]
]
)
)
else:
await app.send_message(chat_id=configGet("admin_group"), text=(locale("application_got", "message")).format(str(msg.from_user.id), msg.from_user.first_name, msg.from_user.last_name, msg.from_user.username, "\n".join(application_content)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( # type: ignore
2022-10-23 13:28:15 +03:00
[
2022-10-25 14:36:16 +03:00
[
InlineKeyboardButton(text=str(locale("sub_yes", "button")), callback_data=f"sub_yes_{msg.from_user.id}")
],
[
InlineKeyboardButton(text=str(locale("sub_no", "button")), callback_data=f"sub_no_{msg.from_user.id}")
],
[
InlineKeyboardButton(text=str(locale("sub_no_aggressive", "button")), callback_data=f"sub_no_aggresive_{msg.from_user.id}")
],
[
InlineKeyboardButton(text=str(locale("sub_no_russian", "button")), callback_data=f"sub_no_russian_{msg.from_user.id}")
]
2022-10-23 13:28:15 +03:00
]
2022-10-25 14:36:16 +03:00
)
2022-10-23 13:28:15 +03:00
)
2022-10-21 15:00:03 +03:00
2022-10-23 13:28:15 +03:00
logWrite(f"User {msg.from_user.id} sent his application and it will now be reviewed")
2022-10-23 12:56:28 +03:00
configSet(["sent"], True, file=str(msg.from_user.id))
configSet(["confirmed"], True, file=str(msg.from_user.id))
2022-10-21 15:00:03 +03:00
@app.on_message(~ filters.scheduled & filters.private & (filters.regex(locale("confirm", "keyboard")[1][0])))
async def confirm_no(app, msg):
user_stage = configGet("stage", file=str(msg.from_user.id))
if user_stage == 10:
2022-10-22 10:45:02 +03:00
jsonSave(jsonLoad(f"{configGet('data', 'locations')}{sep}user_default.json"), f"{configGet('data', 'locations')}{sep}users{sep}{msg.from_user.id}.json")
configSet(["telegram_id"], str(msg.from_user.username), file=str(msg.from_user.id))
configSet(["telegram_name"], f"{msg.from_user.first_name} {msg.from_user.last_name}", file=str(msg.from_user.id))
configSet(["telegram_phone"], str(msg.from_user.phone_number), file=str(msg.from_user.id))
configSet(["telegram_locale"], str(msg.from_user.language_code), file=str(msg.from_user.id))
await welcome_pass(app, msg, once_again=True)
2022-10-23 12:56:28 +03:00
logWrite(f"User {msg.from_user.id} restarted the application due to typo in it")
2022-10-21 15:00:03 +03:00
# ==============================================================================================================================
2022-10-24 00:40:43 +03:00
# Callbacks application ========================================================================================================
2022-10-22 10:41:58 +03:00
@app.on_callback_query(filters.regex("sub_yes_[\s\S]*")) # type: ignore
2022-10-21 15:00:03 +03:00
async def callback_query_accept(app, clb):
fullclb = clb.data.split("_")
2022-10-23 01:27:54 +03:00
await app.send_message(configGet("admin_group"), locale("approved_by", "message").format(clb.from_user.first_name, fullclb[2]), disable_notification=True) # type: ignore
2022-10-23 12:56:28 +03:00
logWrite(f"User {fullclb[2]} got approved by {clb.from_user.id}")
2022-10-21 16:11:13 +03:00
2022-10-24 15:34:18 +03:00
need_link = True
2022-10-21 16:11:13 +03:00
2022-10-24 15:34:18 +03:00
async for member in app.get_chat_members(configGet("destination_group")):
if member.user.id == int(fullclb[2]):
need_link = False
if need_link:
link = await app.create_chat_invite_link(configGet("destination_group"), name=f"Invite for {fullclb[2]}", member_limit=1) #, expire_date=datetime.now()+timedelta(days=1))
2022-10-21 15:00:03 +03:00
2022-10-27 16:13:58 +03:00
await app.send_message(int(fullclb[2]), locale("read_rules", "message"))
for rule_msg in locale("rules"):
await app.send_message(int(fullclb[2]), rule_msg)
2022-10-24 15:34:18 +03:00
await app.send_message(int(fullclb[2]), locale("approved", "message"), reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(str(locale("join", "button")), url=link.invite_link)
]]
))
configSet(["link"], link.invite_link, file=fullclb[2])
2022-10-24 15:34:18 +03:00
logWrite(f"User {fullclb[2]} got an invite link {link.invite_link}")
2022-12-04 22:10:38 +02:00
2022-10-24 15:34:18 +03:00
else:
await app.send_message(int(fullclb[2]), locale("approved_joined", "message"))
2022-10-23 01:27:54 +03:00
configSet(["approved"], True, file=fullclb[2])
configSet(["sent"], False, file=fullclb[2])
2022-10-23 12:56:28 +03:00
2022-10-23 01:27:54 +03:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
application[fullclb[2]]["approved"] = True
application[fullclb[2]]["approved_by"] = clb.from_user.id
application[fullclb[2]]["approval_date"] = int(time())
jsonSave(application, f"{configGet('data', 'locations')}{sep}applications.json")
2022-10-22 10:41:58 +03:00
edited_markup = [[InlineKeyboardButton(text=str(locale("accepted", "button")), callback_data="nothing")]]
2022-10-20 16:08:46 +03:00
2022-10-21 15:00:03 +03:00
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
2022-10-22 10:41:58 +03:00
await clb.answer(text=locale("sub_accepted", "callback").format(fullclb[2]), show_alert=True) # type: ignore
2022-10-21 15:00:03 +03:00
2022-10-23 01:27:54 +03:00
@app.on_callback_query(filters.regex("sub_no_aggressive_[\s\S]*")) # type: ignore
async def callback_query_refuse_aggressive(app, clb):
2022-10-21 15:00:03 +03:00
fullclb = clb.data.split("_")
2022-10-23 01:27:54 +03:00
await app.send_message(configGet("admin_group"), locale("refused_by_agr", "message").format(clb.from_user.first_name, fullclb[3]), disable_notification=True) # type: ignore
await app.send_message(int(fullclb[3]), locale("refused", "message"))
2022-10-23 12:56:28 +03:00
logWrite(f"User {fullclb[3]} got refused by {clb.from_user.id} due to being aggressive")
2022-10-23 01:27:54 +03:00
configSet(["refused"], True, file=fullclb[3])
configSet(["sent"], False, file=fullclb[3])
2022-10-23 01:27:54 +03:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
application[fullclb[3]]["refused"] = True
application[fullclb[3]]["refused_by"] = clb.from_user.id
application[fullclb[3]]["refusal_date"] = int(time())
jsonSave(application, f"{configGet('data', 'locations')}{sep}applications.json")
2022-10-21 15:00:03 +03:00
2022-10-22 10:41:58 +03:00
edited_markup = [[InlineKeyboardButton(text=str(locale("declined", "button")), callback_data="nothing")]]
2022-10-21 15:00:03 +03:00
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
2022-10-23 01:27:54 +03:00
await clb.answer(text=locale("sub_no_aggressive", "callback").format(fullclb[3]), show_alert=True) # type: ignore
2022-10-21 15:00:03 +03:00
2022-10-23 01:27:54 +03:00
@app.on_callback_query(filters.regex("sub_no_russian_[\s\S]*")) # type: ignore
async def callback_query_refuse_russian(app, clb):
2022-10-21 15:00:03 +03:00
fullclb = clb.data.split("_")
2022-10-23 01:27:54 +03:00
await app.send_message(configGet("admin_group"), locale("refused_by_rus", "message").format(clb.from_user.first_name, fullclb[3]), disable_notification=True) # type: ignore
await app.send_message(int(fullclb[3]), locale("refused", "message"))
await app.send_message(int(fullclb[3]), locale("refused_russian", "message"))
2022-10-23 12:56:28 +03:00
logWrite(f"User {fullclb[3]} got refused by {clb.from_user.id} due to being russian")
2022-10-23 01:27:54 +03:00
configSet(["refused"], True, file=fullclb[3])
configSet(["sent"], False, file=fullclb[3])
2022-10-23 01:27:54 +03:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
application[fullclb[3]]["refused"] = True
application[fullclb[3]]["refused_by"] = clb.from_user.id
application[fullclb[3]]["refusal_date"] = int(time())
jsonSave(application, f"{configGet('data', 'locations')}{sep}applications.json")
2022-10-21 15:00:03 +03:00
2022-10-22 10:41:58 +03:00
edited_markup = [[InlineKeyboardButton(text=str(locale("declined", "button")), callback_data="nothing")]]
2022-10-21 15:00:03 +03:00
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
2022-10-23 01:27:54 +03:00
await clb.answer(text=locale("sub_no_russian", "callback").format(fullclb[3]), show_alert=True) # type: ignore
2022-10-21 15:00:03 +03:00
2022-10-23 01:27:54 +03:00
@app.on_callback_query(filters.regex("sub_no_[\s\S]*")) # type: ignore
async def callback_query_refuse(app, clb):
2022-10-21 15:00:03 +03:00
fullclb = clb.data.split("_")
2022-10-23 01:27:54 +03:00
await app.send_message(configGet("admin_group"), locale("refused_by", "message").format(clb.from_user.first_name, fullclb[2]), disable_notification=True) # type: ignore
2022-10-22 10:41:58 +03:00
await app.send_message(int(fullclb[2]), locale("refused", "message"))
2022-10-23 12:56:28 +03:00
logWrite(f"User {fullclb[2]} got refused by {clb.from_user.id}")
2022-10-21 15:00:03 +03:00
configSet(["refused"], True, file=fullclb[2])
configSet(["sent"], False, file=fullclb[2])
2022-10-25 14:36:16 +03:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
application[fullclb[2]]["refused"] = True
application[fullclb[2]]["refused_by"] = clb.from_user.id
application[fullclb[2]]["refusal_date"] = int(time())
jsonSave(application, f"{configGet('data', 'locations')}{sep}applications.json")
edited_markup = [[InlineKeyboardButton(text=str(locale("declined", "button")), callback_data="nothing")]]
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
await clb.answer(text=locale("sub_refused", "callback").format(fullclb[2]), show_alert=True) # type: ignore
# ==============================================================================================================================
# Callbacks application ========================================================================================================
@app.on_callback_query(filters.regex("reapply_yes_[\s\S]*")) # type: ignore
2022-11-13 14:40:49 +02:00
async def callback_reapply_query_accept(app, clb):
2022-10-25 14:36:16 +03:00
fullclb = clb.data.split("_")
await app.send_message(configGet("admin_group"), locale("approved_by", "message").format(clb.from_user.first_name, fullclb[2]), disable_notification=True) # type: ignore
logWrite(f"User {fullclb[2]} got their reapplication approved by {clb.from_user.id}")
await app.send_message(int(fullclb[2]), locale("approved_joined", "message"))
configSet(["approved"], True, file=fullclb[2])
configSet(["sent"], False, file=fullclb[2])
2022-10-25 14:36:16 +03:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
application[fullclb[2]]["approved"] = True
application[fullclb[2]]["approved_by"] = clb.from_user.id
application[fullclb[2]]["approval_date"] = int(time())
jsonSave(application, f"{configGet('data', 'locations')}{sep}applications.json")
edited_markup = [[InlineKeyboardButton(text=str(locale("accepted", "button")), callback_data="nothing")]]
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
await clb.answer(text=locale("sub_accepted", "callback").format(fullclb[2]), show_alert=True) # type: ignore
2022-12-04 16:05:51 +02:00
need_link = True
async for member in app.get_chat_members(configGet("destination_group")):
if member.user.id == int(fullclb[2]):
need_link = False
if need_link:
link = await app.create_chat_invite_link(configGet("destination_group"), name=f"Invite for {fullclb[2]}", member_limit=1) #, expire_date=datetime.now()+timedelta(days=1))
await app.send_message(int(fullclb[2]), locale("read_rules", "message"))
for rule_msg in locale("rules"):
await app.send_message(int(fullclb[2]), rule_msg)
await app.send_message(int(fullclb[2]), locale("approved", "message"), reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(str(locale("join", "button")), url=link.invite_link)
]]
))
configSet(["link"], link.invite_link, file=fullclb[2])
logWrite(f"User {fullclb[2]} got an invite link {link.invite_link}")
else:
await app.send_message(int(fullclb[2]), locale("approved_joined", "message"))
2022-10-25 14:36:16 +03:00
@app.on_callback_query(filters.regex("reapply_no_[\s\S]*")) # type: ignore
2022-11-13 14:40:49 +02:00
async def callback_query_reapply_refuse(app, clb):
2022-10-25 14:36:16 +03:00
fullclb = clb.data.split("_")
await app.send_message(configGet("admin_group"), locale("refused_by", "message").format(clb.from_user.first_name, fullclb[2]), disable_notification=True) # type: ignore
await app.send_message(int(fullclb[2]), locale("refused", "message"))
logWrite(f"User {fullclb[2]} got their reapplication refused by {clb.from_user.id}")
configSet(["refused"], True, file=fullclb[2])
configSet(["sent"], False, file=fullclb[2])
2022-10-23 01:27:54 +03:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
application[fullclb[2]]["refused"] = True
application[fullclb[2]]["refused_by"] = clb.from_user.id
application[fullclb[2]]["refusal_date"] = int(time())
jsonSave(application, f"{configGet('data', 'locations')}{sep}applications.json")
2022-10-22 10:41:58 +03:00
edited_markup = [[InlineKeyboardButton(text=str(locale("declined", "button")), callback_data="nothing")]]
2022-10-21 15:00:03 +03:00
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
2022-10-23 01:27:54 +03:00
await clb.answer(text=locale("sub_refused", "callback").format(fullclb[2]), show_alert=True) # type: ignore
2022-12-05 15:38:18 +02:00
# Use old application when user reapplies after leaving the chat
@app.on_callback_query(filters.regex("reapply_old_[\s\S]*")) # type: ignore
async def callback_query_reapply_old(app, clb):
fullclb = clb.data.split("_")
message = await app.get_messages(clb.from_user.id, int(fullclb[2]))
configSet(["approved"], False, file=str(clb.from_user.id))
configSet(["refused"], False, file=str(clb.from_user.id))
await confirm_yes(app, message)
await clb.message.edit(clb.message.text, reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(locale("done", "button"), "nothing")]]))
2022-12-05 15:38:18 +02:00
# Start a new application when user reapplies after leaving the chat
@app.on_callback_query(filters.regex("reapply_new_[\s\S]*")) # type: ignore
async def callback_query_reapply_new(app, clb):
fullclb = clb.data.split("_")
jsonSave(jsonLoad(f"{configGet('data', 'locations')}{sep}user_default.json"), f"{configGet('data', 'locations')}{sep}users{sep}{clb.from_user.id}.json")
configSet(["telegram_id"], str(clb.from_user.username), file=str(clb.from_user.id))
configSet(["telegram_name"], f"{clb.from_user.first_name} {clb.from_user.last_name}", file=str(clb.from_user.id))
configSet(["telegram_phone"], str(clb.from_user.phone_number), file=str(clb.from_user.id))
configSet(["telegram_locale"], str(clb.from_user.language_code), file=str(clb.from_user.id))
await clb.answer(locale("reapply_stopped", "callback"))
message = await app.get_messages(clb.from_user.id, int(fullclb[2]))
await welcome_pass(app, message, once_again=True)
logWrite(f"User {clb.from_user.id} restarted the application after leaving the chat earlier")
await clb.message.edit(clb.message.text, reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(locale("done", "button"), "nothing")]]))
2022-12-05 15:38:18 +02:00
# Abort application fill in progress and restart it
@app.on_callback_query(filters.regex("reapply_stop_[\s\S]*")) # type: ignore
async def callback_query_reapply_stop(app, clb):
fullclb = clb.data.split("_")
jsonSave(jsonLoad(f"{configGet('data', 'locations')}{sep}user_default.json"), f"{configGet('data', 'locations')}{sep}users{sep}{clb.from_user.id}.json")
configSet(["telegram_id"], str(clb.from_user.username), file=str(clb.from_user.id))
configSet(["telegram_name"], f"{clb.from_user.first_name} {clb.from_user.last_name}", file=str(clb.from_user.id))
configSet(["telegram_phone"], str(clb.from_user.phone_number), file=str(clb.from_user.id))
configSet(["telegram_locale"], str(clb.from_user.language_code), file=str(clb.from_user.id))
await clb.answer(locale("reapply_stopped", "callback"))
message = await app.get_messages(clb.from_user.id, int(fullclb[2]))
await welcome_pass(app, message, once_again=True)
logWrite(f"User {clb.from_user.id} restarted the application due to typo in it")
await clb.message.edit(clb.message.text, reply_markup=None)
2022-10-21 15:00:03 +03:00
# ==============================================================================================================================
2022-10-23 01:27:54 +03:00
2022-10-24 00:40:43 +03:00
# Callbacks sus users ==========================================================================================================
@app.on_callback_query(filters.regex("sus_allow_[\s\S]*")) # type: ignore
async def callback_query_sus_allow(app, clb):
fullclb = clb.data.split("_")
await app.send_message(configGet("admin_group"), locale("sus_allowed_by", "message").format(clb.from_user.first_name, fullclb[2]), disable_notification=True) # type: ignore
logWrite(f"User {fullclb[2]} was allowed to join with another link by {clb.from_user.id}")
edited_markup = [[InlineKeyboardButton(text=str(locale("sus_allowed", "button")), callback_data="nothing")]]
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
await clb.answer(text=locale("sus_allowed", "callback").format(fullclb[2]), show_alert=True) # type: ignore
await app.restrict_chat_member(configGet("destination_group"), int(fullclb[2]), permissions=ChatPermissions(
can_send_messages=True,
can_send_media_messages=True,
can_send_other_messages=True,
can_send_polls=True
)
)
@app.on_callback_query(filters.regex("sus_refuse_[\s\S]*")) # type: ignore
async def callback_query_sus_refuse(app, clb):
fullclb = clb.data.split("_")
await app.send_message(configGet("admin_group"), locale("sus_refused_by", "message").format(clb.from_user.first_name, fullclb[2]), disable_notification=True) # type: ignore
logWrite(f"User {fullclb[2]} was refused to join with another link by {clb.from_user.id}")
edited_markup = [[InlineKeyboardButton(text=str(locale("sus_refused", "button")), callback_data="nothing")]]
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
await clb.answer(text=locale("sus_refused", "callback").format(fullclb[2]), show_alert=True) # type: ignore
await app.ban_chat_member(configGet("destination_group"), int(fullclb[2]))
jsonSave(jsonLoad(f"{configGet('data', 'locations')}{sep}user_default.json"), f"{configGet('data', 'locations')}{sep}users{sep}{fullclb[2]}.json")
configSet(["stage"], 10, file=fullclb[2])
configSet(["refused"], True, file=fullclb[2])
configSet(["refused_by"], clb.from_user.id, file=fullclb[2])
2022-10-24 00:40:43 +03:00
# ==============================================================================================================================
# Callback empty ===============================================================================================================
@app.on_callback_query(filters.regex("nothing")) # type: ignore
async def callback_query_nothing(app, clb):
await clb.answer(text=locale("nothing", "callback"))
# ==============================================================================================================================
2022-11-30 14:02:07 +02:00
# Callback rule ================================================================================================================
@app.on_callback_query(filters.regex("rule_[\s\S]*")) # type: ignore
async def callback_query_rule(app, clb):
fullclb = clb.data.split("_")
logWrite(f"User {clb.from_user.id} requested to check out rule {fullclb[1]}")
2022-12-01 12:27:33 +02:00
rule_num = int(fullclb[1])
if rule_num == len(locale("rules")):
lower_buttons = [
InlineKeyboardButton(locale("rules_prev", "button"), callback_data=f"rule_{rule_num-1}")
]
elif rule_num == 1:
lower_buttons = [
InlineKeyboardButton(locale("rules_next", "button"), callback_data=f"rule_{rule_num+1}")
]
else:
lower_buttons = [
InlineKeyboardButton(locale("rules_prev", "button"), callback_data=f"rule_{rule_num-1}"),
InlineKeyboardButton(locale("rules_next", "button"), callback_data=f"rule_{rule_num+1}")
]
2022-11-30 14:02:07 +02:00
try:
2022-12-01 12:27:33 +02:00
await clb.message.edit(text=locale("rules")[rule_num-1], disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(locale("rules_home", "button"), callback_data="rules_home"),
InlineKeyboardButton(locale("rules_additional", "button"), callback_data="rules_additional")
],
lower_buttons
]
)
)
2022-11-30 14:02:07 +02:00
except bad_request_400.MessageNotModified:
pass
await clb.answer(text=locale("rules_page", "callback").format(fullclb[1])) # type: ignore
@app.on_callback_query(filters.regex("rules_home")) # type: ignore
async def callback_query_rules_home(app, clb):
logWrite(f"User {clb.from_user.id} requested to check out homepage rules")
try:
2022-12-01 12:27:33 +02:00
await clb.message.edit(text=locale("rules_msg"), disable_web_page_preview=True, reply_markup=default_rules_markup)
2022-11-30 14:02:07 +02:00
except bad_request_400.MessageNotModified:
pass
await clb.answer(text=locale("rules_home", "callback")) # type: ignore
@app.on_callback_query(filters.regex("rules_additional")) # type: ignore
async def callback_query_rules_additional(app, clb):
logWrite(f"User {clb.from_user.id} requested to check out additional rules")
try:
2022-12-01 12:27:33 +02:00
await clb.message.edit(text=locale("rules_additional"), disable_web_page_preview=True, reply_markup=default_rules_markup)
2022-11-30 14:02:07 +02:00
except bad_request_400.MessageNotModified:
pass
await clb.answer(text=locale("rules_additional", "callback")) # type: ignore
# ==============================================================================================================================
2022-10-24 00:40:43 +03:00
2022-10-23 01:42:21 +03:00
# Contact getting ==============================================================================================================
2022-10-23 12:48:34 +03:00
@app.on_message(~ filters.scheduled & filters.contact & filters.private)
2022-10-23 01:27:54 +03:00
async def get_contact(app, msg):
2022-10-24 15:34:18 +03:00
if (path.exists(f"{configGet('data', 'locations')}{sep}users{sep}{msg.from_user.id}.json") and jsonLoad(f"{configGet('data', 'locations')}{sep}users{sep}{msg.from_user.id}.json")["approved"]) or (await isAnAdmin(msg.from_user.id)):
2022-10-23 01:27:54 +03:00
if msg.contact.user_id != None:
try:
user_data = jsonLoad(f"{configGet('data', 'locations')}{sep}users{sep}{msg.contact.user_id}.json")
2022-12-04 12:37:45 +02:00
application = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")[str(msg.contact.user_id)]
2022-10-23 01:27:54 +03:00
application_content = []
i = 1
2022-12-04 12:37:45 +02:00
for question in application["application"]:
2022-10-24 15:34:18 +03:00
if i == 2:
2022-12-04 12:37:45 +02:00
age = relativedelta(datetime.now(), datetime.strptime(application['application']['2'], '%d.%m.%Y'))
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {application['application']['2']} ({age.years} р.)")
2022-10-24 15:34:18 +03:00
else:
2022-12-04 12:37:45 +02:00
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {application['application'][question]}")
2022-10-23 01:27:54 +03:00
i += 1
if user_data["sent"]:
if user_data["approved"]:
2022-10-23 12:48:34 +03:00
application_status = locale("application_status_accepted", "message").format((await app.get_users(application["approved_by"])).first_name, datetime.fromtimestamp(application["approval_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
2022-10-23 01:27:54 +03:00
elif application["refused"]:
2022-10-23 12:48:34 +03:00
application_status = locale("application_status_refused", "message").format((await app.get_users(application["refused_by"])).first_name, datetime.fromtimestamp(application["refusal_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
2022-10-23 01:27:54 +03:00
else:
application_status = locale("application_status_on_hold", "message")
else:
2022-10-25 14:36:16 +03:00
if user_data["approved"]:
application_status = locale("application_status_accepted", "message").format((await app.get_users(application["approved_by"])).first_name, datetime.fromtimestamp(application["approval_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
elif application["refused"]:
application_status = locale("application_status_refused", "message").format((await app.get_users(application["refused_by"])).first_name, datetime.fromtimestamp(application["refusal_date"]).strftime("%d.%m.%Y, %H:%M")) # type: ignore
else:
application_status = locale("application_status_not_send", "message")
2022-10-23 12:56:28 +03:00
logWrite(f"User {msg.from_user.id} requested application of {msg.contact.user_id}")
2022-10-23 01:27:54 +03:00
await msg.reply_text(locale("contact", "message").format(str(msg.contact.user_id), "\n".join(application_content), application_status)) # type: ignore
except FileNotFoundError:
2022-10-23 12:56:28 +03:00
logWrite(f"User {msg.from_user.id} requested application of {msg.contact.user_id} but user does not exists")
2022-10-23 01:27:54 +03:00
await msg.reply_text(locale("contact_invalid", "message"))
else:
2022-10-23 12:56:28 +03:00
logWrite(f"User {msg.from_user.id} requested application of someone but user is not telegram user")
2022-10-23 01:27:54 +03:00
await msg.reply_text(locale("contact_not_member", "message"))
2022-10-23 01:42:21 +03:00
# ==============================================================================================================================
2022-10-23 01:27:54 +03:00
2022-10-21 15:00:03 +03:00
# Any other input ==============================================================================================================
@app.on_message(~ filters.scheduled & filters.private)
2022-10-20 16:08:46 +03:00
async def any_stage(app, msg):
if msg.via_bot is None:
2022-10-24 15:34:18 +03:00
2022-10-26 15:54:55 +03:00
user_stage = configGet("stage", file=str(msg.from_user.id))
if user_stage == 1:
await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply"))))
logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application")
configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id))
configSet(["stage"], user_stage+1, file=str(msg.from_user.id))
2022-10-24 15:34:18 +03:00
2022-10-26 15:54:55 +03:00
elif user_stage == 2:
2022-10-24 15:34:18 +03:00
2022-10-26 15:54:55 +03:00
try:
2022-10-24 15:34:18 +03:00
configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id))
2022-10-24 15:34:18 +03:00
2022-10-26 15:54:55 +03:00
input_dt = datetime.strptime(msg.text, "%d.%m.%Y")
2022-10-24 15:34:18 +03:00
2022-10-26 15:54:55 +03:00
if datetime.now() <= input_dt:
logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to joking")
await msg.reply_text(locale("question2_joke", "message"), reply_markup=ForceReply(placeholder=str(locale("question2", "force_reply"))))
2022-10-24 15:34:18 +03:00
2022-10-26 15:54:55 +03:00
elif ((datetime.now() - input_dt).days) < ((datetime.now() - datetime.now().replace(year=datetime.now().year - configGet("age_allowed"))).days):
logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to being underage")
await msg.reply_text(locale("question2_underage", "message").format(str(configGet("age_allowed"))), reply_markup=ForceReply(placeholder=str(locale("question2", "force_reply")))) # type: ignore
2022-10-24 15:34:18 +03:00
2022-10-26 15:54:55 +03:00
else:
logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application")
await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply"))))
configSet(["stage"], user_stage+1, file=str(msg.from_user.id))
2022-10-26 15:54:55 +03:00
except ValueError:
logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to sending invalid date format")
await msg.reply_text(locale(f"question2_invalid", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage}", "force_reply"))))
2022-10-24 15:34:18 +03:00
2022-10-20 16:08:46 +03:00
else:
2022-10-26 15:54:55 +03:00
if user_stage <= 9:
logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application")
await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply"))))
configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id))
configSet(["stage"], user_stage+1, file=str(msg.from_user.id))
2022-10-26 15:54:55 +03:00
else:
if not configGet("sent", file=str(msg.from_user.id)):
if not configGet("confirmed", file=str(msg.from_user.id)):
configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id))
2022-10-26 15:54:55 +03:00
application_content = []
i = 1
for question in configGet("application", file=str(msg.from_user.id)):
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {configGet('application', file=str(msg.from_user.id))[question]}")
i += 1
await msg.reply_text(locale("confirm", "message").format("\n".join(application_content)), reply_markup=ReplyKeyboardMarkup(locale("confirm", "keyboard"), resize_keyboard=True)) # type: ignore
#configSet("sent", True, file=str(msg.from_user.id))
#configSet("application_date", int(time()), file=str(msg.from_user.id))
else:
if not configGet("approved", file=str(msg.from_user.id)) and not configGet("refused", file=str(msg.from_user.id)):
await msg.reply_text(locale("already_sent", "message"))
else:
2022-10-25 14:36:16 +03:00
if not configGet("approved", file=str(msg.from_user.id)) and not configGet("refused", file=str(msg.from_user.id)):
await msg.reply_text(locale("already_sent", "message"))
@app.on_message(~ filters.scheduled & filters.group)
async def message_in_group(app, msg):
if (msg.chat is not None) and (msg.via_bot is not None):
if (msg.via_bot.id == configGet("bot_id")) and (msg.chat.id == configGet("destination_group")):
if configGet("remove_application_time") > 0:
logWrite(f"User {msg.from_user.id} requested application in destination group, removing in {configGet('remove_application_time')} minutes")
await asyncio.sleep(configGet("remove_application_time")*60)
await msg.delete()
logWrite(f"Removed application requested by {msg.from_user.id} in destination group")
2022-10-21 15:00:03 +03:00
# ==============================================================================================================================
2022-10-20 13:24:32 +03:00
2022-10-17 00:30:07 +03:00
2022-10-23 17:22:38 +03:00
# Filter users on join =========================================================================================================
2022-10-24 00:40:43 +03:00
@app.on_chat_member_updated(group=configGet("destination_group"))
#@app.on_message(filters.new_chat_members, group=configGet("destination_group"))
2022-10-23 17:22:38 +03:00
async def filter_join(app, member):
2022-10-24 00:40:43 +03:00
if member.invite_link != None:
2022-10-24 15:34:18 +03:00
if (path.exists(f"{configGet('data', 'locations')}{sep}users{sep}{member.from_user.id}.json") and jsonLoad(f"{configGet('data', 'locations')}{sep}users{sep}{member.from_user.id}.json")["approved"]) or (await isAnAdmin(member.from_user.id)):
2022-10-24 00:40:43 +03:00
if configGet("link", file=str(member.from_user.id)) == member.invite_link.invite_link:
return
2022-10-24 15:34:18 +03:00
if await isAnAdmin(member.invite_link.creator.id):
return
2022-10-24 00:40:43 +03:00
await app.send_message(configGet("admin_group"), f"User **{member.from_user.first_name}** (`{member.from_user.id}`) joined the chat not with his personal link", reply_markup=InlineKeyboardMarkup(
2022-10-23 20:20:33 +03:00
[
2022-10-24 00:40:43 +03:00
[
InlineKeyboardButton(text=str(locale("sus_allow", "button")), callback_data=f"sus_allow_{member.from_user.id}")
],
[
InlineKeyboardButton(text=str(locale("sus_refuse", "button")), callback_data=f"sus_refuse_{member.from_user.id}")
]
2022-10-23 20:20:33 +03:00
]
2022-10-24 00:40:43 +03:00
))
await app.restrict_chat_member(member.chat.id, member.from_user.id, permissions=ChatPermissions(
can_send_messages=False,
can_send_media_messages=False,
can_send_other_messages=False,
can_send_polls=False
)
2022-10-23 20:20:33 +03:00
)
2022-10-23 17:22:38 +03:00
# ==============================================================================================================================
2022-10-17 00:30:07 +03:00
if __name__ == "__main__":
logWrite(f"Starting up with pid {pid}")
# Yes, it should be in some kind of async main() function but I don't give a shit.
2022-10-20 13:24:32 +03:00
# I did compare performance, almost no difference and it's much more useful this way. Change my mind.
2022-10-17 00:30:07 +03:00
app.start() # type: ignore
2022-10-25 15:18:51 +03:00
if configGet("birthdays_notify"):
every().day.at(configGet("birthdays_time")).do(check_birthdays, app)
# Background tasks checker
def background_task():
try:
while True:
try:
run_pending()
#print('Checked')
time.sleep(1)
except:
pass
except KeyboardInterrupt:
print('\nShutting down')
killProc(pid)
t = Thread(target=background_task)
t.start()
2022-10-17 00:30:07 +03:00
app.send_message(configGet("owner"), f"Starting up with pid `{pid}`") # type: ignore
2022-10-25 14:36:16 +03:00
# Registering user commands
commands_list = []
for command in configGet("commands"):
commands_list.append(BotCommand(command, configGet("commands")[command]))
app.set_bot_commands(commands_list) # type: ignore
2022-10-17 00:30:07 +03:00
# Registering admin commands
commands_admin_list = []
2022-10-25 14:36:16 +03:00
for command in configGet("commands"):
commands_admin_list.append(BotCommand(command, configGet("commands")[command]))
2022-10-17 00:30:07 +03:00
for command in configGet("commands_admin"):
commands_admin_list.append(BotCommand(command, configGet("commands_admin")[command]))
for admin in configGet("admins"):
try:
app.set_bot_commands(commands_admin_list, scope=BotCommandScopeChat(chat_id=admin)) # type: ignore
except bad_request_400.PeerIdInvalid:
pass
2022-11-13 14:40:49 +02:00
2022-10-17 00:30:07 +03:00
app.set_bot_commands(commands_admin_list, scope=BotCommandScopeChat(chat_id=configGet("owner"))) # type: ignore
2022-11-13 14:40:49 +02:00
# Registering admin group commands
commands_group_admin_list = []
for command in configGet("commands_group_admin"):
commands_group_admin_list.append(BotCommand(command, configGet("commands_group_admin")[command]))
app.set_bot_commands(commands_group_admin_list, scope=BotCommandScopeChat(chat_id=configGet("admin_group"))) # type: ignore
# Registering destination group commands
commands_group_destination_list = []
for command in configGet("commands_group_destination"):
commands_group_destination_list.append(BotCommand(command, configGet("commands_group_destination")[command]))
app.set_bot_commands(commands_group_destination_list, scope=BotCommandScopeChat(chat_id=configGet("destination_group"))) # type: ignore
2022-10-17 00:30:07 +03:00
idle()
app.send_message(configGet("owner"), f"Shutting with pid `{pid}`") # type: ignore
app.stop() # type: ignore
killProc(pid)