-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
350 lines (259 loc) · 14.6 KB
/
app.py
File metadata and controls
350 lines (259 loc) · 14.6 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
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
# Importacoes
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters,
)
import logging
from google_sheets_api import GoogleSheets
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
# Constants responsible for being conversation entry points
CHOOSE_OPTION, REGISTER_INCOME_STEP_1, REGISTER_INCOME_STEP_2, REGISTER_INCOME_STEP_3, AWAITING_COMMENT, COMPLETE_INCOME, REGISTER_OUTCOME_STEP_1, REGISTER_OUTCOME_STEP_2, REGISTER_OUTCOME_STEP_3, REGISTER_OUTCOME_STEP_4, COMPLETE_OUTCOME, REGISTER_TRANSFER_STEP_1, REGISTER_TRANSFER_STEP_2, REGISTER_TRANSFER_STEP_3, REGISTER_TRANSFER_STEP_4, COMPLETE_TRANSFER, DISPLAY_REPORT_STEP_1 = range(
17)
# States(Functions responsible for doing something, called by the state controller)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
reply_keyboard = [["Incomes", "Outcomes", "Transfers", 'Report']]
await update.message.reply_text(
"Hi! Choose an option.", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
return CHOOSE_OPTION
async def register_income_step_1(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text("Which input do you want to register?", reply_markup=ReplyKeyboardMarkup(
[['Salary', '13º Salary', 'Others']], one_time_keyboard=True))
return REGISTER_INCOME_STEP_1
async def register_income_step_2(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['category'] = update.message.text
await update.message.reply_text("Which was the value of the input?", reply_markup=ReplyKeyboardRemove())
return REGISTER_INCOME_STEP_2
async def register_income_step_3(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['value'] = update.message.text
# Request the comment and wait for the answer
await update.message.reply_text("Leave a comment?", reply_markup=ReplyKeyboardMarkup([['Yes', 'No']], one_time_keyboard=True))
return REGISTER_INCOME_STEP_3
async def register_income_step_4(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
answer = update.message.text
if answer == 'Yes':
await update.message.reply_text("Leave a comment!", reply_markup=ReplyKeyboardRemove())
return AWAITING_COMMENT # New state to wait for the comment
else:
await update.effective_chat.send_message("Proceeding", reply_markup=ReplyKeyboardRemove())
# Set the comment as empty if the user doesn't want to add one
context.user_data['comments'] = ""
return await complete_income(update, context)
async def awaiting_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['comments'] = update.message.text
return await complete_income(update, context)
async def complete_income(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
try:
category = context.user_data.get('category')
value = context.user_data.get('value')
comments = context.user_data.get('comments', '')
google_sheets_api = GoogleSheets()
google_sheets_api.insert_incomes(value, category, comments)
await update.message.reply_text("Entry completed!")
except Exception as error:
await update.message.reply_text(f"An error occurred: {error}")
print(error)
return ConversationHandler.END
async def register_outcome_step_1(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text("Select the type of outcome", reply_markup=ReplyKeyboardMarkup([["Fixed", "Variable"], ["Unique"]], one_time_keyboard=True))
return REGISTER_OUTCOME_STEP_1
async def register_outcome_step_2(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['classification'] = update.message.text
await update.message.reply_text("What is the outcome category?", reply_markup=ReplyKeyboardMarkup([["Bills", "Maintenance", "Subscriptions", "Health"]], one_time_keyboard=True))
return REGISTER_OUTCOME_STEP_2
async def register_outcome_step_3(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['outcome_category'] = update.message.text
await update.message.reply_text("What was the outcome value?", reply_markup=ReplyKeyboardRemove())
return REGISTER_OUTCOME_STEP_3
async def register_outcome_step_4(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['outcome_value'] = update.message.text
await update.message.reply_text("Would you like to add a comment?", reply_markup=ReplyKeyboardMarkup([["Yes", "No"]], one_time_keyboard=True))
return REGISTER_OUTCOME_STEP_4
async def register_outcome_step_5(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
response = update.message.text
if response == 'Yes':
await update.message.reply_text("Enter the comment", reply_markup=ReplyKeyboardRemove())
return COMPLETE_OUTCOME
else:
await update.effective_chat.send_message("Ok, proceeding without comments", reply_markup=ReplyKeyboardRemove())
return await complete_outcome(update, context)
async def complete_outcome(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
try:
context.user_data['comment'] = update.message.text
print(context.user_data['classification'])
print(context.user_data['outcome_category'])
print(context.user_data['outcome_value'])
print(context.user_data['comment'])
value = context.user_data['outcome_value']
classification = context.user_data['classification']
category = context.user_data['outcome_category']
comment = context.user_data['comment']
google_sheets_api = GoogleSheets()
google_sheets_api.insert_outcomes(
value, classification, category, comment)
except Exception as e:
print('a comment was not provided')
await update.message.reply_text("Outcome recorded successfully!", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
async def register_transfer_step_1(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text("Which account are you transferring to?", reply_markup=ReplyKeyboardMarkup([["Account 1", "Account 2", "Account 3"]], one_time_keyboard=True))
return REGISTER_TRANSFER_STEP_1
async def register_transfer_step_2(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['from_account'] = update.message.text
await update.message.reply_text("To which account are you transferring?", reply_markup=ReplyKeyboardMarkup([["Account 1", "Account 2", "Account 3"]], one_time_keyboard=True))
return REGISTER_TRANSFER_STEP_2
async def register_transfer_step_3(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['to_account'] = update.message.text
await update.message.reply_text("What is the transfer amount?", reply_markup=ReplyKeyboardRemove())
context.user_data['transfer_value'] = update.message.text
return REGISTER_TRANSFER_STEP_3
async def register_transfer_step_4(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['transfer_amount'] = update.message.text
await update.message.reply_text("Would you like to add a comment?", reply_markup=ReplyKeyboardMarkup([["Yes", "No"]], one_time_keyboard=True))
return REGISTER_TRANSFER_STEP_4
async def register_transfer_step_5(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
response = update.message.text
if response == 'Yes':
await update.message.reply_text("Enter the comment", reply_markup=ReplyKeyboardRemove())
return COMPLETE_TRANSFER
else:
await update.effective_chat.send_message("Ok, proceeding without comments", reply_markup=ReplyKeyboardRemove())
return await complete_transfer(update, context)
async def complete_transfer(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
try:
context.user_data['comment'] = update.message.text
print(context.user_data['from_account'])
print(context.user_data['to_account'])
print(context.user_data['transfer_value'])
print(context.user_data['comment'])
from_account = context.user_data['from_account']
to_account = context.user_data['to_account']
value = context.user_data['transfer_value']
comments = context.user_data['comment']
google_sheets_api = GoogleSheets()
google_sheets_api.insert_transfers(
value, from_account, to_account, comments)
except Exception as e:
print('a comment was not provided')
await update.message.reply_text("Transfer registered successfully!", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
async def display_report_step_1(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text("Select the period", reply_markup=ReplyKeyboardMarkup([["Total Incomes", "Total Outcomes", "Total Transfers"]], one_time_keyboard=True))
return DISPLAY_REPORT_STEP_1
async def display_report_step_2(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
context.user_data['period'] = update.message.text
await update.effective_chat.send_message('Ok, proceeding with the report', reply_markup=ReplyKeyboardRemove())
return await show_report(update, context)
async def show_report(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
google_sheet_api = GoogleSheets()
column = update.message.text
values = google_sheet_api.show_report(column)
await update.effective_chat.send_message(f"Your {column} were {values}.", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
async def invalid_input(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text("This is not a typing area please restart /start")
def main():
application = Application.builder().token(
'6629668965:AAEINdUidQT5stzxVc2v5YfjStTVLJ2HCpM').build()
conversation_handler = ConversationHandler(
# entry_points what the person needs to type to start chatting with my bot ex: /start
entry_points=[CommandHandler("start", start)],
states={
CHOOSE_OPTION: [
MessageHandler(filters.Regex("^(Incomes)$"),
register_income_step_1),
MessageHandler(filters.Regex("^(Outcomes)$"),
register_outcome_step_1),
MessageHandler(filters.Regex("^(Transfers)$"),
register_transfer_step_1),
MessageHandler(filters.Regex("^(Report)$"),
display_report_step_1),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input),
],
REGISTER_INCOME_STEP_1: [
MessageHandler(filters.Regex(
"^(Salary|13º Salary|Others)$"), register_income_step_2),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input),
],
REGISTER_INCOME_STEP_2: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
register_income_step_3)
],
REGISTER_INCOME_STEP_3: [
MessageHandler(filters.Regex("^(Yes|No)$"),
register_income_step_4),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input)
],
AWAITING_COMMENT: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
awaiting_comment)
],
COMPLETE_INCOME: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
complete_income)
],
REGISTER_OUTCOME_STEP_1: [
MessageHandler(filters.Regex(
"^(Fixed|Variable|Unique)$"), register_outcome_step_2),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input)
],
REGISTER_OUTCOME_STEP_2: [
MessageHandler(filters.Regex(
"^(Bills|Maintenance|Subscriptions|Health)$"), register_outcome_step_3),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input)
],
REGISTER_OUTCOME_STEP_3: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
register_outcome_step_4),
],
REGISTER_OUTCOME_STEP_4: [
MessageHandler(filters.Regex(
"^(Yes|No)$"), register_outcome_step_5),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input)
],
COMPLETE_OUTCOME: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
complete_outcome),
],
REGISTER_TRANSFER_STEP_1: [
MessageHandler(filters.Regex(
"^(Account 1|Account 2|Account 3)$"), register_transfer_step_2),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input)
],
REGISTER_TRANSFER_STEP_2: [
MessageHandler(filters.Regex(
"^(Account 1|Account 2|Account 3)$"), register_transfer_step_3),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input)
],
REGISTER_TRANSFER_STEP_3: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
register_transfer_step_4)
],
REGISTER_TRANSFER_STEP_4: [
MessageHandler(filters.Regex("^(Yes|No)$"),
register_transfer_step_5),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input),
],
COMPLETE_TRANSFER: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
complete_transfer)
],
DISPLAY_REPORT_STEP_1: [
MessageHandler(filters.Regex("^(Total Incomes|Total Outcomes|Total Transfers)$"),
display_report_step_2),
MessageHandler(filters.TEXT & ~filters.COMMAND, invalid_input)
],
},
fallbacks=[CommandHandler("start", start)],
)
application.add_handler(conversation_handler)
application.run_polling()
if __name__ == "__main__":
main()