Add of Markdown, keyboard and some fix
This commit is contained in:
parent
ea29352b29
commit
87e138562f
1 changed files with 44 additions and 26 deletions
70
bot.py
70
bot.py
|
@ -1,6 +1,7 @@
|
|||
import logging, shelve, datetime, hashlib
|
||||
from aiogram import Bot, Dispatcher, executor, types
|
||||
from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle
|
||||
from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle, ParseMode, reply_keyboard
|
||||
from aiogram.utils import markdown
|
||||
from ics import Calendar
|
||||
from ics.parse import ParseError
|
||||
from requests import get
|
||||
|
@ -18,52 +19,67 @@ def edt(text, user_id):
|
|||
with dbL:
|
||||
with shelve.open("edt") as db:
|
||||
if not str(user_id) in db:
|
||||
return "Your EDT is not set !"
|
||||
return markdown.bold("Your EDT is not set !")
|
||||
now = datetime.datetime.now()
|
||||
if text == "week":
|
||||
if text.lower() == "week":
|
||||
firstdate = now.date() - datetime.timedelta(days=now.isoweekday()-1)
|
||||
lastdate = now.date() + datetime.timedelta(days=(7 - now.isoweekday()))
|
||||
elif text == "next":
|
||||
elif text.lower() == "next":
|
||||
now += datetime.timedelta(days=7)
|
||||
firstdate = now.date() - datetime.timedelta(days=now.isoweekday())
|
||||
lastdate = now.date() + datetime.timedelta(days=(7 - now.isoweekday()))
|
||||
elif text == "":
|
||||
firstdate, lastdate = now, now
|
||||
elif text == "" or text.lower() == "day":
|
||||
firstdate, lastdate = now.date(), now.date()
|
||||
else:
|
||||
return "Invalid choice !"
|
||||
return markdown.bold("Invalid choice !")
|
||||
|
||||
url = f"{db[str(user_id)]}&firstDate={firstdate}&lastDate={lastdate}"
|
||||
c = Calendar(get(url).text)
|
||||
msg = str()
|
||||
msg = list()
|
||||
days = list()
|
||||
|
||||
for e in list(c.timeline):
|
||||
begin = e.begin.datetime
|
||||
end = e.end.datetime
|
||||
if str(begin.date())[5:] not in msg:
|
||||
msg += f"<{str(begin.date())[5:]}>" + "\n"
|
||||
if begin.date() not in days:
|
||||
days.append(begin.date())
|
||||
msg.append(markdown.bold(f"<{str(begin.date())[5:]}>"))
|
||||
|
||||
msg += f"[{e.name}]:" + "\n"
|
||||
msg += f"{str(begin.time())[:-3]} -> {str(end.time())[:-3]}" + "\n"
|
||||
prof = e.description.split('\n')[3]
|
||||
msg += f"{e.location} {prof}" + "\n\n"
|
||||
msg.append(markdown.code(f"📓[{e.name}]:"))
|
||||
msg.append(markdown.text(f"⌚️ {str(begin.time())[:-3]} -> {str(end.time())[:-3]}"))
|
||||
prof = markdown.text(e.description.split('\n')[3])
|
||||
msg.append(markdown.italic(f"📍 {e.location} 👨🏫 {prof}" + "\n"))
|
||||
|
||||
if msg == "":
|
||||
msg = "but nobody came..."
|
||||
return msg
|
||||
if len(msg) == 0:
|
||||
msg.append(markdown.italic("but nobody came..."))
|
||||
return markdown.text(*msg, sep="\n")
|
||||
|
||||
|
||||
@dp.message_handler(commands=["start", "help"])
|
||||
async def send_welcome(message: types.Message):
|
||||
await message.reply("""Welcome to the TelegramEDT, a calendar bot for the Lyon 1 University !
|
||||
/edt [week | next], for show your next course
|
||||
/setedt <url>, to setup your calendar
|
||||
/getedt, to get your calendar url
|
||||
/help, to show this command""")
|
||||
msg = markdown.text(
|
||||
markdown.text("💠 Welcome to the TelegramEDT, a calendar bot for the Lyon 1 University ! 💠\n"),
|
||||
markdown.text(markdown.code("/edt [day | week | next]"), markdown.text(", for show your next course")),
|
||||
markdown.text(markdown.code("/setedt <url>"), markdown.text(", to setup your calendar")),
|
||||
markdown.text(markdown.code("/getedt"), markdown.text(", to get your calendar url")),
|
||||
markdown.text(markdown.code("/help"), markdown.text(", to show this command")),
|
||||
sep="\n"
|
||||
)
|
||||
await message.reply(msg, parse_mode=ParseMode.MARKDOWN)
|
||||
|
||||
|
||||
@dp.message_handler(commands=["edt"])
|
||||
@dp.message_handler(lambda msg: msg.text.lower() in ["day", "week", "next"])
|
||||
async def edt_cmd(message: types.Message):
|
||||
await message.reply(edt(message.text[5:], message.from_user.id))
|
||||
text = message.text
|
||||
if message.text[:4] == "/edt":
|
||||
text = message.text[5:]
|
||||
resp = edt(text, message.from_user.id)
|
||||
key = reply_keyboard.ReplyKeyboardMarkup()
|
||||
key.add(reply_keyboard.KeyboardButton("Day"))
|
||||
key.add(reply_keyboard.KeyboardButton("Week"))
|
||||
key.add(reply_keyboard.KeyboardButton("Next"))
|
||||
await message.reply(resp, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
||||
|
||||
|
||||
@dp.inline_handler()
|
||||
|
@ -79,7 +95,6 @@ async def inline_echo(inline_query: InlineQuery):
|
|||
title=f"Your {text} course",
|
||||
input_message_content=input_content
|
||||
)
|
||||
# don't forget to set cache_time=1 for testing (default is 300s or 5m)
|
||||
await bot.answer_inline_query(inline_query.id, results=[item], cache_time=1)
|
||||
|
||||
|
||||
|
@ -87,6 +102,8 @@ async def inline_echo(inline_query: InlineQuery):
|
|||
async def edt_set(message: types.Message):
|
||||
url_http = message.text.find("http")
|
||||
url_end = message.text.find(" ", url_http)
|
||||
if url_end == -1:
|
||||
url_end = None
|
||||
url = message.text[url_http:url_end]
|
||||
|
||||
try:
|
||||
|
@ -106,7 +123,7 @@ async def edt_set(message: types.Message):
|
|||
await message.reply("EDT set !")
|
||||
|
||||
|
||||
@dp.message_handler(commands=["getedturl"])
|
||||
@dp.message_handler(commands=["getedt"])
|
||||
async def edt_geturl(message: types.Message):
|
||||
with dbL:
|
||||
with shelve.open("edt") as db:
|
||||
|
@ -115,5 +132,6 @@ async def edt_geturl(message: types.Message):
|
|||
else:
|
||||
await message.reply("No EDT set !")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
executor.start_polling(dp, skip_updates=True)
|
||||
executor.start_polling(dp, skip_updates=True)
|
||||
|
|
Reference in a new issue