# -*- coding: utf-8 -*- from os import getpid, system from subprocess import call from pyrogram import filters from pyrogram.client import Client from pyrogram.sync import idle from pyrogram.types import ( ForceReply, BotCommand, BotCommandScopeChat, Message, ReplyKeyboardRemove, ) from pyrogram.enums.chat_action import ChatAction from convopyro import Conversation, listen_message from functions import * from modules.colors import * from modules.bwt import * config = jsonLoad("config.json") owner_id = config["owner_id"] app = Client( config["bot_name"], api_id=config["api_id"], api_hash=config["api_hash"], bot_token=config["bot_token"], ) Conversation(app) @app.on_message( ~filters.scheduled & filters.command(["setcard", "задать карту"], prefixes=["/", ""]) ) async def setcard(_: Client, msg: Message): await msg.reply_text( string("send_number"), reply_markup=ForceReply(placeholder=string("enter_number")), ) answer = await listen_message(_, msg.chat.id, timeout=None) if answer is None: return elif answer.text.strip() in ["/cancel", "cancel", "/відміна", "відміна"]: await msg.reply_text(string("cancel"), reply_markup=ReplyKeyboardRemove()) return userSet(answer.from_user.id, "card", answer.text) appendLog(f"User {str(msg.from_user.id)} set card id to {answer.text}") await msg.reply_text( string("card_linked").format(answer.text), reply_markup=ReplyKeyboardRemove() ) @app.on_message( ~filters.scheduled & filters.command(["resetcard", "забути картку"], prefixes=["/", ""]) ) async def resetcard(_: Client, msg: Message): if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]: userReset(msg.from_user.id, "card") await msg.reply_text(string("card_unlinked")) appendLog(f"User {str(msg.from_user.id)} reseted his card") else: await msg.reply_text(string("card_not_linked").format(string("get_number"))) appendLog(f"User {str(msg.from_user.id)} tried to reset non-existent card") @app.on_message( ~filters.scheduled & filters.command(["balance", "баланс"], prefixes=["/", ""]) ) async def balance(_: Client, msg: Message): try: if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]: await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING) water_left = await getWaterLeft( userGet(msg.from_user.id, "card"), msg.from_user.id, app ) if water_left == "": await msg.reply_text( string("error_new").format( f'https://bwtaqua.com.ua/card-topup/?id={userGet(msg.from_user.id, "card")}' ) ) # raise EmptyCardException("Card information is empty") elif water_left == "Failure": await msg.reply_text( string("error_occured").format(string("get_number")) ) appendLog( f"User {str(msg.from_user.id)} could not get left water amount" ) else: await msg.reply_text(string("card_balance").format(water_left)) appendLog( f"User {str(msg.from_user.id)} has {water_left} liters remaining" ) else: await msg.reply_text(string("card_not_linked").format(string("get_number"))) appendLog( f"User {str(msg.from_user.id)} tried to get balance without card set" ) except Exception as exp: if msg.from_user.id != config["owner_id"]: await msg.reply_text(string("error_occured").format(string("get_number"))) await app.send_message( owner_id, f"Error occured by {str(msg.from_user.id)}:\nException: `{exp}`\nTraceback: `{format_exc()}`", ) appendLog(f"User {str(msg.from_user.id)} could not get left water amount") @app.on_message( ~filters.scheduled & filters.command(["topup", "refill", "поповнити"], prefixes=["/", ""]) ) async def topup_cmd(_: Client, msg: Message): try: if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]: await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING) await msg.reply_text( string("top_up").format(str(userGet(msg.from_user.id, "card"))) ) appendLog(f"User {str(msg.from_user.id)} requested top up") else: await msg.reply_text(string("card_not_linked").format(string("get_number"))) appendLog( f"User {str(msg.from_user.id)} tried to request top up without card set" ) except Exception as exp: await msg.reply_text(str(exp)) @app.on_message( ~filters.scheduled & filters.command(["start", "help", "допомога"], prefixes=["/", ""]) ) async def help(_: Client, msg: Message): await msg.reply_text(string("welcome").format(string("get_number"))) if msg.from_user.language_code in jsonLoad("strings.json"): userSet(msg.from_user.id, "locale", msg.from_user.language_code) else: userSet(msg.from_user.id, "locale", "en") pid = getpid() @app.on_message( ~filters.scheduled & filters.command(["kill", "die", "shutdown"], prefixes="/") ) async def kill(_: Client, msg: Message): if msg.from_user.id == owner_id: await msg.reply_text(f"Shutting down bot with pid **{pid}**") system(f"kill -9 {pid}") print(f"{nowtime()} {WHITE}Starting with PID {YELLOW}{pid}{RESET}") app.start() # type: ignore app.send_message(owner_id, f"Starting bot with pid **{pid}**") # type: ignore app.set_bot_commands( [ BotCommand("help", "Меню допомоги"), BotCommand("balance", "Баланс картки"), BotCommand("topup", "Поповнити картку"), BotCommand("setcard", "Прив'язати картку"), BotCommand("resetcard", "Відв'язати картку"), ], language_code="uk", ) # type: ignore app.set_bot_commands( [ BotCommand("help", "Меню допомоги"), BotCommand("balance", "Баланс картки"), BotCommand("topup", "Поповнити картку"), BotCommand("setcard", "Прив'язати картку"), BotCommand("resetcard", "Відв'язати картку"), ], language_code="ru", ) # type: ignore app.set_bot_commands( [ BotCommand("help", "Help menu"), BotCommand("balance", "Card's balance"), BotCommand("topup", "Refill card"), BotCommand("setcard", "Link card"), BotCommand("resetcard", "Unlink card"), ] ) # type: ignore app.set_bot_commands( [ BotCommand("help", "Help menu"), BotCommand("balance", "Card's balance"), BotCommand("topup", "Refill card"), BotCommand("setcard", "Link card"), BotCommand("resetcard", "Unlink card"), BotCommand("shutdown", "Turn off the bot"), ], scope=BotCommandScopeChat(chat_id=owner_id), ) # type: ignore idle() app.send_message(owner_id, f"Shutting down bot with pid **{pid}**") # type: ignore print(f"\n{nowtime()} {WHITE}Shutting down with PID {YELLOW}{pid}{RESET}") call(f"kill -9 {pid}", shell=True)