1
|
|
|
import re |
2
|
|
|
from functools import partial |
3
|
|
|
|
4
|
|
|
import telegram |
5
|
|
|
|
6
|
|
|
from . import BotCommand, CommandError |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class AddCommand(BotCommand): |
10
|
|
|
command = '/add' |
11
|
|
|
|
12
|
|
|
def get_amount(self, content): |
13
|
|
|
match = re.match('((/add )|(/remove ))?(?P<amount>\d+(\.\d+)?)( (?P<reason>.*))?', content) |
14
|
|
|
if not match: |
15
|
|
|
raise CommandError() |
16
|
|
|
|
17
|
|
|
amount = float(match.groupdict()['amount']) |
18
|
|
|
reason = match.groupdict()['reason'] |
19
|
|
|
return amount, reason |
20
|
|
|
|
21
|
|
|
def add(self, tab_id, user_id, message_id, date, amount, reason=''): |
22
|
|
|
tab = self.get_tab(tab_id) |
23
|
|
|
tab.add(message_id, user_id, date, amount, reason) |
24
|
|
|
|
25
|
|
|
def default(self, message): |
26
|
|
|
content = message.text.split(' ', 1)[1] if len(message.text.split(' ', 1)) == 2 else '' |
27
|
|
|
if content: |
28
|
|
|
self.process_howmuch(message) |
29
|
|
|
else: |
30
|
|
|
msg = self.bot.say(message, 'How much?', |
31
|
|
|
reply_markup=telegram.ForceReply(selective=True)) |
32
|
|
|
self.queue(msg, partial(self.process_howmuch)) |
33
|
|
|
|
34
|
|
|
def process_howmuch(self, message): |
35
|
|
|
try: |
36
|
|
|
amount, reason = self.get_amount(message.text) |
37
|
|
|
except CommandError: |
38
|
|
|
self.bot.say(message, "Nope, I don't get ya") |
39
|
|
|
return |
40
|
|
|
self.add(message.chat.id, message.from_user.id, message.message_id, |
41
|
|
|
message.date, amount, reason) |
42
|
|
|
self.bot.say(message, telegram.Emoji.THUMBS_UP_SIGN) |
43
|
|
|
|