Added function addTask (by Arnau/Obscurs)
Added string generator function to Task Added addTask compatibility with optional args Added some exception handling to addTask
This commit is contained in:
parent
c06cbc71c4
commit
abf9f03e88
3 changed files with 115 additions and 3 deletions
37
chat.py
37
chat.py
|
@ -1,5 +1,38 @@
|
||||||
|
from task import *
|
||||||
class Chat(object):
|
class Chat(object):
|
||||||
def __init__(self, ident):
|
def __init__(self, ident, name):
|
||||||
self.id = ident
|
self.id = ident
|
||||||
self.tasks = []
|
self.tasks = []
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
def addTask(self, name, repeat=0, hour=0, day=0, month=0, year=0):
|
||||||
|
t = Task(name, repeat, hour, day, month, year)
|
||||||
|
self.tasks.append(t)
|
||||||
|
print('task ' + self.tasks[-1].name + ' added')
|
||||||
|
return t
|
||||||
|
|
||||||
|
def getTasks(self, y=0, d=0, m=0, y2=0, d2=0, m2=0):
|
||||||
|
dateNow = datetime.now()
|
||||||
|
if y == 0:
|
||||||
|
y = dateNow.year
|
||||||
|
if m == 0:
|
||||||
|
m = dateNow.month
|
||||||
|
if d == 0:
|
||||||
|
d = dateNow.day
|
||||||
|
if y2 == 0:
|
||||||
|
y2 = dateNow.year
|
||||||
|
if m2 == 0:
|
||||||
|
m2 = dateNow.month
|
||||||
|
if d2 == 0:
|
||||||
|
d2 = dateNow.day
|
||||||
|
dateIni = datetime(y, m, d)
|
||||||
|
dateEnd = datetime(y2, m2, d2)
|
||||||
|
resultTasks = []
|
||||||
|
for t in self.tasks:
|
||||||
|
print(t)
|
||||||
|
if t.getDate() >= dateIni and t.getDate() <= dateEnd:
|
||||||
|
resultTasks.append(t)
|
||||||
|
return resultTasks
|
||||||
|
|
||||||
|
def getChatName(self):
|
||||||
|
return self.name
|
||||||
|
|
69
main.py
69
main.py
|
@ -1,14 +1,79 @@
|
||||||
|
|
||||||
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
||||||
from telegram.error import *
|
from telegram.error import *
|
||||||
|
from chat import *
|
||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
|
|
||||||
|
chats = {}
|
||||||
|
|
||||||
|
# Enable logging
|
||||||
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||||
|
level=logging.INFO)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def get_chatname(chat):
|
||||||
|
if chat.title is not None:
|
||||||
|
return chat.title
|
||||||
|
elif chat.first_name is not None:
|
||||||
|
if chat.last_name is not None:
|
||||||
|
return chat.first_name + " " + chat.last_name
|
||||||
|
else:
|
||||||
|
return chat.first_name
|
||||||
|
else:
|
||||||
|
return ""
|
||||||
def error(bot, update, error):
|
def error(bot, update, error):
|
||||||
logger.warn('Update "%s" caused error "%s"' % (update, error))
|
logger.warn('Update "%s" caused error "%s"' % (update, error))
|
||||||
|
|
||||||
def start(bot, update):
|
def start(bot, update):
|
||||||
update.message.reply_text('Hello, World!')
|
update.message.reply_text('Hello, World!!')
|
||||||
|
|
||||||
|
def addTask(bot, update,args):
|
||||||
|
global chats
|
||||||
|
tchat = update.message.chat
|
||||||
|
ident = str(tchat.id)
|
||||||
|
if not ident in chats:
|
||||||
|
title = get_chatname(tchat)
|
||||||
|
chat = Chat(tchat.id, title)
|
||||||
|
else:
|
||||||
|
chat = chats[ident]
|
||||||
|
try:
|
||||||
|
if len(args) > 5:
|
||||||
|
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]),int(args[4]),int(args[5]))
|
||||||
|
elif len(args) > 4:
|
||||||
|
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]),int(args[4]))
|
||||||
|
elif len(args) > 3:
|
||||||
|
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]))
|
||||||
|
elif len(args) > 2:
|
||||||
|
t = chat.addTask(args[0],args[1],int(args[2]))
|
||||||
|
elif len(args) > 1:
|
||||||
|
t = chat.addTask(args[0],args[1])
|
||||||
|
else:
|
||||||
|
t = chat.addTask(args[0])
|
||||||
|
update.message.reply_text('Task ' + t.name + ' added')
|
||||||
|
chats[ident] = chat
|
||||||
|
except:
|
||||||
|
update.message.reply_text('Wrong format')
|
||||||
|
|
||||||
|
def getTasksToday(bot,update):
|
||||||
|
global chats
|
||||||
|
tchat = update.message.chat
|
||||||
|
ident = str(tchat.id)
|
||||||
|
if not ident in chats:
|
||||||
|
title = get_chatname(tchat)
|
||||||
|
chat = Chat(tchat.id, title)
|
||||||
|
else:
|
||||||
|
chat = chats[ident]
|
||||||
|
update.message.reply_text('showing today tasks:')
|
||||||
|
dateNow = datetime.now()
|
||||||
|
tasks = chat.getTasks()
|
||||||
|
for task in tasks:
|
||||||
|
update.message.reply_text('leiendo task')
|
||||||
|
dateTask = task.getDate()
|
||||||
|
update.message.reply_text(task.toString())
|
||||||
|
update.message.reply_text('thats all for today')
|
||||||
|
chats[ident] = chat
|
||||||
def stop(bot, update):
|
def stop(bot, update):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -26,6 +91,8 @@ def main():
|
||||||
|
|
||||||
# on different commands - answer in Telegram
|
# on different commands - answer in Telegram
|
||||||
dp.add_handler(CommandHandler("start", start))
|
dp.add_handler(CommandHandler("start", start))
|
||||||
|
dp.add_handler(CommandHandler("tasksToday", getTasksToday))
|
||||||
|
dp.add_handler(CommandHandler("new", addTask, pass_args=True))
|
||||||
#dp.add_handler(CommandHandler("about", about))
|
#dp.add_handler(CommandHandler("about", about))
|
||||||
#dp.add_handler(CommandHandler("help", help))
|
#dp.add_handler(CommandHandler("help", help))
|
||||||
dp.add_handler(CommandHandler("stop", stop))
|
dp.add_handler(CommandHandler("stop", stop))
|
||||||
|
|
12
task.py
12
task.py
|
@ -24,3 +24,15 @@ class Task(object):
|
||||||
if h > 0 and datetime.now() > datetime(date.year, date.month, date.day, h):
|
if h > 0 and datetime.now() > datetime(date.year, date.month, date.day, h):
|
||||||
date += timedelta(1)
|
date += timedelta(1)
|
||||||
self.date = datetime(date.year, date.month, date.day, max(date.hour, h))
|
self.date = datetime(date.year, date.month, date.day, max(date.hour, h))
|
||||||
|
|
||||||
|
def getDate(self):
|
||||||
|
return self.date
|
||||||
|
|
||||||
|
def getName(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
s = "Task " self.name + ":\n"
|
||||||
|
s += "Date: " + " " + str(self.date.day) + "/" + str(self.date.year) + "/" + str(self.date.month) + "\n"
|
||||||
|
if self.repeat > 0:
|
||||||
|
s += "Repeats every " + self.repeat + " days."
|
||||||
|
|
Loading…
Reference in a new issue