Skip to content

Commit 9d5f84e

Browse files
authored
[BUGFIX] backlog (#35)
1 parent 89d95f6 commit 9d5f84e

File tree

6 files changed

+72
-31
lines changed

6 files changed

+72
-31
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ MASTER_TELE_ID=your_telegram_id
5353
### Step 3
5454
* Proceed to project directory and run:
5555
``` python
56-
python3.9 test.py
56+
python3.9 test_polling.py
57+
OR
58+
python3.9 test_webhook.py
5759
```
5860

5961
## Usage

bot/google_sheet_service.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,16 @@ def create_entry(spreadsheet_id, month, row_tracker, row_data):
153153

154154

155155
def get_sheet_id_by_title(spreadsheet_id, title_to_find):
156-
sheet_metadata = sheets_api.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
157-
sheets = sheet_metadata.get('sheets', '')
158-
156+
sheet_metadata = (
157+
sheets_api.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
158+
)
159+
sheets = sheet_metadata.get("sheets", "")
160+
159161
for sheet in sheets:
160162
title = sheet.get("properties", {}).get("title")
161163
if title == title_to_find:
162164
return sheet.get("properties", {}).get("sheetId")
163-
165+
164166
return None
165167

166168

@@ -171,7 +173,9 @@ def create_backlog_entry(spreadsheet_id, backlog_day, backlog_month, row_data):
171173
category = row_data[3].strip()
172174
payment = row_data[4].strip()
173175

174-
day_first_entry_index = get_day_first_entry_index(spreadsheet_id, backlog_month, backlog_day)
176+
day_first_entry_index = get_day_first_entry_index(
177+
spreadsheet_id, backlog_month, backlog_day
178+
)
175179
row_to_move = int(get_first_row_to_move(spreadsheet_id, backlog_month, backlog_day))
176180
last_row_to_move = int(get_last_entered_row(spreadsheet_id, backlog_month))
177181
new_entry_row = row_to_move
@@ -185,33 +189,31 @@ def create_backlog_entry(spreadsheet_id, backlog_day, backlog_month, row_data):
185189
"copyPaste": {
186190
"source": {
187191
"sheetId": sheet_id,
188-
"startRowIndex": row_to_move - 1,
189-
"endRowIndex": last_row_to_move,
190-
"startColumnIndex": start_column_index,
191-
"endColumnIndex": end_column_index
192+
"startRowIndex": row_to_move - 1,
193+
"endRowIndex": last_row_to_move,
194+
"startColumnIndex": start_column_index,
195+
"endColumnIndex": end_column_index,
192196
},
193197
"destination": {
194198
"sheetId": sheet_id,
195-
"startRowIndex": row_to_move,
196-
"endRowIndex": last_row_to_move + 1,
197-
"startColumnIndex": start_column_index,
198-
"endColumnIndex": end_column_index
199+
"startRowIndex": row_to_move,
200+
"endRowIndex": last_row_to_move + 1,
201+
"startColumnIndex": start_column_index,
202+
"endColumnIndex": end_column_index,
199203
},
200204
"pasteType": "PASTE_NORMAL",
201-
"pasteOrientation": "NORMAL"
205+
"pasteOrientation": "NORMAL",
202206
}
203207
}
204208
]
205209

206210
sheets_api.spreadsheets().batchUpdate(
207-
spreadsheetId=spreadsheet_id,
208-
body={"requests": requests}
211+
spreadsheetId=spreadsheet_id, body={"requests": requests}
209212
).execute()
210213

211214
clear_range = f"{backlog_month}!A{new_entry_row}:K{new_entry_row}"
212215
sheets_api.spreadsheets().values().clear(
213-
spreadsheetId=spreadsheet_id,
214-
range=clear_range
216+
spreadsheetId=spreadsheet_id, range=clear_range
215217
).execute()
216218

217219
if day_first_entry_index is None:
@@ -227,18 +229,15 @@ def create_backlog_entry(spreadsheet_id, backlog_day, backlog_month, row_data):
227229
data = [price] + remarks_list + [category, payment]
228230

229231
body = {"values": [data]}
230-
range_name = (
231-
f"{backlog_month}!{sheet_column_start}{new_entry_row}:{sheet_column_end}{new_entry_row}"
232-
)
232+
range_name = f"{backlog_month}!{sheet_column_start}{new_entry_row}:{sheet_column_end}{new_entry_row}"
233233
sheets_api.spreadsheets().values().update(
234234
spreadsheetId=spreadsheet_id,
235235
range=range_name,
236236
valueInputOption="USER_ENTERED",
237237
body=body,
238238
).execute()
239-
240-
update_prev_day(spreadsheet_id, backlog_month, day_first_entry_index, new_entry_row)
241239

240+
update_prev_day(spreadsheet_id, backlog_month, day_first_entry_index, new_entry_row)
242241

243242

244243
def get_trackers(spreadsheet_id):
@@ -449,11 +448,11 @@ def get_first_row_to_move(spreadsheet_id, month, date):
449448
next_date = str(int(date) + 1)
450449
while next_date not in flat_list and int(next_date) < 32:
451450
next_date = str(int(next_date) + 1)
452-
451+
453452
try:
454453
last_row = flat_list.index(next_date)
455454
except ValueError:
456-
return None
455+
return get_last_entered_row(spreadsheet_id, month) + 1
457456
return last_row + 1
458457

459458

@@ -470,9 +469,10 @@ def get_day_first_entry_index(spreadsheet_id, month, date):
470469
return None
471470
first_row = flat_list.index(date)
472471
first_row += 1
473-
472+
474473
return first_row
475474

475+
476476
def get_work_place(spreadsheet_id):
477477
result = (
478478
sheets_api.spreadsheets()

bot/telegram_bot.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def start(update, context):
5757
update.message.reply_text(SETUP_TEXT, parse_mode=ParseMode.HTML)
5858
return CS.SET_UP
5959
except Exception as e:
60+
print(e)
6061
update.message.reply_text(ERROR_TEXT)
6162
return ConversationHandler.END
6263

@@ -197,6 +198,7 @@ def config_setup(update, context) -> int:
197198
)
198199
return CS.CONFIG_CATEGORY
199200
except Exception as e:
201+
print(e)
200202
update.callback_query.message.reply_text(ERROR_TEXT)
201203
return ConversationHandler.END
202204
update.callback_query.edit_message_text(END_TEXT, reply_markup=None)
@@ -231,6 +233,7 @@ def config_category(update, context) -> int:
231233
)
232234
return CS.CONFIG_SUBCATEGORY
233235
except Exception as e:
236+
print(e)
234237
update.callback_query.reply_text(ERROR_TEXT)
235238
return ConversationHandler.END
236239

@@ -253,6 +256,7 @@ def config_subcategory(update, context) -> int:
253256
)
254257
return CS.CONFIG_PAYMENT
255258
except Exception as e:
259+
print(e)
256260
update.callback_query.message.reply_text(ERROR_TEXT)
257261
return ConversationHandler.END
258262

@@ -272,6 +276,7 @@ def config_payment(update, context) -> int:
272276
)
273277
return CS.CONFIG_SUBPAYMENT
274278
except Exception as e:
279+
print(e)
275280
update.callback_query.message.reply_text(ERROR_TEXT)
276281
return ConversationHandler.END
277282

@@ -297,6 +302,7 @@ def config_subpayment(update, context) -> int:
297302
)
298303
return ConversationHandler.END
299304
except Exception as e:
305+
print(e)
300306
update.callback_query.message.reply_text(ERROR_TEXT)
301307
return ConversationHandler.END
302308

@@ -356,6 +362,7 @@ def remarks(update: Update, context) -> int:
356362
)
357363
return CS.CATEGORY
358364
except Exception as e:
365+
print(e)
359366
update.message.reply_text(ERROR_TEXT)
360367
return ConversationHandler.END
361368

@@ -400,6 +407,7 @@ def category(update, context) -> int:
400407
)
401408
return CS.PAYMENT
402409
except Exception as e:
410+
print(e)
403411
update.callback_query.reply_text(ERROR_TEXT)
404412
return ConversationHandler.END
405413

@@ -426,6 +434,7 @@ def subcategory(update, context) -> int:
426434
)
427435
return CS.PAYMENT
428436
except Exception as e:
437+
print(e)
429438
update.callback_query.message.reply_text(ERROR_TEXT)
430439
return ConversationHandler.END
431440

@@ -457,6 +466,7 @@ def payment(update, context) -> int:
457466
update.callback_query.message.reply_text("Transaction logged.")
458467
return ConversationHandler.END
459468
except Exception as e:
469+
print(e)
460470
update.callback_query.message.reply_text(ERROR_TEXT)
461471
return ConversationHandler.END
462472

@@ -484,6 +494,7 @@ def subpayment(update, context) -> int:
484494
return ConversationHandler.END
485495

486496
except Exception as e:
497+
print(e)
487498
update.callback_query.message.reply_text(ERROR_TEXT)
488499
return ConversationHandler.END
489500

@@ -596,6 +607,7 @@ def add_transport(update, context):
596607
context.user_data["sheet_id"], EntryType.TRANSPORT
597608
)
598609
except Exception as e:
610+
print(e)
599611
update.callback_query.message.reply_text(ERROR_TEXT)
600612
return ConversationHandler.END
601613
if not setting_list or not setting_list[0]:
@@ -632,6 +644,7 @@ def add_others(update, context):
632644
context.user_data["sheet_id"], EntryType.OTHERS
633645
)
634646
except Exception as e:
647+
print(e)
635648
update.callback_query.message.reply_text(ERROR_TEXT)
636649
return ConversationHandler.END
637650
if not setting_list or not setting_list[0]:
@@ -683,6 +696,7 @@ def quick_add(update, context) -> int:
683696
update.message.reply_text(ERROR_TEXT)
684697
return ConversationHandler.END
685698
except Exception as e:
699+
print(e)
686700
update.message.reply_text("Please follow the format and try again.")
687701
return CS.QUICK_ADD
688702

@@ -699,6 +713,7 @@ def get_day_transaction(update, context):
699713
update.message.reply_text(GET_TRANSACTION_TEXT)
700714
return CS.HANDLE_GET_TRANSACTION
701715
except Exception as e:
716+
print(e)
702717
update.message.reply_text(ERROR_TEXT)
703718
return ConversationHandler.END
704719

@@ -711,6 +726,7 @@ def get_overall(update, context):
711726
update.message.reply_text(GET_OVERALL_TEXT)
712727
return CS.HANDLE_GET_OVERALL
713728
except Exception as e:
729+
print(e)
714730
update.message.reply_text(ERROR_TEXT)
715731
return ConversationHandler.END
716732

@@ -753,6 +769,7 @@ def handle_get_transaction(update, context):
753769
update.message.reply_text(GET_TRANSACTION_TEXT)
754770
return CS.HANDLE_GET_TRANSACTION
755771
except Exception as e:
772+
print(e)
756773
update.message.reply_text(ERROR_TEXT)
757774
return ConversationHandler.END
758775

@@ -789,6 +806,7 @@ def handle_get_overall(update, context):
789806
update.message.reply_text(GET_OVERALL_TEXT)
790807
return CS.HANDLE_GET_OVERALL
791808
except Exception as e:
809+
print(e)
792810
update.message.reply_text(ERROR_TEXT)
793811
return ConversationHandler.END
794812

@@ -800,6 +818,7 @@ def add_income(update, context):
800818
context.user_data["sheet_id"] = db.get_user_sheet_id(telegram_id)
801819
update.message.reply_text(ADD_INCOME_TEXT)
802820
except Exception as e:
821+
print(e)
803822
update.message.reply_text(ERROR_TEXT)
804823
return ConversationHandler.END
805824
return CS.INCOME
@@ -828,6 +847,7 @@ def income(update, context) -> int:
828847
)
829848
return CS.WORK_PLACE
830849
except Exception as e:
850+
print(e)
831851
update.message.reply_text(ERROR_TEXT)
832852
return ConversationHandler.END
833853

@@ -861,6 +881,7 @@ def cpf(update, context) -> int:
861881
update.callback_query.message.reply_text(INCOME_LIMIT_TEXT)
862882
return ConversationHandler.END
863883
except Exception as e:
884+
print(e)
864885
update.callback_query.message.reply_text(ERROR_TEXT)
865886
return ConversationHandler.END
866887

release_notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Release Notes
2+
## Version 2.2.0 - Date 27 Jan 2024
3+
### Bug Fix 🛠️
4+
- Fix backlog bug
5+
26
## Version 2.1.9 - Date 23 Dec 2023
37
### For Developer 🧑‍💻
48
- Push features message to all users using `/notifyall` command (requires your telegram id)

test_polling.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
from bot.telegram_bot import setup_handlers
55

66
# Enable logging and set the file to write logs to
7-
log_file = 'telegram_bot.log'
8-
logging.basicConfig(filename=log_file, filemode='a',
9-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
10-
level=logging.INFO)
7+
log_file = "telegram_bot.log"
8+
logging.basicConfig(
9+
filename=log_file,
10+
filemode="a",
11+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
12+
level=logging.INFO,
13+
)
1114

1215
logger = logging.getLogger(__name__)
1316

test_webhook.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@
44
import os
55
from bot.telegram_bot import setup_handlers
66
from pyngrok import ngrok
7+
import logging
78

89
TOKEN = os.environ.get("TEST_TOKEN")
910
app = Flask(__name__)
1011
updater = Updater(token=TOKEN)
1112
dispatcher = updater.dispatcher
1213

14+
log_file = "telegram_bot.log"
15+
logging.basicConfig(
16+
filename=log_file,
17+
filemode="a",
18+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
19+
level=logging.INFO,
20+
)
21+
22+
logger = logging.getLogger(__name__)
23+
1324

1425
@app.route("/webhook", methods=["POST"])
1526
def webhook():

0 commit comments

Comments
 (0)