-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
195 lines (157 loc) · 6.9 KB
/
main.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import logging
from datetime import timedelta
from apscheduler.triggers.combining import AndTrigger, OrTrigger
from typing import List
from telegram import ForceReply, Update, User
from telegram.ext import Application, CommandHandler, StringCommandHandler, StringRegexHandler, ConversationHandler, ContextTypes, MessageHandler, filters
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
class Assignee():
def __init__(self, user:str) -> None:
self.user = user
self.duties = 0
def __repr__(self) -> str:
return self.user + f"[{self.duties}]"
class Task():
def __init__(self, name:str) -> None:
self.name = name
self.description = None
self.trigger = None
self.schedule = None
self.assignees = []
def update_description(self, description:str) -> None:
self.description = description
def add_assignee(self, assignee:Assignee) -> None:
self.assignees.append(assignee)
def remove_assignee(self, assignee:Assignee) -> None:
self.assignees.remove(assignee)
def update_schedule(self, schedule:str) -> None:
self.schedule = schedule
def __repr__(self) -> str:
return f"{self.name}/{self.triggerdays}/{[a for a in self.assignees]}" #TODO remove assignees once debugged
async def init(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /start is issued."""
context.chat_data['tasks'] = dict()
# normal message
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"Guten Tag, {update.effective_user.name}."
)
# TODO: add help message/instructions
async def add_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Add a job to the queue."""
task_name = context.args[0]
trigger = {
'trigger': 'cron',
'second': '*/5',
}
context.chat_data['tasks'] = {task_name:Task(task_name)}
# async def edit_task_description(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# """Modify a job"""
# context.chat_data['tasks']
async def start_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
task = context.chat_data['tasks'][context.args[0]]
context.job_queue.run_custom(alarm, data="test", job_kwargs=task.trigger, chat_id=update.effective_chat.id)
await update.message.reply_text('Timer successfully set!')
async def alarm(context) -> None:
"""Send the alarm message."""
await context.bot.send_message(chat_id=context.job.chat_id, text='Beep!')
# conversation callbacks
ADD_DESC, ADD_TRIGGER, ADD_ASSIGNEES, CONFIRM = range(4)
END = ConversationHandler.END
async def conv0_add_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Start the conversation and ask the user about the chore to add."""
task_name = context.args[0]
context.chat_data['wip_task'] = Task(task_name)
await update.message.reply_text(
f'New Task: {task_name}\n'+
f'Please add a description for the task, or type /skip if you don\'t want to.',
reply_markup=ForceReply(selective=True),
)
return ADD_DESC
async def conv1_add_desc(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Add description."""
context.chat_data['wip_task'].description = update.message.text
await update.message.reply_text(
'Please provide the periodicity of the task (supported formats: cron).',
reply_markup=ForceReply(selective=True),
)
return ADD_TRIGGER
async def conv1_skip_desk(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Skips the description"""
await update.message.reply_text(
'No description added (u lazy boi)\n'+
'Please provide the periodicity of the task.',
reply_markup=ForceReply(selective=True),
)
return ADD_TRIGGER
async def conv2_add_trigger(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Add the interval/trigger for the task."""
f = update.message.text
context.chat_data['wip_task'].trigger = f
# TODO implement cron parsing
context.chat_data['wip_task'].trigger = {
'trigger': 'cron',
'second': '*/5',
}
# TODO parse trigger and print result
await update.message.reply_text(
f'Setting up trigger: {f}\n'+
f'Please add the people responsible for the task.',
reply_markup=ForceReply(selective=True),
)
return ADD_ASSIGNEES
async def conv3_add_people(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Add the people responsible for the task."""
# TODO investigate MessageEntity.TEXT_MENTION,MessageEntity.MENTION
# TODO make assignees objects from mentions
f = update.message.text
context.chat_data['wip_task'].assignees.append(f)
await update.message.reply_text(
f'Task ready to release: {f}'+
f'{context.chat_data["wip_task"]}'+
f'Please confirm the task with /confirm or cancel with /cancel.',
reply_markup=ForceReply(selective=True),
)
return CONFIRM
async def conv4_confirm(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Confirm and save the task."""
# add task to chat_data
# todo copy data to avoid reference issues
context.chat_data['tasks'][context.chat_data['wip_task'].name] = context.chat_data['wip_task']
task = context.chat_data['wip_task']
await update.message.reply_text(
f'Task confirmed and scheduled.',
)
context.job_queue.run_custom(alarm, data="test", job_kwargs=task.trigger, chat_id=update.effective_chat.id)
context.chat_data['wip_task'] = None
return END
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Cancel the conversation."""
await update.message.reply_text('Task Canceled')
return END
def main() -> None:
"""Start the bot."""
with open('TOKEN.txt', 'r') as f:
TOKEN = f.readline().strip()
# Create the Application and pass it your bot's token.
application = Application.builder().token(TOKEN).build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("init", init))
application.add_handler(ConversationHandler(
entry_points=[CommandHandler('add_task', conv0_add_task)],
states={
ADD_DESC: [MessageHandler(filters.ALL & ~ filters.COMMAND, conv1_add_desc),
CommandHandler('skip', conv1_skip_desk)],
ADD_TRIGGER: [MessageHandler(filters.ALL & ~ filters.COMMAND, conv2_add_trigger)],
ADD_ASSIGNEES: [MessageHandler(filters.ALL & ~ filters.COMMAND, conv3_add_people)],
CONFIRM: [CommandHandler('confirm', conv4_confirm)]
},
fallbacks=[CommandHandler('cancel', cancel)],
))
# Run the bot until the user presses Ctrl-C
application.run_polling(poll_interval=0.0) #TODO increase poll interval
if __name__ == '__main__':
main()