mirror of
https://git.wownero.com/lza_menace/tg-bot.git
synced 2024-08-15 00:23:12 +00:00
catch exceptions where user hasnt started
This commit is contained in:
parent
b8b21092b9
commit
2d0dfc31fe
9 changed files with 90 additions and 66 deletions
|
@ -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)
|
||||
|
|
|
@ -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.')
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.')
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue