diff --git a/tipbot/commands/balance.py b/tipbot/commands/balance.py index 91d96d8..a09eb3f 100644 --- a/tipbot/commands/balance.py +++ b/tipbot/commands/balance.py @@ -9,8 +9,10 @@ from tipbot.helpers.decorators import wallet_rpc_required, log_event, registrati @log_event @check_debug def balance(update, context): - u = db.User.get(telegram_id=update.message.from_user['id']) + u = db.User.get(telegram_id=update.message.from_user.id) balances = wownero.Wallet().balances(account=u.account_index) - unlocked = balances[1] - locked = balances[0] - balances[1] - update.message.reply_text(f'Available balance for {u.telegram_user}: {float(unlocked)} WOW ({float(locked)} WOW locked)') + 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() diff --git a/tipbot/commands/deposit.py b/tipbot/commands/deposit.py index 39b3a7d..ef01780 100644 --- a/tipbot/commands/deposit.py +++ b/tipbot/commands/deposit.py @@ -13,8 +13,9 @@ from tipbot.helpers.utils import generate_qr def deposit(update, context): u = db.User.get(telegram_id=update.message.from_user['id']) address = wownero.Wallet().addresses(account=u.account_index)[0] - update.message.reply_photo( + update.message.from_user.send_photo( photo=generate_qr(address), caption=f'{u.telegram_user}\'s deposit address: {address}', quote=False ) + update.message.delete() diff --git a/tipbot/commands/help.py b/tipbot/commands/help.py index 7089c1f..0259325 100644 --- a/tipbot/commands/help.py +++ b/tipbot/commands/help.py @@ -10,4 +10,5 @@ def help(update, context): example=pk['example'], help=pk['help'] )) - update.message.reply_text('Here are the available commands for this bot:\n\n' + '\n\n'.join(cmds)) + update.message.from_user.send_message('Here are the available commands for this bot:\n\n' + '\n\n'.join(cmds)) + update.message.delete() diff --git a/tipbot/commands/register.py b/tipbot/commands/register.py index 44a3866..e9dd253 100644 --- a/tipbot/commands/register.py +++ b/tipbot/commands/register.py @@ -13,28 +13,33 @@ def register(update, context): uid = msg.from_user['id'] un = getattr(msg.from_user, 'username', None) if un is None: - msg.reply_text('You need a username configured in Telegram to use this bot.') + msg.from_user.send_message('You need a username configured in Telegram to use this bot.') + msg.delete() return False if User.filter(telegram_id=uid): if User.filter(telegram_id=uid, telegram_user=un): - msg.reply_text('You are already registered. Use /help to see available bot commands.') + msg.from_user.send_message('You are already registered. Use /help to see available bot commands.') + msg.delete() else: try: u = User.get(telegram_id=uid) u.telegram_user = un u.save() - msg.reply_text(f'You have been registered again as Telegram ID {uid} but with username {un}.') + msg.from_user.send_message(f'You have been registered again as Telegram ID {uid} but with username {un}.') + msg.delete() except Exception as e: logging.error(f'Unable to update user in DB: {e}. Debug: {msg}') - msg.reply_text('Unable to update your existing account. Ask for help.') + msg.from_user.send_message('Unable to update your existing account. Ask for help.') + msg.delete() 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.reply_text('Unable to create a new account for you. Ask for help.') + msg.from_user.send_message('Unable to create a new account for you. Ask for help.') + msg.delete() return False try: u = User( @@ -48,8 +53,10 @@ 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.reply_text(' '.join(reply_text)) + msg.from_user.send_message(' '.join(reply_text)) + msg.delete() except Exception as e: logging.error(f'Unable to register user in DB: {e}. Debug: {msg}') - msg.reply_text('Unable to create a new account for you. Ask for help.') + msg.from_user.send_message('Unable to create a new account for you. Ask for help.') + msg.delete() return False diff --git a/tipbot/commands/tip.py b/tipbot/commands/tip.py index e6381c8..0a3c9ac 100644 --- a/tipbot/commands/tip.py +++ b/tipbot/commands/tip.py @@ -11,7 +11,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.reply_text('Not enough arguments passed.') + update.message.from_user.send_message('Not enough arguments passed.') + update.message.delete() return False elif len(context.args) == 2: message = "" @@ -25,7 +26,8 @@ def tip(update, context): target_un = context.args[0] if target_un == update.message.from_user['first_name']: - update.message.reply_text('You cannot tip yourself!') + update.message.from_user.send_message('You cannot tip yourself!') + update.message.delete() return False if not db.User.filter(telegram_user=target_un): @@ -33,24 +35,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.reply_text(' '.join(reply_text)) + update.message.from_user.send_message(' '.join(reply_text)) + update.message.delete() return False # validate amount try: amount = Decimal(context.args[1]) except: - update.message.reply_text(f'Bad Wownero amount specified; not a valid number.') + update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.') + update.message.delete() return False if amount < 1: - update.message.reply_text('Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 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() 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.reply_text(f'You do not have sufficient funds to send {amount} WOW. Check your /balance') + update.message.from_user.send_message(f'You do not have sufficient funds to send {amount} WOW. Check your /balance') + update.message.delete() return False # get target user details @@ -63,10 +69,13 @@ def tip(update, context): if 'tx_hash' in tx: h = tx['tx_hash'] msg = f'Tipped @{target_un} {amount} WOW! Tx: {h}' - update.message.reply_text(msg) + update.message.from_user.send_message(msg) + update.message.delete() else: logging.error(f'Transaction failure details for {tipper.telegram_user} ({tipper.telegram_id}): {tx}') - update.message.reply_text(f'Failed to send a tip. Reason: "{tx["message"]}"') + update.message.from_user.send_message(f'Failed to send a tip. Reason: "{tx["message"]}"') + update.message.delete() except Exception as e: logging.error(f'Unable to send transfer: {e}. Debug: {update.message}') - update.message.reply_text('Failed to send a tip. Ask for help.') + update.message.from_user.send_message('Failed to send a tip. Ask for help.') + update.message.delete() diff --git a/tipbot/commands/withdraw.py b/tipbot/commands/withdraw.py index 28e6b7f..b2b550d 100644 --- a/tipbot/commands/withdraw.py +++ b/tipbot/commands/withdraw.py @@ -11,31 +11,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.reply_text('Not enough arguments passed.') + update.message.from_user.send_message('Not enough arguments passed.') + update.message.delete() return False # validate address if len(context.args[0]) in [97, 108]: address = context.args[0] else: - update.message.reply_text('This does not look like a valid Wownero address. Try again.') + update.message.from_user.send_message('This does not look like a valid Wownero address. Try again.') + update.message.delete() return False # validate amount try: amount = Decimal(context.args[1]) except: - update.message.reply_text(f'Bad Wownero amount specified; not a valid number.') + update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.') + update.message.delete() return False if amount < 1: - update.message.reply_text('Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 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() return False 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.reply_text(f'You do not have sufficient funds to withdraw {amount} WOW. Check your /balance') + update.message.from_user.send_message(f'You do not have sufficient funds to withdraw {amount} WOW. Check your /balance') + update.message.delete() return False # transfer funds to given address @@ -44,10 +49,13 @@ def withdraw(update, context): if 'tx_hash' in tx: h = tx['tx_hash'] msg = f'Sent {amount} WOW! Tx: {h}' - update.message.reply_text(msg) + update.message.from_user.send_message(msg) + update.message.delete() else: logging.error(f'Transaction failure details for {sender.telegram_user} ({sender.telegram_id}): {tx}') - update.message.reply_text(f'Failed to withdraw Wownero. Reason: "{tx["message"]}"') + update.message.from_user.send_message(f'Failed to withdraw Wownero. Reason: "{tx["message"]}"') + update.message.delete() except Exception as e: logging.error(f'Unable to withdraw transfer: {e}. Debug: {update.message}') - update.message.reply_text('Failed to withdraw Wownero. Ask for help.') + update.message.from_user.send_message('Failed to withdraw Wownero. Ask for help.') + update.message.delete()