1
0
Fork 0

Separate await_cmd for eah module

This commit is contained in:
Ethanell 2019-12-26 14:42:34 +01:00
parent cd5484ef88
commit e58b04f7d8
6 changed files with 139 additions and 110 deletions

View file

@ -69,6 +69,6 @@ dp.register_message_handler(load_cmd, commands="load")
dp.register_message_handler(unload_cmd, commands="unload") dp.register_message_handler(unload_cmd, commands="unload")
logger.info("Start loading modules") logger.info("Start loading modules")
for m in ["basic", "edt", "kfet", "tomuss", "notif", "await_cmd", "tools"]: for m in ["basic", "edt", "kfet", "tomuss", "notif", "tools"]:
load_module(m) load_module(m)
logger.info("Modules loading finish") logger.info("Modules loading finish")

View file

@ -1,105 +0,0 @@
import re
import requests
from PIL import Image
from aiogram import types
from aiogram.types import ParseMode, ContentType
from feedparser import parse
from ics.parse import ParseError
from pyzbar.pyzbar import decode
from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema
from TelegramEDT import API_TOKEN, bot, dbL, dp, key, logger, session, check_id
from TelegramEDT.EDTcalendar import Calendar
from TelegramEDT.base import User
from TelegramEDT.lang import lang
re_url = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")
logger = logger.getChild("await_cmd")
def have_await_cmd(msg: types.Message):
with dbL:
user = session.query(User).filter_by(id=msg.from_user.id).first()
return user and user.await_cmd
async def await_cmd(message: types.message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING)
msg = None
with dbL:
user = session.query(User).filter_by(id=message.from_user.id).first()
logger.info(f"{message.from_user.username} do awaited commande: {user.await_cmd}")
if user.await_cmd == "setedt":
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:
Calendar("", int(resources))
except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError, UnboundLocalError):
msg = lang(user, "setedt_err_res")
else:
user.resources = int(resources)
msg = lang(user, "setedt")
elif user.await_cmd == "setkfet":
try:
int(message.text)
except ValueError:
msg = lang(user, "err_num")
else:
user.kfet = int(message.text)
msg = lang(user, "kfet_set")
elif user.await_cmd == "settomuss":
if not len(parse(message.text).entries):
msg = lang(user, "settomuss_error")
else:
user.tomuss_rss = message.text
msg = lang(user, "settomuss")
elif user.await_cmd in ["time", "cooldown"]:
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[6:], value)
if user.await_cmd:
user.await_cmd = str()
session.commit()
if msg:
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
def load():
logger.info("Load await_cmd module")
dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg),content_types=[ContentType.TEXT,
ContentType.PHOTO])
def unload():
logger.info("Unload await_cmd module")
dp.message_handlers.unregister(await_cmd)

View file

@ -1,14 +1,23 @@
import hashlib import hashlib
import re
import requests
from PIL import Image
from aiogram import types from aiogram import types
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode, InputTextMessageContent, \ from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InputTextMessageContent, \
InlineQueryResultArticle, InlineQuery InlineQueryResultArticle, InlineQuery, ContentType
from aiogram.types import ParseMode
from ics.parse import ParseError
from pyzbar.pyzbar import decode
from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema
from TelegramEDT import dbL, dp, key, logger, posts_cb, session, TIMES, bot, check_id from TelegramEDT import API_TOKEN, TIMES, bot, dbL, dp, key, logger, session, check_id, posts_cb
from TelegramEDT.EDTcalendar import Calendar
from TelegramEDT.base import User from TelegramEDT.base import User
from TelegramEDT.lang import lang from TelegramEDT.lang import lang
logger = logger.getChild("edt") logger = logger.getChild("edt")
re_url = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")
def calendar(time: str, user_id: int): def calendar(time: str, user_id: int):
@ -28,6 +37,12 @@ def edt_key():
return keys return keys
def have_await_cmd(msg: types.Message):
with dbL:
user = session.query(User).filter_by(id=msg.from_user.id).first()
return user and user.await_cmd == "setedt"
async def edt_cmd(message: types.Message): async def edt_cmd(message: types.Message):
check_id(message.from_user) check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
@ -69,6 +84,41 @@ async def edt_await(message: types.Message):
await message.reply(lang(user, "setedt_wait"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) await message.reply(lang(user, "setedt_wait"), 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 dbL:
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:
Calendar("", int(resources))
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)
async def edt_geturl(message: types.Message): async def edt_geturl(message: types.Message):
check_id(message.from_user) check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
@ -87,6 +137,8 @@ def load():
dp.register_inline_handler(inline_edt) dp.register_inline_handler(inline_edt)
dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"])) dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"]))
dp.register_message_handler(edt_await, lambda msg: msg.text.lower() == "setedt") dp.register_message_handler(edt_await, lambda msg: msg.text.lower() == "setedt")
dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg), content_types=[ContentType.TEXT,
ContentType.PHOTO])
dp.register_message_handler(edt_geturl, commands="getedt") dp.register_message_handler(edt_geturl, commands="getedt")
@ -96,4 +148,5 @@ def unload():
dp.inline_query_handlers.unregister(inline_edt) dp.inline_query_handlers.unregister(inline_edt)
dp.callback_query_handlers.unregister(edt_query) dp.callback_query_handlers.unregister(edt_query)
dp.message_handlers.unregister(edt_await) dp.message_handlers.unregister(edt_await)
dp.message_handlers.unregister(await_cmd)
dp.message_handlers.unregister(edt_geturl) dp.message_handlers.unregister(edt_geturl)

View file

@ -16,6 +16,12 @@ def get_now():
return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None) return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None)
def have_await_cmd(msg: types.Message):
with dbL:
user = session.query(User).filter_by(id=msg.from_user.id).first()
return user and user.await_cmd == "setkfet"
async def kfet(message: types.Message): async def kfet(message: types.Message):
check_id(message.from_user) check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
@ -49,13 +55,33 @@ async def kfet_set(message: types.Message):
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) 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 dbL:
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)
def load(): def load():
logger.info("Load kfet module") logger.info("Load kfet module")
dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet") dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet")
dp.register_message_handler(kfet_set, lambda msg: msg.text.lower() == "setkfet") dp.register_message_handler(kfet_set, lambda msg: msg.text.lower() == "setkfet")
dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg))
def unload(): def unload():
logger.info("Unload kfet module") logger.info("Unload kfet module")
dp.message_handlers.unregister(kfet) dp.message_handlers.unregister(kfet)
dp.message_handlers.unregister(kfet_set) dp.message_handlers.unregister(kfet_set)
dp.message_handlers.unregister(await_cmd)

View file

@ -4,13 +4,19 @@ from aiogram import types
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode
from aiogram.utils import markdown from aiogram.utils import markdown
from TelegramEDT import bot, dbL, dp, logger, posts_cb, session, check_id from TelegramEDT import bot, dbL, dp, logger, posts_cb, session, check_id, key
from TelegramEDT.base import User from TelegramEDT.base import User
from TelegramEDT.lang import lang from TelegramEDT.lang import lang
logger = logger.getChild("notif") logger = logger.getChild("notif")
def have_await_cmd(msg: types.Message):
with dbL:
user = session.query(User).filter_by(id=msg.from_user.id).first()
return user and user.await_cmd in ["time", "cooldown"]
async def notif(): async def notif():
while True: while True:
with dbL: with dbL:
@ -85,13 +91,37 @@ async def notif_query(query: types.CallbackQuery, callback_data: dict):
await query.message.reply(msg, parse_mode=ParseMode.MARKDOWN) await query.message.reply(msg, parse_mode=ParseMode.MARKDOWN)
async def await_cmd(message: types.message):
check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING)
with dbL:
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[6:], value)
user.await_cmd = str()
session.commit()
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
def load(): def load():
logger.info("Load notif module") logger.info("Load notif module")
dp.register_message_handler(notif_cmd, lambda msg: msg.text.lower() == "notif") dp.register_message_handler(notif_cmd, lambda msg: msg.text.lower() == "notif")
dp.register_callback_query_handler(notif_query, posts_cb.filter(action=["toggle", "time", "cooldown"])) dp.register_callback_query_handler(notif_query, posts_cb.filter(action=["toggle", "time", "cooldown"]))
dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg))
def unload(): def unload():
logger.info("Unload notif module") logger.info("Unload notif module")
dp.message_handlers.unregister(notif_cmd) dp.message_handlers.unregister(notif_cmd)
dp.callback_query_handlers.unregister(notif_query) dp.callback_query_handlers.unregister(notif_query)
dp.message_handlers.unregister(await_cmd)

View file

@ -1,5 +1,6 @@
from aiogram import types from aiogram import types
from aiogram.types import ParseMode from aiogram.types import ParseMode
from feedparser import parse
from TelegramEDT import dbL, dp, key, logger, session, check_id from TelegramEDT import dbL, dp, key, logger, session, check_id
from TelegramEDT.base import User from TelegramEDT.base import User
@ -8,6 +9,12 @@ from TelegramEDT.lang import lang
logger = logger.getChild("tomuss") logger = logger.getChild("tomuss")
def have_await_cmd(msg: types.Message):
with dbL:
user = session.query(User).filter_by(id=msg.from_user.id).first()
return user and user.await_cmd == "settomuss"
async def settomuss(message: types.Message): async def settomuss(message: types.Message):
check_id(message.from_user) check_id(message.from_user)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
@ -20,11 +27,29 @@ async def settomuss(message: types.Message):
await message.reply(lang(user, "settomuss_wait"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) await message.reply(lang(user, "settomuss_wait"), 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 dbL:
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
msg = lang(user, "settomuss")
user.await_cmd = str()
session.commit()
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
def load(): def load():
logger.info("Load tomuss module") logger.info("Load tomuss module")
dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss") dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss")
dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg))
def unload(): def unload():
logger.info("Unload tomuss module") logger.info("Unload tomuss module")
dp.message_handlers.unregister(settomuss) dp.message_handlers.unregister(settomuss)
dp.message_handlers.unregister(await_cmd)