-
Notifications
You must be signed in to change notification settings - Fork 0
/
financebot.py
380 lines (315 loc) · 11.2 KB
/
financebot.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import datetime
import os
import json
import logging
from dotenv import load_dotenv
from typing import Dict
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters,
)
from core import manager
# load_dotenv()
TOKEN = os.getenv("TOKEN", "none")
ALLOWED_USERS = int(os.getenv("ALLOWED_USERS", 0))
TAGS_PK = json.loads(os.getenv('TAGS_PK'))
ACCOUNTS_PK = json.loads(os.getenv('ACCOUNTS_PK'))
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
(
CHOOSING,
TYPING_REPLY,
TYPING_CHOICE,
CHOOSINGCATEGORY,
CHOOSINGPLACE,
CHOOSINGAMOUNT,
CHOOSINGDATE,
CHOOSINGACCOUNT,
END,
) = range(9)
def facts_to_str(user_data: Dict[str, str]) -> str:
"""Helper function for formatting the gathered user info."""
facts = [f"{key} - {value}" for key, value in user_data.items()]
return "\n".join(facts).join(["\n", "\n"])
def _get_categories_menu_keyboard() -> ReplyKeyboardMarkup:
"""Helper function for formatting the categories menu keyboard."""
CATEGORIES_PK = json.loads(os.getenv('CATEGORIES_PK'))
reply_keyboard = []
aux = []
c = 0
for category in CATEGORIES_PK.keys():
if c == 5:
reply_keyboard.append(aux)
aux = []
c = 0
aux.append(category)
c += 1
reply_keyboard.append(aux)
# add cancel option
reply_keyboard.append(["Cancel transaction"])
logger.debug(reply_keyboard)
return reply_keyboard
def get_main_menu_keyboard() -> ReplyKeyboardMarkup:
"""Helper function for formatting the main menu keyboard."""
reply_keyboard = [
["Create Transaction"],
["Create category", "Create tag"],
["Done"],
]
return ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Start the conversation and ask user for input."""
markup = get_main_menu_keyboard()
await update.message.reply_text(
"Hi! My name is Finance Bot. "
"Why don't you tell me what is your transaction?",
reply_markup=markup,
)
return CHOOSING
async def create_simple_operation(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
"""Create a Category or a Tag."""
user = update.message.from_user
text = update.message.text
context.user_data["choice"] = text
field = text.split()[1]
logger.info(f"USER @{user.username}:{user.id} - {text} operation started.")
await update.message.reply_text(f"Please enter the new {field} name:")
return TYPING_REPLY
async def received_information_simple_operation(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
user_data = context.user_data
new_name_of_instance = update.message.text
operation = user_data["choice"].split()[1]
user_data[user_data["choice"]] = new_name_of_instance
del user_data["choice"]
user = update.message.from_user
if user.id != ALLOWED_USERS:
msg = f"Sorry {user.first_name}, you are not allowed to use this bot."
await update.message.reply_text(msg, reply_markup=ReplyKeyboardRemove())
user_data.clear()
return ConversationHandler.END
# request to create a new operation
manager.create_simple_entity(entity=operation, name=new_name_of_instance)
markup = get_main_menu_keyboard()
msg = f"New `{new_name_of_instance}` {operation} created."
await update.message.reply_text(
msg,
reply_markup=markup,
)
return CHOOSING
async def start_transaction(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Ask the user for a description of a custom category."""
user = update.message.from_user
logger.info(f"USER @{user.username}:{user.id} - Transaction.")
keyboard_options_category = _get_categories_menu_keyboard()
await update.message.reply_text(
"Please choose from the following options:",
reply_markup=ReplyKeyboardMarkup(
keyboard_options_category,
resize_keyboard=True,
one_time_keyboard=True,
input_field_placeholder="Choose an option...",
),
)
return CHOOSINGCATEGORY
async def choose_tag_for_transaction(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
"""Ask the user for a description of a custom category."""
text = update.message.text
keyboard_options_tags = [
["Holiday", "None"],
["Cancel transaction"],
]
if text == "Cancel transaction":
await update.message.reply_text(
"Transaction cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
context.user_data["category"] = text
await update.message.reply_text(
"Please choose from the following options:",
reply_markup=ReplyKeyboardMarkup(
keyboard_options_tags,
resize_keyboard=True,
one_time_keyboard=True,
input_field_placeholder="Choose an option...",
),
)
return CHOOSINGPLACE
async def choose_place_for_transaction(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
"""Ask the user for a description of a custom category."""
tag = update.message.text
if tag == "Cancel transaction":
await update.message.reply_text(
"Transaction cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
context.user_data["tag"] = None
if tag != "None":
context.user_data["tag"] = tag
await update.message.reply_text("Please enter the `place` name:")
return CHOOSINGAMOUNT
async def choose_amount_for_transaction(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
"""Ask the user for a description of a custom category."""
place = update.message.text
if place == "Cancel transaction":
await update.message.reply_text(
"Transaction cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
context.user_data["place"] = place
await update.message.reply_text("Please enter the `amount` you spent €:")
return CHOOSINGDATE
async def choose_date_for_transaction(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
"""Ask the user for a description of a custom category."""
amount = update.message.text
if amount == "Cancel transaction":
await update.message.reply_text(
"Transaction cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
context.user_data["amount"] = amount
await update.message.reply_text("Please enter the `date` (YYYY-MM-DD) or `today`:")
return CHOOSINGACCOUNT
async def choose_account_for_transaction(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
"""Ask the user for a description of a custom category."""
date = update.message.text
if date.lower() == "today":
date = datetime.datetime.now().strftime("%Y-%m-%d")
context.user_data["date"] = date
keyboard_options_accounts = [
["Revolut", "Wise", "BOI"],
["Cancel transaction"],
]
await update.message.reply_text(
"Please choose a `account` from the following options:",
reply_markup=ReplyKeyboardMarkup(
keyboard_options_accounts, resize_keyboard=True, one_time_keyboard=True
),
)
return END
async def complete_transaction(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:
"""Ask the user for a description of a custom category."""
account = update.message.text
if account == "Cancel transaction":
await update.message.reply_text(
"Transaction cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
context.user_data["account"] = account
data = context.user_data
manager.create_transaction(
category=data["category"],
tag=data["tag"],
place=data["place"],
amount=data["amount"],
date=data["date"],
account=data["account"],
)
markup = get_main_menu_keyboard()
await update.message.reply_text(
"Cool! Transaction completed.",
reply_markup=markup,
)
return CHOOSING
async def done(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Display the gathered info and end the conversation."""
user_data = context.user_data
if "choice" in user_data:
del user_data["choice"]
await update.message.reply_text(
"I learned these facts about you:" f"{facts_to_str(user_data)}Until next time!",
reply_markup=ReplyKeyboardRemove(),
)
user_data.clear()
return ConversationHandler.END
def main() -> None:
"""Run the bot."""
application = Application.builder().token(TOKEN).build()
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
CHOOSING: [
MessageHandler(
filters.Regex("^Create +(tag|category)$"), create_simple_operation
),
MessageHandler(
filters.Regex("^Create Transaction$"), start_transaction
),
],
CHOOSINGCATEGORY: [
MessageHandler(
filters.TEXT & ~(filters.COMMAND | filters.Regex("^Done$")),
choose_tag_for_transaction,
)
],
CHOOSINGPLACE: [
MessageHandler(
filters.TEXT & ~(filters.COMMAND | filters.Regex("^Done$")),
choose_place_for_transaction,
)
],
CHOOSINGAMOUNT: [
MessageHandler(
filters.TEXT & ~(filters.COMMAND | filters.Regex("^Done$")),
choose_amount_for_transaction,
)
],
CHOOSINGDATE: [
MessageHandler(
filters.TEXT & ~(filters.COMMAND | filters.Regex("^Done$")),
choose_date_for_transaction,
)
],
CHOOSINGACCOUNT: [
MessageHandler(
filters.TEXT & ~(filters.COMMAND | filters.Regex("^Done$")),
choose_account_for_transaction,
)
],
END: [
MessageHandler(
filters.TEXT & ~(filters.COMMAND | filters.Regex("^Done$")),
complete_transaction,
)
],
TYPING_REPLY: [
MessageHandler(
filters.TEXT & ~(filters.COMMAND | filters.Regex("^Done$")),
received_information_simple_operation,
)
],
},
fallbacks=[MessageHandler(filters.Regex("^Done$"), done)],
)
application.add_handler(conv_handler)
# Run the bot until the user presses Ctrl-C
application.run_polling()
if __name__ == "__main__":
main()