-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
160 lines (108 loc) · 3.73 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
import logging
import telebot # type: ignore
from flask import Flask
from telebot.types import Message # type: ignore
import config # type: ignore
import replies
from kjbot import (add_item, check_payment, check_time_and_chat,
display_status, edit_balances, find_payee, list_items,
remove_item, users)
app = Flask(__name__)
TOKEN: str = config.token
URL: str = config.URL + TOKEN
logger: logging.Logger = telebot.logger
logger.setLevel(logging.DEBUG)
bot: telebot.TeleBot = telebot.TeleBot(TOKEN, threaded=False)
@bot.message_handler(commands=['start'])
def start(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
bot.reply_to(m, replies.START_GREETING)
@bot.message_handler(commands=['help'])
def help(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
bot.reply_to(m, replies.HELP, parse_mode='Markdown')
@bot.message_handler(commands=['spent'])
def spent(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
amount_flt, valid_number = check_payment(m.text, needs_description=True)
if not valid_number:
bot.reply_to(m, replies.VALID_AMT_W_DESC)
return
info = ['spent']
balance_before, ok = edit_balances(str(m.from_user.id), amount_flt, info)
if not ok:
return
msg = display_status(balance_before)
try:
bot.reply_to(m, msg, parse_mode='Markdown')
except Exception:
bot.send_message(config.chat_id, msg, parse_mode='Markdown')
for user_id in users.keys():
bot.send_message(user_id, m.text[7:], parse_mode='Markdown')
@bot.message_handler(commands=['paid'])
def paid(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
amount_flt, valid_number = check_payment(m.text, needs_description=False)
if not valid_number:
bot.reply_to(m, replies.VALID_AMT)
return
paidfrom = m.from_user.id
paidfrom = str(paidfrom)
paidto = find_payee(paidfrom)
if paidto == '':
bot.reply_to(m, replies.PAID_TO_WHOM)
return
info = ['paid', paidfrom, paidto]
balance_before, ok = edit_balances(str(m.from_user.id), amount_flt, info)
if not ok:
return
msg = display_status(balance_before)
try:
bot.reply_to(m, msg, parse_mode='Markdown')
except Exception:
bot.send_message(config.chat_id, msg, parse_mode='Markdown')
@bot.message_handler(commands=['status'])
def status(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
msg = display_status()
try:
bot.reply_to(m, msg, parse_mode='Markdown')
except Exception:
bot.send_message(config.chat_id, msg, parse_mode='Markdown')
@bot.message_handler(commands=['add'])
def add(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
try:
item: str = m.text[5:]
except Exception:
bot.reply_to(m, replies.EMPTY_ADD)
return
if item == '':
bot.reply_to(m, replies.EMPTY_ADD)
return
msg = add_item(item)
bot.reply_to(m, msg, parse_mode='Markdown')
@bot.message_handler(commands=['remove'])
def remove(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
try:
index = int(m.text.split(' ')[1])
except Exception:
bot.reply_to(m, replies.WRONG_INDEX)
return
msg = remove_item(index)
bot.reply_to(m, msg, parse_mode='Markdown')
@bot.message_handler(commands=['list'])
def list(m: Message) -> None:
if not check_time_and_chat(m.date, m.chat.id):
return
msg = list_items()
bot.reply_to(m, msg, parse_mode='Markdown')
bot.infinity_polling()