-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_tracker.py
executable file
·130 lines (104 loc) · 4.56 KB
/
time_tracker.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
#!/usr/bin/env python3
import tempfile
from config import Config
from server import bot, server
from database import management
from keyboards import keyboard_notstarted, keyboard_started, text_trigger
@bot.message_handler(func=text_trigger('Begin interval'))
@bot.message_handler(commands=['begin'])
def begin_interval_handler(message):
time = management.begin_interval(message.from_user.id, message.date)
bot.reply_to(message, 'New interval started at {}'.format(time), reply_markup=keyboard_started)
@bot.message_handler(func=text_trigger('Cancel interval'))
@bot.message_handler(commands=['cancel'])
def cancel_interval_handler(message):
management.cancel_interval(message.from_user.id)
bot.reply_to(message, 'Your last begin time was cleared', reply_markup=keyboard_notstarted)
@bot.message_handler(func=text_trigger('End interval'))
@bot.message_handler(commands=['end'])
def end_interval_handler(message):
try:
record = management.end_interval(message.from_user.id, message.date)
# TODO: move '{} - {} (duration {})' to Record.__str__
bot.reply_to(message, 'Record added: {} - {} (duration {})'.format(
record.format_begin_time(),
record.format_end_time(),
record.duration(),
), reply_markup=keyboard_notstarted)
except ValueError as e:
bot.reply_to(message, str(e))
@bot.message_handler(commands=['userscount'])
def users_count_handler(message):
bot.reply_to(message, 'Users: {}'.format(management.get_users_count()))
@bot.message_handler(func=text_trigger('Last record'))
@bot.message_handler(commands=['getlast'])
def get_last_handler(message):
try:
record = management.get_last_record(message.from_user.id)
bot.reply_to(message, 'Last record: {} - {} (duration {})'.format(
record.format_begin_time(),
record.format_end_time(),
record.duration(),
))
except ValueError:
bot.reply_to(message, 'You have no any records. Use /begin and /end to add them')
@bot.message_handler(commands=['deletelast'])
def delete_last_handler(message):
try:
management.delete_last_record(message.from_user.id)
bot.reply_to(message, 'Your last record was deleted')
except ValueError:
bot.reply_to(message, 'You have no any records. Use /begin and /end to add them')
@bot.message_handler(commands=['delete'])
def delete_selected_handler(message):
msg = message.text.split()
if len(msg) < 2 or not msg[1].isdigit():
bot.reply_to(message, 'You have not provided record id to delete')
else:
record_id = int(msg[1])
management.delete_record(record_id)
bot.reply_to(message, f'Record #{record_id} was deleted')
@bot.message_handler(func=text_trigger('File'))
@bot.message_handler(commands=['getfile'])
def get_file_handler(message):
user = management.ensure_user(message.from_user.id)
if not user.records:
bot.reply_to(message, 'You have no any records. Use /begin and /end to add them')
else:
file = tempfile.NamedTemporaryFile(mode='w+t')
# Since NamedTemporaryFile has special object to delete file,
# it is safe to set file.name to any desired value
file.name = 'records.csv'
management.records_to_file(user.user_id, file)
file.seek(0)
bot.send_document(message.chat.id, file, caption='Total records: {}'.format(len(user.records)))
@bot.message_handler(commands=['settimezone'])
def set_user_timezone(message):
try:
# TODO: has telebot any methods to get command arguments?
args = message.text.split()
if len(args) < 2:
bot.reply_to(message, f'You have not provided timezone name')
return
tzname = args[1]
try:
tzname = int(tzname)
except ValueError:
pass
tzname = management.set_user_timezone(message.chat.id, tzname)
bot.reply_to(message, f'Your time zone set to {tzname}')
except ValueError as e:
bot.reply_to(message, str(e))
@bot.message_handler(func=text_trigger('My timezone'))
@bot.message_handler(commands=['gettimezone'])
def get_user_timezone(message):
tzname = management.get_user_timezone(message.chat.id)
bot.reply_to(message, f'Your current time zone: {tzname}')
# Any testing functions I need
@bot.message_handler(func=lambda msg: True)
def default_message_handler(message):
msg = message.text.split()
if msg and msg[0] == Config.TOKEN:
bot.reply_to(message, str(server.config))
if __name__ == '__main__':
server.run(host='0.0.0.0', port=Config.PORT)