catch exceptions where user hasnt started

This commit is contained in:
lza_menace 2021-05-10 13:09:55 -07:00
parent b8b21092b9
commit 2d0dfc31fe
9 changed files with 90 additions and 66 deletions

View file

@ -2,6 +2,7 @@ import logging
from tipbot import wownero from tipbot import wownero
from tipbot import db from tipbot import db
from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug
from tipbot.helpers.utils import reply_user
@wallet_rpc_required @wallet_rpc_required
@ -14,5 +15,4 @@ def balance(update, context):
unlocked = float(balances[1]) unlocked = float(balances[1])
locked = float(balances[0] - balances[1]) locked = float(balances[0] - balances[1])
msg = f'Available balance for {u.telegram_user}: {unlocked} WOW ({locked} WOW locked)' msg = f'Available balance for {u.telegram_user}: {unlocked} WOW ({locked} WOW locked)'
update.message.from_user.send_message(msg) reply_user(update.message, context, msg)
update.message.delete()

View file

@ -7,7 +7,7 @@ from tipbot.wownero import Wallet
@log_event @log_event
@check_debug @check_debug
def debug(update, context): 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') update.message.reply_text('ohai')
else: else:
update.message.reply_text('you cant do that.') update.message.reply_text('you cant do that.')

View file

@ -1,5 +1,6 @@
import logging import logging
from telegram import ParseMode from telegram import ParseMode
from telegram.error import Unauthorized
from tipbot import wownero from tipbot import wownero
from tipbot import db from tipbot import db
from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug 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 @log_event
@check_debug @check_debug
def deposit(update, context): 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] address = wownero.Wallet().addresses(account=u.account_index)[0]
try:
update.message.from_user.send_photo( update.message.from_user.send_photo(
photo=generate_qr(address), photo=generate_qr(address),
caption=f'{u.telegram_user}\'s deposit address: {address}', caption=f'{u.telegram_user}\'s deposit address: {address}',
quote=False quote=False
) )
update.message.delete() 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()

View file

@ -1,4 +1,5 @@
from tipbot import commands from tipbot import commands
from tipbot.helpers.utils import reply_user
def help(update, context): def help(update, context):
@ -10,5 +11,5 @@ def help(update, context):
example=pk['example'], example=pk['example'],
help=pk['help'] help=pk['help']
)) ))
update.message.from_user.send_message('Here are the available commands for this bot:\n\n' + '\n\n'.join(cmds)) msg = 'Here are the available commands for this bot:\n\n' + '\n\n'.join(cmds)
update.message.delete() reply_user(update.message, context, msg)

View file

@ -10,9 +10,6 @@ from tipbot import config
@log_event @log_event
@check_debug @check_debug
def mine(update, context): def mine(update, context):
# First delete message to remove clutter
update.message.delete()
# Check that enough arguments were provided # Check that enough arguments were provided
if len(context.args) < 1: if len(context.args) < 1:
update.message.reply_text('Not enough arguments passed. Include hash rate per second to estimate.') update.message.reply_text('Not enough arguments passed. Include hash rate per second to estimate.')

View file

@ -2,6 +2,7 @@ import logging
from tipbot import wownero from tipbot import wownero
from tipbot.db import User from tipbot.db import User
from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug
from tipbot.helpers.utils import reply_user
@wallet_rpc_required @wallet_rpc_required
@ -13,33 +14,34 @@ def register(update, context):
uid = msg.from_user['id'] uid = msg.from_user['id']
un = getattr(msg.from_user, 'username', None) un = getattr(msg.from_user, 'username', None)
if un is None: if un is None:
msg.from_user.send_message('You need a username configured in Telegram to use this bot.') _m = 'You need a username configured in Telegram to use this bot.'
msg.delete() reply_user(msg, context, _m)
return False return False
if User.filter(telegram_id=uid): if User.filter(telegram_id=uid):
if User.filter(telegram_id=uid, telegram_user=un): 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.') _m = 'You are already registered. Use /help to see available bot commands.'
msg.delete() reply_user(msg, context, _m)
else: else:
try: try:
u = User.get(telegram_id=uid) u = User.get(telegram_id=uid)
u.telegram_user = un u.telegram_user = un
u.save() u.save()
msg.from_user.send_message(f'You have been registered again as Telegram ID {uid} but with username {un}.') _m = f'You have been registered again as Telegram ID {uid} but with username {un}.'
msg.delete() reply_user(msg, context, _m)
return True
except Exception as e: except Exception as e:
logging.error(f'Unable to update user in DB: {e}. Debug: {msg}') 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.') _m = 'Unable to update your existing account. Ask for help.'
msg.delete() reply_user(msg, context, _m)
return False return False
else: else:
try: try:
account = wallet.new_account(label=un) account = wallet.new_account(label=un)
except Exception as e: except Exception as e:
logging.error(f'Unable to create a new account in wallet RPC: {e}. Debug: {msg}') 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.') _m = 'Unable to create a new account for you. Ask for help.'
msg.delete() reply_user(msg, context, _m)
return False return False
try: try:
u = User( 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.', 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.' '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)) _m = ' '.join(reply_text)
msg.delete() reply_user(msg, context, _m)
return True
except Exception as e: except Exception as e:
logging.error(f'Unable to register user in DB: {e}. Debug: {msg}') 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.') _m = 'Unable to create a new account for you. Ask for help.'
msg.delete() reply_user(msg, context, _m)
return False return False

View file

@ -3,6 +3,7 @@ from decimal import Decimal
from tipbot import wownero from tipbot import wownero
from tipbot import db from tipbot import db
from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug
from tipbot.helpers.utils import reply_user
@wallet_rpc_required @wallet_rpc_required
@ -11,8 +12,8 @@ from tipbot.helpers.decorators import wallet_rpc_required, log_event, registrati
@check_debug @check_debug
def tip(update, context): def tip(update, context):
if len(context.args) < 2: if len(context.args) < 2:
update.message.from_user.send_message('Not enough arguments passed.') _m = 'Not enough arguments passed.'
update.message.delete() reply_user(update.message, context, _m)
return False return False
elif len(context.args) == 2: elif len(context.args) == 2:
message = "" message = ""
@ -26,8 +27,8 @@ def tip(update, context):
target_un = context.args[0] target_un = context.args[0]
if target_un == update.message.from_user['first_name']: if target_un == update.message.from_user['first_name']:
update.message.from_user.send_message('You cannot tip yourself!') _m = 'You cannot tip yourself!'
update.message.delete() reply_user(update.message, context, _m)
return False return False
if not db.User.filter(telegram_user=target_un): 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.', 'That user has not registered and cannot receive tips yet.',
'If they would like to receive a tip, have them /register with the bot.' 'If they would like to receive a tip, have them /register with the bot.'
] ]
update.message.from_user.send_message(' '.join(reply_text)) _m = ' '.join(reply_text)
update.message.delete() reply_user(update.message, context, _m)
return False return False
# validate amount # validate amount
try: try:
amount = Decimal(context.args[1]) amount = Decimal(context.args[1])
except: except:
update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.') _m = 'Bad Wownero amount specified; not a valid number.'
update.message.delete() reply_user(update.message, context, _m)
return False return False
if amount < 1: 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.') _m = 'Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.'
update.message.delete() reply_user(update.message, context, _m)
return False return False
tipper = db.User.get(telegram_id=update.message.from_user['id']) tipper = db.User.get(telegram_id=update.message.from_user['id'])
tipper_balances = wownero.Wallet().balances(account=tipper.account_index) tipper_balances = wownero.Wallet().balances(account=tipper.account_index)
if amount >= tipper_balances[1]: 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') _m = f'You do not have sufficient funds to send {amount} WOW. Check your /balance'
update.message.delete() reply_user(update.message, context, _m)
return False return False
# get target user details # 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) tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=tipper.account_index)
if 'tx_hash' in tx: if 'tx_hash' in tx:
h = tx['tx_hash'] h = tx['tx_hash']
msg = f'Tipped @{target_un} {amount} WOW! Tx: {h}' _m = f'Tipped @{target_un} {amount} WOW! Tx: {h}'
update.message.from_user.send_message(msg) reply_user(update.message, context, _m)
update.message.delete()
else: else:
logging.error(f'Transaction failure details for {tipper.telegram_user} ({tipper.telegram_id}): {tx}') 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"]}"') _m = f'Failed to send a tip. Reason: {tx["message"]}'
update.message.delete() reply_user(update.message, context, _m)
except Exception as e: except Exception as e:
logging.error(f'Unable to send transfer: {e}. Debug: {update.message}') 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.') _m = 'Failed to send a tip. Ask for help.'
update.message.delete() reply_user(update.message, context, _m)

View file

@ -3,6 +3,7 @@ from decimal import Decimal
from tipbot import wownero from tipbot import wownero
from tipbot import db from tipbot import db
from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug
from tipbot.helpers.utils import reply_user
@wallet_rpc_required @wallet_rpc_required
@ -11,36 +12,36 @@ from tipbot.helpers.decorators import wallet_rpc_required, log_event, registrati
@check_debug @check_debug
def withdraw(update, context): def withdraw(update, context):
if len(context.args) < 2: if len(context.args) < 2:
update.message.from_user.send_message('Not enough arguments passed.') _m = 'Not enough arguments passed.'
update.message.delete() reply_user(update.message, context, _m)
return False return False
# validate address # validate address
if len(context.args[0]) in [97, 108]: if len(context.args[0]) in [97, 108]:
address = context.args[0] address = context.args[0]
else: else:
update.message.from_user.send_message('This does not look like a valid Wownero address. Try again.') _m = 'This does not look like a valid Wownero address. Try again.'
update.message.delete() reply_user(update.message, context, _m)
return False return False
# validate amount # validate amount
try: try:
amount = Decimal(context.args[1]) amount = Decimal(context.args[1])
except: except:
update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.') _m = 'Bad Wownero amount specified; not a valid number.'
update.message.delete() reply_user(update.message, context, _m)
return False return False
if amount < 1: 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.') _m = 'Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.'
update.message.delete() reply_user(update.message, context, _m)
return False 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) sender_balances = wownero.Wallet().balances(account=sender.account_index)
if amount > sender_balances[1]: 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') _m = f'You do not have sufficient funds to withdraw {amount} WOW. Check your /balance'
update.message.delete() reply_user(update.message, context, _m)
return False return False
# transfer funds to given address # 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) tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=sender.account_index)
if 'tx_hash' in tx: if 'tx_hash' in tx:
h = tx['tx_hash'] h = tx['tx_hash']
msg = f'Sent {amount} WOW! Tx: {h}' _m = f'Sent {amount} WOW! Tx: {h}'
update.message.from_user.send_message(msg) reply_user(update.message, context, _m)
update.message.delete()
else: else:
logging.error(f'Transaction failure details for {sender.telegram_user} ({sender.telegram_id}): {tx}') 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"]}"') _m = f'Failed to withdraw Wownero. Reason: "{tx["message"]}"'
update.message.delete() reply_user(update.message, context, _m)
except Exception as e: except Exception as e:
logging.error(f'Unable to withdraw transfer: {e}. Debug: {update.message}') logging.error(f'Unable to withdraw transfer: {e}. Debug: {update.message}')
update.message.from_user.send_message('Failed to withdraw Wownero. Ask for help.') _m = 'Failed to withdraw Wownero. Ask for help.'
update.message.delete() reply_user(update.message, context, _m)

View file

@ -3,6 +3,7 @@ from io import BytesIO
from PIL import Image from PIL import Image
from base64 import b64encode from base64 import b64encode
from qrcode import make as qrcode_make from qrcode import make as qrcode_make
from telegram.error import Unauthorized
from tipbot import config from tipbot import config
@ -17,3 +18,17 @@ def generate_qr(s):
qrcode_make(s).save(_address_qr, format="PNG") qrcode_make(s).save(_address_qr, format="PNG")
_address_qr.seek(0) _address_qr.seek(0)
return _address_qr 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()