async def await_cmd(message: types.message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() logger.info(f"{message.from_user.username} do edt awaited command") url = str() if message.photo: file_path = await bot.get_file(message.photo[0].file_id) file_url = f"https://api.telegram.org/file/bot{API_TOKEN}/{file_path['file_path']}" qr = decode(Image.open(requests.get(file_url, stream=True).raw)) if qr: url = str(qr[0].data) elif message.text: msg_url = re_url.findall(message.text) if msg_url: url = msg_url[0] if url: resources = url[url.find("resources") + 10:][:4] elif message.text: resources = message.text try: string_to_container(requests.get(Calendar("", int(resources)).url).text) except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError, UnboundLocalError): msg = lang(user, "setedt_err_res") else: user.resources = int(resources) msg = lang(user, "setedt") user.await_cmd = str() session.commit() await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
def calendar(time: str, user_id: int): with Session as session: user = session.query(User).filter_by(id=user_id).first() if not user.resources: return lang(user, "edt_err_set") elif time not in TIMES: return lang(user, "edt_err_choice") return str(user.calendar(time))
async def kfet_set(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) logger.info(f"{message.from_user.username} do setkfet") with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() if not 9 < get_now().hour < 14 or not get_now().isoweekday() < 5: msg = lang(user, "kfet_close") else: user.await_cmd = "setkfet" msg = lang(user, "kfet_set_await") session.commit() await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
async def await_cmd(message: types.message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() logger.info(f"{message.from_user.username} do awaited command") if not len(parse(message.text).entries): msg = lang(user, "settomuss_error") else: user.tomuss_rss = message.text user.tomuss_last = str() msg = lang(user, "settomuss") user.await_cmd = str() session.commit() await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
async def await_cmd(message: types.message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() logger.info(f"{message.from_user.username} do awaited command") try: int(message.text) except ValueError: msg = lang(user, "err_num") else: user.kfet = int(message.text) msg = lang(user, "kfet_set") user.await_cmd = str() session.commit() await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
async def help_cmd(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) logger.info(f"{message.from_user.username} do help command") with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() msg = lang(user, "help") await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
async def notif(): with Session as session: for u in session.query(User).all(): try: kf = u.get_kfet() except Exception as e: logger.error(e) else: if kf is not None: if kf == 1: kf = lang(u, "kfet") elif kf == 2: kf = lang(u, "kfet_prb") else: kf = lang(u, "kfet_err") await bot.send_message(u.id, kf, parse_mode=ParseMode.MARKDOWN) session.commit()
async def await_cmd(message: types.message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() logger.info(f"{message.from_user.username} do awaited command") try: value = int(message.text) except ValueError: msg = lang(user, "err_num") else: if user.await_cmd == "time": user.nt_time = value else: user.nt_cooldown = value msg = lang(user, "notif_time_cooldown").format(user.await_cmd, value) user.await_cmd = str() session.commit() await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
async def notif_query(query: types.CallbackQuery, callback_data: dict): check_id(query.message.from_user) await query.message.chat.do(types.ChatActions.TYPING) logger.info(f"{query.message.from_user.username} do notif query") with Session as session: user = session.query(User).filter_by(id=query.from_user.id).first() if callback_data["action"] == "toggle": if user.nt: res = False else: res = True user.nt = res msg = lang(user, "notif_set").format(res) elif callback_data["action"] in ["time", "cooldown"]: user.await_cmd = callback_data["action"] msg = lang(user, "notif_await") session.commit() await query.message.reply(msg, parse_mode=ParseMode.MARKDOWN)
async def edt_geturl(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) logger.info(f"{message.from_user.username} do getedt command") with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() if user.resources: msg = user.resources else: msg = lang(user, "getedt_err") await message.reply(msg, reply_markup=key)
async def kfet(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) logger.info(f"{message.from_user.username} do kfet") with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() if not 9 < get_now().hour < 14 or not get_now().isoweekday() < 6: msg = lang(user, "kfet_close") else: msg = lang(user, "kfet_list") try: cmds = requests.get(KFET_URL).json() except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout): msg = markdown.bold(lang(user, "kfet_error")) else: if cmds: for c in cmds: msg += markdown.code( c) + " " if cmds[c] == "ok" else "" await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
async def notif(): with Session as session: for u in session.query(User).all(): try: nt = u.get_notif() except Exception as e: logger.error(e) else: if nt: await bot.send_message(u.id, lang(u, "notif_event") + str(nt), parse_mode=ParseMode.MARKDOWN) session.commit()
async def notif_cmd(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) logger.info(f"{message.from_user.username} do notif") keys = InlineKeyboardMarkup() for i, n in enumerate(["Toggle", "Time", "Cooldown"]): keys.add( InlineKeyboardButton(n, callback_data=posts_cb.new(id=i, action=n.lower()))) with Session as session: user = session.query(User).filter_by(id=message.from_user.id).first() msg = lang(user, "notif_info").format(user.nt, user.nt_time, user.nt_cooldown) await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=keys)