-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
131 lines (103 loc) · 3.33 KB
/
Copy pathmain.py
File metadata and controls
131 lines (103 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
import json
import os
TOKEN = os.getenv("TOKEN")
CHAT_ID = int(os.getenv("CHAT_ID"))
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
import asyncio
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(
"Hello Naresh!\n\n"
"/addtask\n"
"/tasks\n"
"/deletetask"
)
async def addtask(update: Update, context: ContextTypes.DEFAULT_TYPE):
if len(context.args) < 2:
await update.message.reply_text(
"Usage:\n/addtask HH:MM Task Name"
)
return
task_time = context.args[0]
task_name = " ".join(context.args[1:])
with open("tasks.json", "r") as f:
data = json.load(f)
data["tasks"].append(
{
"time": task_time,
"task": task_name
}
)
with open("tasks.json", "w") as f:
json.dump(data, f, indent=4)
await update.message.reply_text(
f"✅ Task Added\n\n{task_time} - {task_name}"
)
async def tasks(update: Update, context: ContextTypes.DEFAULT_TYPE):
with open("tasks.json", "r") as f:
data = json.load(f)
if not data["tasks"]:
await update.message.reply_text(
"No tasks available."
)
return
message = "📋 Your Tasks\n\n"
for i, task in enumerate(data["tasks"], start=1):
message += (
f"{i}. {task['time']} - {task['task']}\n"
)
await update.message.reply_text(message)
async def deletetask(update: Update, context: ContextTypes.DEFAULT_TYPE):
if len(context.args) != 1:
await update.message.reply_text(
"Usage:\n/deletetask task_number"
)
return
try:
task_index = int(context.args[0]) - 1
with open("tasks.json", "r") as f:
data = json.load(f)
if task_index < 0 or task_index >= len(data["tasks"]):
await update.message.reply_text(
"❌ Invalid task number"
)
return
deleted_task = data["tasks"].pop(task_index)
with open("tasks.json", "w") as f:
json.dump(data, f, indent=4)
await update.message.reply_text(
f"✅ Deleted\n\n{deleted_task['time']} - {deleted_task['task']}"
)
except ValueError:
await update.message.reply_text(
"❌ Enter a valid task number"
)
def check_tasks(app):
current_time = datetime.now().strftime("%H:%M")
with open("tasks.json", "r") as f:
data = json.load(f)
for task in data["tasks"]:
if task["time"] == current_time:
asyncio.run(
app.bot.send_message(
chat_id=CHAT_ID,
text=f"🔔 Reminder\n\n{task['task']}"
)
)
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("addtask", addtask))
app.add_handler(CommandHandler("tasks", tasks))
app.add_handler(CommandHandler("deletetask", deletetask))
scheduler = BackgroundScheduler()
scheduler.add_job(
check_tasks,
"interval",
minutes=1,
args=[app]
)
scheduler.start()
print("Reminder Scheduler Started...")
app.run_polling()