diff --git a/README.md b/README.md index 4995d77..ed33dd7 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,9 @@ MASTER_TELE_ID=your_telegram_id ### Step 3 * Proceed to project directory and run: ``` python -python3.9 test.py +python3.9 test_polling.py +OR +python3.9 test_webhook.py ``` ## Usage diff --git a/bot/google_sheet_service.py b/bot/google_sheet_service.py index 2e0b981..82f7cf1 100644 --- a/bot/google_sheet_service.py +++ b/bot/google_sheet_service.py @@ -153,14 +153,16 @@ def create_entry(spreadsheet_id, month, row_tracker, row_data): def get_sheet_id_by_title(spreadsheet_id, title_to_find): - sheet_metadata = sheets_api.spreadsheets().get(spreadsheetId=spreadsheet_id).execute() - sheets = sheet_metadata.get('sheets', '') - + sheet_metadata = ( + sheets_api.spreadsheets().get(spreadsheetId=spreadsheet_id).execute() + ) + sheets = sheet_metadata.get("sheets", "") + for sheet in sheets: title = sheet.get("properties", {}).get("title") if title == title_to_find: return sheet.get("properties", {}).get("sheetId") - + return None @@ -171,7 +173,9 @@ def create_backlog_entry(spreadsheet_id, backlog_day, backlog_month, row_data): category = row_data[3].strip() payment = row_data[4].strip() - day_first_entry_index = get_day_first_entry_index(spreadsheet_id, backlog_month, backlog_day) + day_first_entry_index = get_day_first_entry_index( + spreadsheet_id, backlog_month, backlog_day + ) row_to_move = int(get_first_row_to_move(spreadsheet_id, backlog_month, backlog_day)) last_row_to_move = int(get_last_entered_row(spreadsheet_id, backlog_month)) new_entry_row = row_to_move @@ -185,33 +189,31 @@ def create_backlog_entry(spreadsheet_id, backlog_day, backlog_month, row_data): "copyPaste": { "source": { "sheetId": sheet_id, - "startRowIndex": row_to_move - 1, - "endRowIndex": last_row_to_move, - "startColumnIndex": start_column_index, - "endColumnIndex": end_column_index + "startRowIndex": row_to_move - 1, + "endRowIndex": last_row_to_move, + "startColumnIndex": start_column_index, + "endColumnIndex": end_column_index, }, "destination": { "sheetId": sheet_id, - "startRowIndex": row_to_move, - "endRowIndex": last_row_to_move + 1, - "startColumnIndex": start_column_index, - "endColumnIndex": end_column_index + "startRowIndex": row_to_move, + "endRowIndex": last_row_to_move + 1, + "startColumnIndex": start_column_index, + "endColumnIndex": end_column_index, }, "pasteType": "PASTE_NORMAL", - "pasteOrientation": "NORMAL" + "pasteOrientation": "NORMAL", } } ] sheets_api.spreadsheets().batchUpdate( - spreadsheetId=spreadsheet_id, - body={"requests": requests} + spreadsheetId=spreadsheet_id, body={"requests": requests} ).execute() clear_range = f"{backlog_month}!A{new_entry_row}:K{new_entry_row}" sheets_api.spreadsheets().values().clear( - spreadsheetId=spreadsheet_id, - range=clear_range + spreadsheetId=spreadsheet_id, range=clear_range ).execute() if day_first_entry_index is None: @@ -227,18 +229,15 @@ def create_backlog_entry(spreadsheet_id, backlog_day, backlog_month, row_data): data = [price] + remarks_list + [category, payment] body = {"values": [data]} - range_name = ( - f"{backlog_month}!{sheet_column_start}{new_entry_row}:{sheet_column_end}{new_entry_row}" - ) + range_name = f"{backlog_month}!{sheet_column_start}{new_entry_row}:{sheet_column_end}{new_entry_row}" sheets_api.spreadsheets().values().update( spreadsheetId=spreadsheet_id, range=range_name, valueInputOption="USER_ENTERED", body=body, ).execute() - - update_prev_day(spreadsheet_id, backlog_month, day_first_entry_index, new_entry_row) + update_prev_day(spreadsheet_id, backlog_month, day_first_entry_index, new_entry_row) def get_trackers(spreadsheet_id): @@ -449,11 +448,11 @@ def get_first_row_to_move(spreadsheet_id, month, date): next_date = str(int(date) + 1) while next_date not in flat_list and int(next_date) < 32: next_date = str(int(next_date) + 1) - + try: last_row = flat_list.index(next_date) except ValueError: - return None + return get_last_entered_row(spreadsheet_id, month) + 1 return last_row + 1 @@ -470,9 +469,10 @@ def get_day_first_entry_index(spreadsheet_id, month, date): return None first_row = flat_list.index(date) first_row += 1 - + return first_row + def get_work_place(spreadsheet_id): result = ( sheets_api.spreadsheets() diff --git a/bot/telegram_bot.py b/bot/telegram_bot.py index 3dc7948..9103e79 100644 --- a/bot/telegram_bot.py +++ b/bot/telegram_bot.py @@ -57,6 +57,7 @@ def start(update, context): update.message.reply_text(SETUP_TEXT, parse_mode=ParseMode.HTML) return CS.SET_UP except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -197,6 +198,7 @@ def config_setup(update, context) -> int: ) return CS.CONFIG_CATEGORY except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END update.callback_query.edit_message_text(END_TEXT, reply_markup=None) @@ -231,6 +233,7 @@ def config_category(update, context) -> int: ) return CS.CONFIG_SUBCATEGORY except Exception as e: + print(e) update.callback_query.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -253,6 +256,7 @@ def config_subcategory(update, context) -> int: ) return CS.CONFIG_PAYMENT except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -272,6 +276,7 @@ def config_payment(update, context) -> int: ) return CS.CONFIG_SUBPAYMENT except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -297,6 +302,7 @@ def config_subpayment(update, context) -> int: ) return ConversationHandler.END except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -356,6 +362,7 @@ def remarks(update: Update, context) -> int: ) return CS.CATEGORY except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -400,6 +407,7 @@ def category(update, context) -> int: ) return CS.PAYMENT except Exception as e: + print(e) update.callback_query.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -426,6 +434,7 @@ def subcategory(update, context) -> int: ) return CS.PAYMENT except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -457,6 +466,7 @@ def payment(update, context) -> int: update.callback_query.message.reply_text("Transaction logged.") return ConversationHandler.END except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -484,6 +494,7 @@ def subpayment(update, context) -> int: return ConversationHandler.END except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -596,6 +607,7 @@ def add_transport(update, context): context.user_data["sheet_id"], EntryType.TRANSPORT ) except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END if not setting_list or not setting_list[0]: @@ -632,6 +644,7 @@ def add_others(update, context): context.user_data["sheet_id"], EntryType.OTHERS ) except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END if not setting_list or not setting_list[0]: @@ -683,6 +696,7 @@ def quick_add(update, context) -> int: update.message.reply_text(ERROR_TEXT) return ConversationHandler.END except Exception as e: + print(e) update.message.reply_text("Please follow the format and try again.") return CS.QUICK_ADD @@ -699,6 +713,7 @@ def get_day_transaction(update, context): update.message.reply_text(GET_TRANSACTION_TEXT) return CS.HANDLE_GET_TRANSACTION except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -711,6 +726,7 @@ def get_overall(update, context): update.message.reply_text(GET_OVERALL_TEXT) return CS.HANDLE_GET_OVERALL except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -753,6 +769,7 @@ def handle_get_transaction(update, context): update.message.reply_text(GET_TRANSACTION_TEXT) return CS.HANDLE_GET_TRANSACTION except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -789,6 +806,7 @@ def handle_get_overall(update, context): update.message.reply_text(GET_OVERALL_TEXT) return CS.HANDLE_GET_OVERALL except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -800,6 +818,7 @@ def add_income(update, context): context.user_data["sheet_id"] = db.get_user_sheet_id(telegram_id) update.message.reply_text(ADD_INCOME_TEXT) except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END return CS.INCOME @@ -828,6 +847,7 @@ def income(update, context) -> int: ) return CS.WORK_PLACE except Exception as e: + print(e) update.message.reply_text(ERROR_TEXT) return ConversationHandler.END @@ -861,6 +881,7 @@ def cpf(update, context) -> int: update.callback_query.message.reply_text(INCOME_LIMIT_TEXT) return ConversationHandler.END except Exception as e: + print(e) update.callback_query.message.reply_text(ERROR_TEXT) return ConversationHandler.END diff --git a/release_notes.md b/release_notes.md index 0cdb5f4..a53eaea 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,4 +1,8 @@ # Release Notes +## Version 2.2.0 - Date 27 Jan 2024 +### Bug Fix 🛠️ +- Fix backlog bug + ## Version 2.1.9 - Date 23 Dec 2023 ### For Developer 🧑‍💻 - Push features message to all users using `/notifyall` command (requires your telegram id) diff --git a/test_polling.py b/test_polling.py index 3d009de..cbacf5b 100644 --- a/test_polling.py +++ b/test_polling.py @@ -4,10 +4,13 @@ from bot.telegram_bot import setup_handlers # Enable logging and set the file to write logs to -log_file = 'telegram_bot.log' -logging.basicConfig(filename=log_file, filemode='a', - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', - level=logging.INFO) +log_file = "telegram_bot.log" +logging.basicConfig( + filename=log_file, + filemode="a", + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + level=logging.INFO, +) logger = logging.getLogger(__name__) diff --git a/test_webhook.py b/test_webhook.py index 336a8f2..b42f9c0 100644 --- a/test_webhook.py +++ b/test_webhook.py @@ -4,12 +4,23 @@ import os from bot.telegram_bot import setup_handlers from pyngrok import ngrok +import logging TOKEN = os.environ.get("TEST_TOKEN") app = Flask(__name__) updater = Updater(token=TOKEN) dispatcher = updater.dispatcher +log_file = "telegram_bot.log" +logging.basicConfig( + filename=log_file, + filemode="a", + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + level=logging.INFO, +) + +logger = logging.getLogger(__name__) + @app.route("/webhook", methods=["POST"]) def webhook():