Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 26 additions & 26 deletions bot/google_sheet_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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


Expand All @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 7 additions & 4 deletions test_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
11 changes: 11 additions & 0 deletions test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down