tg-bot/tipbot/commands/register.py

55 lines
2.2 KiB
Python
Raw Normal View History

2020-08-09 06:39:19 +00:00
msgimport logging
2020-08-06 05:52:13 +00:00
from tipbot import wownero
2020-08-09 06:39:19 +00:00
from tipbot.db import User
from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug
2020-08-06 05:52:13 +00:00
@wallet_rpc_required
@log_event
@check_debug
2020-08-06 05:52:13 +00:00
def register(update, context):
2020-08-09 06:39:19 +00:00
wallet = wownero.Wallet()
msg = update.message
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.')
return False
2020-08-06 05:52:13 +00:00
2020-08-09 06:39:19 +00:00
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.')
2020-08-06 05:52:13 +00:00
else:
try:
2020-08-09 06:39:19 +00:00
u = User.get(telegram_id=uid)
2020-08-06 05:52:13 +00:00
u.telegram_user = un
u.save()
2020-08-09 06:39:19 +00:00
msg.reply_text(f'You have been registered again as Telegram ID {uid} but with username {un}.')
2020-08-06 05:52:13 +00:00
except Exception as e:
2020-08-09 06:39:19 +00:00
logging.error(f'Unable to update user in DB: {e}. Debug: {msg}')
msg.reply_text('Unable to update your existing account. Ask for help.')
2020-08-06 05:52:13 +00:00
return False
else:
try:
account_index = wallet.new_account(label=un)
except Exception as e:
2020-08-09 06:39:19 +00:00
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.')
2020-08-06 05:52:13 +00:00
return False
try:
2020-08-09 06:39:19 +00:00
u = User(
2020-08-06 05:52:13 +00:00
telegram_id=uid,
telegram_user=un,
account_index=account_index,
)
u.save()
reply_text = [
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.'
]
2020-08-09 06:39:19 +00:00
msg.reply_text(' '.join(reply_text))
2020-08-06 05:52:13 +00:00
except Exception as e:
2020-08-09 06:39:19 +00:00
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.')
2020-08-06 05:52:13 +00:00
return False