Notification check now in User, add kfet API
This commit is contained in:
parent
3c0ebc25d9
commit
6a46224fd6
3 changed files with 93 additions and 14 deletions
34
EDTuser.py
34
EDTuser.py
|
@ -1,5 +1,12 @@
|
||||||
from EDTcalendar import Calendar
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import requests
|
||||||
|
from EDTcalendar import Calendar
|
||||||
|
|
||||||
|
KFET_URL = "https://kfet.bdeinfo.org/?page=api_commandes"
|
||||||
|
|
||||||
|
|
||||||
|
def get_now():
|
||||||
|
return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None)
|
||||||
|
|
||||||
|
|
||||||
class User:
|
class User:
|
||||||
|
@ -10,7 +17,30 @@ class User:
|
||||||
self.nt = False
|
self.nt = False
|
||||||
self.nt_time = 20
|
self.nt_time = 20
|
||||||
self.nt_cooldown = 20
|
self.nt_cooldown = 20
|
||||||
self.nt_last = datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None)
|
self.nt_last = get_now()
|
||||||
|
self.kfet = None
|
||||||
|
|
||||||
def calendar(self, time: str = "", pass_week: bool = False):
|
def calendar(self, time: str = "", pass_week: bool = False):
|
||||||
return Calendar(time, self.resources, pass_week=pass_week)
|
return Calendar(time, self.resources, pass_week=pass_week)
|
||||||
|
|
||||||
|
def get_notif(self):
|
||||||
|
if self.resources and self.nt:
|
||||||
|
now = get_now()
|
||||||
|
c = self.calendar(pass_week=False)
|
||||||
|
for e in c.timeline:
|
||||||
|
if 0 <= (e.begin - now).total_seconds().__abs__() // 60 <= self.nt_time and \
|
||||||
|
0 <= (now - self.nt_last).total_seconds() // 60 >= self.nt_cooldown:
|
||||||
|
self.nt_last = get_now()
|
||||||
|
return e
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_kfet(self):
|
||||||
|
res = None
|
||||||
|
if self.kfet:
|
||||||
|
cmds = requests.get(KFET_URL).json()
|
||||||
|
if str(self.kfet) in cmds:
|
||||||
|
res = 1 if cmds[str(self.kfet)]["statut"] == "T" else 2
|
||||||
|
elif get_now().hour >= 14:
|
||||||
|
res = 3
|
||||||
|
self.kfet = None if res else self.kfet
|
||||||
|
return res
|
||||||
|
|
|
@ -3,11 +3,18 @@
|
||||||
"help": "\u2139 *Commands help* \u2139\n\uD83D\uDCC5/edt, show your next events\n\uD83D\uDD14/notif, set your events notifications\n\u2699/setedt, set your calendar\n\uD83D\uDCE4\t/getedt to show your ressource",
|
"help": "\u2139 *Commands help* \u2139\n\uD83D\uDCC5/edt, show your next events\n\uD83D\uDD14/notif, set your events notifications\n\u2699/setedt, set your calendar\n\uD83D\uDCE4\t/getedt to show your ressource",
|
||||||
"edt_err_set": "Your EDT is not set ! \u274C\n\u2139Use /setedt to fix that",
|
"edt_err_set": "Your EDT is not set ! \u274C\n\u2139Use /setedt to fix that",
|
||||||
"edt_err_choice": "Invalid choice ! \u274C\n\u2139You can choose between: `day`, `next`, `week`, `next week`",
|
"edt_err_choice": "Invalid choice ! \u274C\n\u2139You can choose between: `day`, `next`, `week`, `next week`",
|
||||||
"setedt_err_res": "Invalid resources ! \u274C",
|
"setedt_err_res": "Invalid resources ! \u274C\n\u2139Put your resources number from a export url of your calendar",
|
||||||
"setedt": "EDT set \u2705",
|
"setedt": "EDT set \u2705",
|
||||||
"getedt_err": "No EDT set ! \u274C",
|
"getedt_err": "No EDT set ! \u274C",
|
||||||
|
"notif_event": "\uD83D\uDD14A event is coming !\n",
|
||||||
|
"kfet": "\uD83D\uDD14Your command is ready !",
|
||||||
|
"kfet_prb": "\u26A0Your command got a problem, go check whats happening",
|
||||||
|
"kfet_err": "\u274CYour order is not out",
|
||||||
|
"kfet_list": "\uD83D\uDDD2Kfet commands ready:\n",
|
||||||
|
"kfet_close": "\uD83D\uDEABKfet close, sorry",
|
||||||
|
"kfet_set": "Your command is set ! \u2705",
|
||||||
"notif_set": "Notifications set on `{}` ! \u2705",
|
"notif_set": "Notifications set on `{}` ! \u2705",
|
||||||
"notif_err_num": "Invalid number ! \u274C",
|
"err_num": "Invalid number ! \u274C",
|
||||||
"notif_time_cooldown": "Notification `{}` set to `{}` ! \u2705",
|
"notif_time_cooldown": "Notification `{}` set to `{}` ! \u2705",
|
||||||
"notif_info": "_Notification_\n*State:* {}\n*Time:* {}\n*Cooldown:* {}",
|
"notif_info": "_Notification_\n*State:* {}\n*Time:* {}\n*Cooldown:* {}",
|
||||||
"notif_help": "\u2139 *Notif help* \u2139\n\uD83D\uDD39`toggle` to switch on/off notifications\n\uD83D\uDD39`time` to set time in minutes between notification and event\n\uD83D\uDD39`cooldown` set time in minutes between notification",
|
"notif_help": "\u2139 *Notif help* \u2139\n\uD83D\uDD39`toggle` to switch on/off notifications\n\uD83D\uDD39`time` to set time in minutes between notification and event\n\uD83D\uDD39`cooldown` set time in minutes between notification",
|
||||||
|
|
62
bot.py
62
bot.py
|
@ -3,6 +3,7 @@ import datetime
|
||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
import shelve
|
import shelve
|
||||||
|
import requests
|
||||||
from asyncio import sleep
|
from asyncio import sleep
|
||||||
from os import mkdir
|
from os import mkdir
|
||||||
from os.path import isdir, isfile
|
from os.path import isdir, isfile
|
||||||
|
@ -13,7 +14,7 @@ from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResul
|
||||||
from aiogram.utils import markdown
|
from aiogram.utils import markdown
|
||||||
from aiogram.utils.exceptions import MessageIsTooLong
|
from aiogram.utils.exceptions import MessageIsTooLong
|
||||||
from EDTcalendar import Calendar
|
from EDTcalendar import Calendar
|
||||||
from EDTuser import User
|
from EDTuser import User, KFET_URL
|
||||||
from lang import lang
|
from lang import lang
|
||||||
from ics.parse import ParseError
|
from ics.parse import ParseError
|
||||||
from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema
|
from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema
|
||||||
|
@ -62,14 +63,18 @@ async def notif():
|
||||||
with dbL:
|
with dbL:
|
||||||
with shelve.open("edt", writeback=True) as db:
|
with shelve.open("edt", writeback=True) as db:
|
||||||
for u in db:
|
for u in db:
|
||||||
if db[u].resources and db[u].nt:
|
nt = db[u].get_notif()
|
||||||
now = get_now()
|
kf = db[u].get_kfet()
|
||||||
c = db[u].calendar(pass_week=True)
|
if nt:
|
||||||
for e in c.timeline:
|
await bot.send_message(int(u), lang(db[u], "notif_event")+nt, parse_mode=ParseMode.MARKDOWN)
|
||||||
if 0 <= (e.begin - now).total_seconds().__abs__()//60 <= db[u].nt_time and \
|
if kf is not None:
|
||||||
0 <= (now - db[u].nt_last).total_seconds()//60 >= db[u].nt_cooldown:
|
if kf == 1:
|
||||||
db[u].nt_last = get_now()
|
kf = lang(db[u], "kfet")
|
||||||
await bot.send_message(int(u), e, parse_mode=ParseMode.MARKDOWN)
|
elif kf == 2:
|
||||||
|
kf = lang(db[u], "kfet_prb")
|
||||||
|
else:
|
||||||
|
kf = lang(db[u], "kfet_err")
|
||||||
|
await bot.send_message(int(u), kf, parse_mode=ParseMode.MARKDOWN)
|
||||||
await sleep(60)
|
await sleep(60)
|
||||||
|
|
||||||
|
|
||||||
|
@ -127,6 +132,43 @@ async def edt_cmd(message: types.Message):
|
||||||
await message.reply(resp, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
await message.reply(resp, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
||||||
|
|
||||||
|
|
||||||
|
@dp.message_handler(commands="kfet")
|
||||||
|
async def edt_cmd(message: types.Message):
|
||||||
|
user_id = str(message.from_user.id)
|
||||||
|
await message.chat.do(types.ChatActions.TYPING)
|
||||||
|
logger.info(f"{message.from_user.username} do kfet command: {message.text}")
|
||||||
|
with dbL:
|
||||||
|
with shelve.open("edt", writeback=True) as db:
|
||||||
|
if not 9 < get_now().hour < 14 or not get_now().isoweekday() < 5:
|
||||||
|
msg = lang(db[user_id], "kfet_close")
|
||||||
|
else:
|
||||||
|
msg = lang(db[user_id], "kfet_list")
|
||||||
|
cmds = requests.get(KFET_URL).json()
|
||||||
|
for c in cmds:
|
||||||
|
msg += markdown.code(c) + " " if cmds[c]["statut"] == "T" else ""
|
||||||
|
await message.reply(msg, parse_mode=ParseMode.MARKDOWN)
|
||||||
|
|
||||||
|
|
||||||
|
@dp.message_handler(commands="setkfet")
|
||||||
|
async def edt_cmd(message: types.Message):
|
||||||
|
user_id = str(message.from_user.id)
|
||||||
|
await message.chat.do(types.ChatActions.TYPING)
|
||||||
|
logger.info(f"{message.from_user.username} do setkfet command: {message.text}")
|
||||||
|
with dbL:
|
||||||
|
with shelve.open("edt", writeback=True) as db:
|
||||||
|
if not 9 < get_now().hour < 14 or not get_now().isoweekday() < 5:
|
||||||
|
msg = lang(db[user_id], "kfet_close")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
int(message.text[9:])
|
||||||
|
except ValueError:
|
||||||
|
msg = lang(db[user_id], "err_num")
|
||||||
|
else:
|
||||||
|
db[user_id].kfet = int(message.text[9:])
|
||||||
|
msg = lang(db[user_id], "kfet_set")
|
||||||
|
await message.reply(msg, parse_mode=ParseMode.MARKDOWN)
|
||||||
|
|
||||||
|
|
||||||
@dp.message_handler(commands="setedt")
|
@dp.message_handler(commands="setedt")
|
||||||
async def edt_set(message: types.Message):
|
async def edt_set(message: types.Message):
|
||||||
user_id = str(message.from_user.id)
|
user_id = str(message.from_user.id)
|
||||||
|
@ -181,7 +223,7 @@ async def notif_cmd(message: types.Message):
|
||||||
try:
|
try:
|
||||||
int(message.text[cut+1:])
|
int(message.text[cut+1:])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
msg = lang(db[user_id], "notif_err_num")
|
msg = lang(db[user_id], "err_num")
|
||||||
else:
|
else:
|
||||||
if cut == 11:
|
if cut == 11:
|
||||||
db[user_id].nt_time = int(message.text[cut+1:])
|
db[user_id].nt_time = int(message.text[cut+1:])
|
||||||
|
|
Reference in a new issue