From 2d0dfc31fe946f4a89e09d8f077ab3098bbb1ddf Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 10 May 2021 13:09:55 -0700 Subject: [PATCH] catch exceptions where user hasnt started --- tipbot/commands/balance.py | 4 ++-- tipbot/commands/debug.py | 2 +- tipbot/commands/deposit.py | 22 ++++++++++++++------- tipbot/commands/help.py | 5 +++-- tipbot/commands/mining.py | 3 --- tipbot/commands/register.py | 31 ++++++++++++++++-------------- tipbot/commands/tip.py | 38 ++++++++++++++++++------------------- tipbot/commands/withdraw.py | 36 +++++++++++++++++------------------ tipbot/helpers/utils.py | 15 +++++++++++++++ 9 files changed, 90 insertions(+), 66 deletions(-) diff --git a/tipbot/commands/balance.py b/tipbot/commands/balance.py index a09eb3f..70e4c5e 100644 --- a/tipbot/commands/balance.py +++ b/tipbot/commands/balance.py @@ -2,6 +2,7 @@ import logging from tipbot import wownero from tipbot import db from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug +from tipbot.helpers.utils import reply_user @wallet_rpc_required @@ -14,5 +15,4 @@ def balance(update, context): unlocked = float(balances[1]) locked = float(balances[0] - balances[1]) msg = f'Available balance for {u.telegram_user}: {unlocked} WOW ({locked} WOW locked)' - update.message.from_user.send_message(msg) - update.message.delete() + reply_user(update.message, context, msg) diff --git a/tipbot/commands/debug.py b/tipbot/commands/debug.py index b4e4b36..40febcf 100644 --- a/tipbot/commands/debug.py +++ b/tipbot/commands/debug.py @@ -7,7 +7,7 @@ from tipbot.wownero import Wallet @log_event @check_debug def debug(update, context): - if is_tg_admin(update.message.from_user['id']): + if is_tg_admin(update.message.from_user.id): update.message.reply_text('ohai') else: update.message.reply_text('you cant do that.') diff --git a/tipbot/commands/deposit.py b/tipbot/commands/deposit.py index ef01780..3c9c204 100644 --- a/tipbot/commands/deposit.py +++ b/tipbot/commands/deposit.py @@ -1,5 +1,6 @@ import logging from telegram import ParseMode +from telegram.error import Unauthorized from tipbot import wownero from tipbot import db from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug @@ -11,11 +12,18 @@ from tipbot.helpers.utils import generate_qr @log_event @check_debug def deposit(update, context): - u = db.User.get(telegram_id=update.message.from_user['id']) + u = db.User.get(telegram_id=update.message.from_user.id) address = wownero.Wallet().addresses(account=u.account_index)[0] - update.message.from_user.send_photo( - photo=generate_qr(address), - caption=f'{u.telegram_user}\'s deposit address: {address}', - quote=False - ) - update.message.delete() + try: + update.message.from_user.send_photo( + photo=generate_qr(address), + caption=f'{u.telegram_user}\'s deposit address: {address}', + quote=False + ) + update.message.delete() + except Unauthorized: + update.message.reply_text(f'You have to initiate a convo with the bot first: https://t.me/{context.bot.username}') + update.message.delete() + except: + update.message.reply_text(f'Something borked -_-') + update.message.delete() diff --git a/tipbot/commands/help.py b/tipbot/commands/help.py index 0259325..6d3c405 100644 --- a/tipbot/commands/help.py +++ b/tipbot/commands/help.py @@ -1,4 +1,5 @@ from tipbot import commands +from tipbot.helpers.utils import reply_user def help(update, context): @@ -10,5 +11,5 @@ def help(update, context): example=pk['example'], help=pk['help'] )) - update.message.from_user.send_message('Here are the available commands for this bot:\n\n' + '\n\n'.join(cmds)) - update.message.delete() + msg = 'Here are the available commands for this bot:\n\n' + '\n\n'.join(cmds) + reply_user(update.message, context, msg) diff --git a/tipbot/commands/mining.py b/tipbot/commands/mining.py index 3bc3326..b8d5d83 100644 --- a/tipbot/commands/mining.py +++ b/tipbot/commands/mining.py @@ -10,9 +10,6 @@ from tipbot import config @log_event @check_debug def mine(update, context): - # First delete message to remove clutter - update.message.delete() - # Check that enough arguments were provided if len(context.args) < 1: update.message.reply_text('Not enough arguments passed. Include hash rate per second to estimate.') diff --git a/tipbot/commands/register.py b/tipbot/commands/register.py index e9dd253..fc0d95c 100644 --- a/tipbot/commands/register.py +++ b/tipbot/commands/register.py @@ -2,6 +2,7 @@ import logging from tipbot import wownero from tipbot.db import User from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug +from tipbot.helpers.utils import reply_user @wallet_rpc_required @@ -13,33 +14,34 @@ def register(update, context): uid = msg.from_user['id'] un = getattr(msg.from_user, 'username', None) if un is None: - msg.from_user.send_message('You need a username configured in Telegram to use this bot.') - msg.delete() + _m = 'You need a username configured in Telegram to use this bot.' + reply_user(msg, context, _m) return False if User.filter(telegram_id=uid): if User.filter(telegram_id=uid, telegram_user=un): - msg.from_user.send_message('You are already registered. Use /help to see available bot commands.') - msg.delete() + _m = 'You are already registered. Use /help to see available bot commands.' + reply_user(msg, context, _m) else: try: u = User.get(telegram_id=uid) u.telegram_user = un u.save() - msg.from_user.send_message(f'You have been registered again as Telegram ID {uid} but with username {un}.') - msg.delete() + _m = f'You have been registered again as Telegram ID {uid} but with username {un}.' + reply_user(msg, context, _m) + return True except Exception as e: logging.error(f'Unable to update user in DB: {e}. Debug: {msg}') - msg.from_user.send_message('Unable to update your existing account. Ask for help.') - msg.delete() + _m = 'Unable to update your existing account. Ask for help.' + reply_user(msg, context, _m) return False else: try: account = wallet.new_account(label=un) except Exception as e: logging.error(f'Unable to create a new account in wallet RPC: {e}. Debug: {msg}') - msg.from_user.send_message('Unable to create a new account for you. Ask for help.') - msg.delete() + _m = 'Unable to create a new account for you. Ask for help.' + reply_user(msg, context, _m) return False try: u = User( @@ -53,10 +55,11 @@ def register(update, context): f'You have been registered as Telegram ID {uid} and username {un} and can now send and receive tips.', 'Ask for /help to see all available bot commands. Maybe start with /deposit to get your deposit address.' ] - msg.from_user.send_message(' '.join(reply_text)) - msg.delete() + _m = ' '.join(reply_text) + reply_user(msg, context, _m) + return True except Exception as e: logging.error(f'Unable to register user in DB: {e}. Debug: {msg}') - msg.from_user.send_message('Unable to create a new account for you. Ask for help.') - msg.delete() + _m = 'Unable to create a new account for you. Ask for help.' + reply_user(msg, context, _m) return False diff --git a/tipbot/commands/tip.py b/tipbot/commands/tip.py index 0a3c9ac..673d604 100644 --- a/tipbot/commands/tip.py +++ b/tipbot/commands/tip.py @@ -3,6 +3,7 @@ from decimal import Decimal from tipbot import wownero from tipbot import db from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug +from tipbot.helpers.utils import reply_user @wallet_rpc_required @@ -11,8 +12,8 @@ from tipbot.helpers.decorators import wallet_rpc_required, log_event, registrati @check_debug def tip(update, context): if len(context.args) < 2: - update.message.from_user.send_message('Not enough arguments passed.') - update.message.delete() + _m = 'Not enough arguments passed.' + reply_user(update.message, context, _m) return False elif len(context.args) == 2: message = "" @@ -26,8 +27,8 @@ def tip(update, context): target_un = context.args[0] if target_un == update.message.from_user['first_name']: - update.message.from_user.send_message('You cannot tip yourself!') - update.message.delete() + _m = 'You cannot tip yourself!' + reply_user(update.message, context, _m) return False if not db.User.filter(telegram_user=target_un): @@ -35,28 +36,28 @@ def tip(update, context): 'That user has not registered and cannot receive tips yet.', 'If they would like to receive a tip, have them /register with the bot.' ] - update.message.from_user.send_message(' '.join(reply_text)) - update.message.delete() + _m = ' '.join(reply_text) + reply_user(update.message, context, _m) return False # validate amount try: amount = Decimal(context.args[1]) except: - update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.') - update.message.delete() + _m = 'Bad Wownero amount specified; not a valid number.' + reply_user(update.message, context, _m) return False if amount < 1: - update.message.from_user.send_message('Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.') - update.message.delete() + _m = 'Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.' + reply_user(update.message, context, _m) return False tipper = db.User.get(telegram_id=update.message.from_user['id']) tipper_balances = wownero.Wallet().balances(account=tipper.account_index) if amount >= tipper_balances[1]: - update.message.from_user.send_message(f'You do not have sufficient funds to send {amount} WOW. Check your /balance') - update.message.delete() + _m = f'You do not have sufficient funds to send {amount} WOW. Check your /balance' + reply_user(update.message, context, _m) return False # get target user details @@ -68,14 +69,13 @@ def tip(update, context): tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=tipper.account_index) if 'tx_hash' in tx: h = tx['tx_hash'] - msg = f'Tipped @{target_un} {amount} WOW! Tx: {h}' - update.message.from_user.send_message(msg) - update.message.delete() + _m = f'Tipped @{target_un} {amount} WOW! Tx: {h}' + reply_user(update.message, context, _m) else: logging.error(f'Transaction failure details for {tipper.telegram_user} ({tipper.telegram_id}): {tx}') - update.message.from_user.send_message(f'Failed to send a tip. Reason: "{tx["message"]}"') - update.message.delete() + _m = f'Failed to send a tip. Reason: {tx["message"]}' + reply_user(update.message, context, _m) except Exception as e: logging.error(f'Unable to send transfer: {e}. Debug: {update.message}') - update.message.from_user.send_message('Failed to send a tip. Ask for help.') - update.message.delete() + _m = 'Failed to send a tip. Ask for help.' + reply_user(update.message, context, _m) diff --git a/tipbot/commands/withdraw.py b/tipbot/commands/withdraw.py index b2b550d..f9f340e 100644 --- a/tipbot/commands/withdraw.py +++ b/tipbot/commands/withdraw.py @@ -3,6 +3,7 @@ from decimal import Decimal from tipbot import wownero from tipbot import db from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug +from tipbot.helpers.utils import reply_user @wallet_rpc_required @@ -11,36 +12,36 @@ from tipbot.helpers.decorators import wallet_rpc_required, log_event, registrati @check_debug def withdraw(update, context): if len(context.args) < 2: - update.message.from_user.send_message('Not enough arguments passed.') - update.message.delete() + _m = 'Not enough arguments passed.' + reply_user(update.message, context, _m) return False # validate address if len(context.args[0]) in [97, 108]: address = context.args[0] else: - update.message.from_user.send_message('This does not look like a valid Wownero address. Try again.') - update.message.delete() + _m = 'This does not look like a valid Wownero address. Try again.' + reply_user(update.message, context, _m) return False # validate amount try: amount = Decimal(context.args[1]) except: - update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.') - update.message.delete() + _m = 'Bad Wownero amount specified; not a valid number.' + reply_user(update.message, context, _m) return False if amount < 1: - update.message.from_user.send_message('Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.') - update.message.delete() + _m = 'Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.' + reply_user(update.message, context, _m) return False - sender = db.User.get(telegram_id=update.message.from_user['id']) + sender = db.User.get(telegram_id=update.message.from_user.id) sender_balances = wownero.Wallet().balances(account=sender.account_index) if amount > sender_balances[1]: - update.message.from_user.send_message(f'You do not have sufficient funds to withdraw {amount} WOW. Check your /balance') - update.message.delete() + _m = f'You do not have sufficient funds to withdraw {amount} WOW. Check your /balance' + reply_user(update.message, context, _m) return False # transfer funds to given address @@ -48,14 +49,13 @@ def withdraw(update, context): tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=sender.account_index) if 'tx_hash' in tx: h = tx['tx_hash'] - msg = f'Sent {amount} WOW! Tx: {h}' - update.message.from_user.send_message(msg) - update.message.delete() + _m = f'Sent {amount} WOW! Tx: {h}' + reply_user(update.message, context, _m) else: logging.error(f'Transaction failure details for {sender.telegram_user} ({sender.telegram_id}): {tx}') - update.message.from_user.send_message(f'Failed to withdraw Wownero. Reason: "{tx["message"]}"') - update.message.delete() + _m = f'Failed to withdraw Wownero. Reason: "{tx["message"]}"' + reply_user(update.message, context, _m) except Exception as e: logging.error(f'Unable to withdraw transfer: {e}. Debug: {update.message}') - update.message.from_user.send_message('Failed to withdraw Wownero. Ask for help.') - update.message.delete() + _m = 'Failed to withdraw Wownero. Ask for help.' + reply_user(update.message, context, _m) diff --git a/tipbot/helpers/utils.py b/tipbot/helpers/utils.py index fb3bcd0..9fbd81f 100644 --- a/tipbot/helpers/utils.py +++ b/tipbot/helpers/utils.py @@ -3,6 +3,7 @@ from io import BytesIO from PIL import Image from base64 import b64encode from qrcode import make as qrcode_make +from telegram.error import Unauthorized from tipbot import config @@ -17,3 +18,17 @@ def generate_qr(s): qrcode_make(s).save(_address_qr, format="PNG") _address_qr.seek(0) return _address_qr + +def reply_user(msg, context, text, pm=True, delete=True): + try: + if pm: + msg.from_user.send_message(text) + else: + msg.reply_text(text) + except Unauthorized: + msg.reply_text(f'@{msg.from_user.username}: You have to initiate a convo with the bot first: https://t.me/{context.bot.username}') + except: + msg.reply_text(f'@{msg.from_user.username}: Something borked -_-') + + if delete: + msg.delete()