-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinBot.py
More file actions
276 lines (206 loc) · 9.79 KB
/
finBot.py
File metadata and controls
276 lines (206 loc) · 9.79 KB
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
import os
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, MessageHandler
from telegram.ext import CommandHandler, CallbackQueryHandler, ConversationHandler, filters
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from dotenv import load_dotenv
from responses import FIXED_CHARGES_CATEGORY, FIXED_INCOME_CATEGORY, HEADER_FIXED_CHARGES, HEADER_FIXED_INCOME, HE
from warnings import filterwarnings
from telegram.warnings import PTBUserWarning
import helper
from sheetApi import add_income, add_outcome
# Load env
load_dotenv()
# Ignore warning
filterwarnings(action="ignore", message=r".*CallbackQueryHandler", category=PTBUserWarning)
# ENV
BOT_TOKEN = os.getenv('BOT_TOKEN_PROD')
AUTH_USER_ID = int(os.getenv('AUTH_USER_ID'))
STEP_1_CATEGORY = 0
STEP_1_OTHER = 1
STEP_2_SERVICE = 2
STEP_3_COST = 3
STEP_1_CATEGORY_INCOME = 11
STEP_1_OTHER_INCOME = 12
STEP_2_COST_INCOME = 13
current_payment = helper.init_outcome()
current_income = helper.init_income()
def fixed_charge_message():
return HEADER_FIXED_CHARGES
def fixed_income_message():
return HEADER_FIXED_INCOME
async def out(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(fixed_charge_message(), reply_markup=service_menu())
return STEP_1_CATEGORY
async def income(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(fixed_income_message(), reply_markup=income_menu())
return STEP_1_CATEGORY_INCOME
async def fuel(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment["category"] = 'Fuel'
current_payment['category_display_name'] = FIXED_CHARGES_CATEGORY['Fuel']
await update.callback_query.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def food(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment["category"] = 'Food'
current_payment['category_display_name'] = FIXED_CHARGES_CATEGORY['Food']
await update.callback_query.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def clothes(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment["category"] = 'Clothes'
current_payment['category_display_name'] = FIXED_CHARGES_CATEGORY['Clothes']
await update.callback_query.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def tech(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment["category"] = 'Tech'
current_payment['category_display_name'] = FIXED_CHARGES_CATEGORY['Tech']
await update.callback_query.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def groceries(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment["category"] = 'Groceries'
current_payment['category_display_name'] = FIXED_CHARGES_CATEGORY['Groceries']
await update.callback_query.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def hangouts(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment["category"] = 'Hangouts'
current_payment['category_display_name'] = FIXED_CHARGES_CATEGORY['Hangouts']
await update.callback_query.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def transportation(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment["category"] = 'Transportation'
current_payment['category_display_name'] = FIXED_CHARGES_CATEGORY['Transportation']
await update.callback_query.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def other(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.callback_query.message.reply_text(HE.get('other_category'))
return STEP_1_OTHER
def service_menu():
services = []
for charge in FIXED_CHARGES_CATEGORY:
services.append([InlineKeyboardButton(FIXED_CHARGES_CATEGORY[charge], callback_data=charge)])
return InlineKeyboardMarkup(services)
def income_menu():
incomes = []
for income in FIXED_INCOME_CATEGORY:
incomes.append([InlineKeyboardButton(FIXED_INCOME_CATEGORY[income], callback_data=income)])
return InlineKeyboardMarkup(incomes)
async def bit(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_income
current_income["service"] = FIXED_INCOME_CATEGORY['Bit']
await update.callback_query.message.reply_text(HE.get('income_amount'))
return STEP_2_COST_INCOME
async def salary(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_income
current_income["service"] = FIXED_INCOME_CATEGORY['Salary']
await update.callback_query.message.reply_text(HE.get('income_amount'))
return STEP_2_COST_INCOME
async def savings(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_income
current_income["service"] = FIXED_INCOME_CATEGORY['Savings']
current_income['method'] = helper.get_definition().get('savings')
await update.callback_query.message.reply_text(HE.get('income_amount'))
return STEP_2_COST_INCOME
async def other_income(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.callback_query.message.reply_text(HE.get('income_service'))
return STEP_1_OTHER_INCOME
async def get_service_income(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_income
current_income['service'] = update.message.text
await update.message.reply_text(HE.get('income_amount'))
return STEP_2_COST_INCOME
async def get_cost_income(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_income
current_income['cost'] = update.message.text
current_income['date'] = helper.get_date()
await update.message.reply_text(f"{HE.get('income_completed')}\n{HE.get('service')}: {current_income['service']}\n{HE.get('cost')}: {current_income['cost']}\n{HE.get('date')}: {current_income['date']}\n.")
if auth_user(update):
if current_income['method'] == helper.get_definition().get('savings'):
# Saving goes from income to savings as an outcome
add_income(current_income)
current_income['category'] = helper.get_definition().get('account_transfer')
add_outcome(current_income)
else:
add_income(current_income)
current_income = helper.init_income()
return ConversationHandler.END
async def get_cost(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment['cost'] = update.message.text
current_payment['date'] = helper.get_date()
await update.message.reply_text(f"{HE.get('completed')}\n{HE.get('category')}: {current_payment['category_display_name']}\n{HE.get('service')}: {current_payment['service']}\n{HE.get('cost')}: {current_payment['cost']}\n{HE.get('date')}: {current_payment['date']}\n.")
if auth_user(update):
add_outcome(current_payment)
current_payment = helper.init_outcome()
return ConversationHandler.END
async def get_service(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment['service'] = update.message.text
await update.message.reply_text(HE.get('service_cost'))
return STEP_3_COST
async def get_category(update: Update, context: ContextTypes.DEFAULT_TYPE):
global current_payment
current_payment['category'] = 'Other'
current_payment['category_display_name'] = update.message.text
await update.message.reply_text(HE.get('service_name'))
return STEP_2_SERVICE
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(HE.get('help'))
async def author(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(HE.get('author'))
def auth_user(update):
if update.message.from_user.id != AUTH_USER_ID:
return False
return True
app = ApplicationBuilder().token(BOT_TOKEN).build()
OUTCOME = ConversationHandler (
entry_points = [CommandHandler("out", out)],
states = {
STEP_1_CATEGORY : [
CallbackQueryHandler(fuel, pattern='Fuel'),
CallbackQueryHandler(food, pattern='Food'),
CallbackQueryHandler(clothes, pattern='Clothes'),
CallbackQueryHandler(tech, pattern='Tech'),
CallbackQueryHandler(groceries, pattern='Groceries'),
CallbackQueryHandler(hangouts, pattern='Hangouts'),
CallbackQueryHandler(transportation, pattern='Transportation'),
CallbackQueryHandler(other, pattern='Other')
],
STEP_1_OTHER: [
MessageHandler(filters.ALL , get_category)
],
STEP_2_SERVICE : [
MessageHandler(filters.ALL , get_service)
],
STEP_3_COST : [
MessageHandler(filters.ALL , get_cost)
]
}
, fallbacks=[CommandHandler("out", out)], allow_reentry=True)
INCOME = ConversationHandler (
entry_points = [CommandHandler("in", income)],
states = {
STEP_1_CATEGORY_INCOME : [
CallbackQueryHandler(bit, pattern='Bit'),
CallbackQueryHandler(salary, pattern='Salary'),
CallbackQueryHandler(savings, pattern='Savings'),
CallbackQueryHandler(other_income, pattern='Other')
],
STEP_1_OTHER_INCOME: [
MessageHandler(filters.ALL , get_service_income)
],
STEP_2_COST_INCOME : [
MessageHandler(filters.ALL , get_cost_income)
]
}
, fallbacks=[CommandHandler("in", income)], allow_reentry=True)
app.add_handler(OUTCOME)
app.add_handler(INCOME)
app.add_handler(CommandHandler("help", help))
app.add_handler(CommandHandler("author", author))
app.run_polling()