dogetipbot-telegram/run.py

164 lines
5.2 KiB
Python
Raw Normal View History

2017-10-05 09:17:02 +02:00
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import ParseMode
from block_io import BlockIo, BlockIoAPIError
2017-10-06 11:23:04 +02:00
import logging
2017-10-05 09:37:34 +02:00
import os
2017-10-05 09:17:02 +02:00
BLOCK_IO_API_KEY = os.environ['BLOCK_IO_API_KEY']
BLOCK_IO_PIN = os.environ['BLOCK_IO_PIN']
TELEGRAM_API_KEY = os.environ['TELEGRAM_API_KEY']
2017-10-05 12:20:35 +02:00
NETWORK = os.environ['NETWORK']
2017-10-05 09:17:02 +02:00
2017-10-06 11:23:04 +02:00
# Logging
2017-10-06 11:26:19 +02:00
logging.basicConfig(level=logging.ERROR,
2017-10-06 11:23:04 +02:00
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
2017-10-05 09:17:02 +02:00
# Exceptions
class NoAccountError(Exception):
pass
class BalanceError(Exception):
pass
class AccountExisting(Exception):
pass
# BlockIO
version = 2
block_io = BlockIo(BLOCK_IO_API_KEY, BLOCK_IO_PIN, version)
# Core functions
def get_balance(account):
try:
response = block_io.get_address_by(label=account)
except BlockIoAPIError:
raise NoAccountError(account)
else:
2017-10-05 12:20:35 +02:00
return (float(response['data']['available_balance']), float(response['data']['pending_received_balance']))
2017-10-05 09:17:02 +02:00
def create_address(account):
try:
response = block_io.get_new_address(label=account)
except BlockIoAPIError:
raise AccountExisting
else:
return response['data']['address']
def get_address(account):
try:
response = block_io.get_address_by(label=account)
except BlockIoAPIError:
raise NoAccountError(account)
else:
return response['data']['address']
def transaction(sender, receiver, amount):
try:
2017-10-05 12:20:35 +02:00
if get_balance(sender)[0] > amount:
2017-10-05 09:17:02 +02:00
address_receiver = get_address(receiver)
2017-10-05 23:22:04 +02:00
return block_io.withdraw_from_labels(amounts=amount, from_labels=sender, to_labels=receiver, priority="low")
2017-10-05 09:17:02 +02:00
else:
return "Pas assez de doge"
except NoAccountError as e:
return "Merci de vous créer un compte @" + str(e)
except BlockIoAPIError:
return "Erreur d'API"
2017-10-05 09:37:34 +02:00
def address_transaction(account, address, amount):
try:
2017-10-05 12:20:35 +02:00
if get_balance(account)[0] > amount:
2017-10-05 23:22:04 +02:00
return block_io.withdraw_from_labels(amounts=amount, from_labels=account, to_addresses=address, priority="low")
2017-10-05 09:37:34 +02:00
else:
return "Pas assez de doge"
except NoAccountError as e:
return "Merci de vous créer un compte @" + str(e)
except BlockIoAPIError:
return "Erreur d'API"
2017-10-05 09:17:02 +02:00
# Telegram functions
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
def dogetip(bot, update, args):
montant = int(args[0])
unit = args[1]
destinataire = args[2][1:]
if unit == "doge":
response = transaction(update.message.from_user.username, destinataire, montant)
2017-10-08 16:47:59 +02:00
try:
txid = response['data']['txid']
except TypeError:
message = response
else:
message = '🚀 Transaction effectuée 🚀\n\n' \
+ str(montant) + ' ' + NETWORK + '\n' \
+ '@' + update.message.from_user.username + ' → @' + destinataire + '\n\n' \
+ '<a href="https://chain.so/tx/' + NETWORK + '/' + txid + '">Voir la transaction</a>'
2017-10-05 14:59:12 +02:00
2017-10-08 16:31:36 +02:00
bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=message)
2017-10-05 09:17:02 +02:00
def register(bot, update):
try:
address = create_address(update.message.from_user.username)
except AccountExisting:
bot.send_message(chat_id=update.message.chat_id, text="Vous avez déjà un compte")
else:
bot.send_message(chat_id=update.message.chat_id, text=address)
def infos(bot, update):
try:
address = get_address(update.message.from_user.username)
2017-10-05 12:20:35 +02:00
balance, unconfirmed_balance = get_balance(update.message.from_user.username)
2017-10-05 09:17:02 +02:00
except NoAccountError as e:
bot.send_message(chat_id=update.message.chat_id, text="Merci de vous créer un compte @" + str(e))
else:
2017-10-05 12:20:35 +02:00
bot.send_message(chat_id=update.message.chat_id, text=address + "\n\n" + str(balance) + " " + NETWORK + "\n" + str(unconfirmed_balance) + " " + NETWORK + " unconfirmed")
2017-10-05 09:17:02 +02:00
2017-10-05 09:37:34 +02:00
def withdraw(bot, update, args):
montant = int(args[0])
unit = args[1]
address = args[2]
if unit == "doge":
response = address_transaction(update.message.from_user.username, address, montant)
txid = response['data']['txid']
2017-10-05 12:20:35 +02:00
bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text="Transaction effectuée !\n [tx](https://chain.so/tx/" + NETWORK + "/" + txid + ")")
2017-10-05 09:37:34 +02:00
2017-10-08 16:21:52 +02:00
def testing(bot, update):
2017-10-08 16:31:36 +02:00
bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text="Bonjour @V_IAL")
2017-10-08 16:21:52 +02:00
2017-10-05 09:17:02 +02:00
# Telegram initialisation
updater = Updater(token=TELEGRAM_API_KEY)
dispatcher = updater.dispatcher
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
dogetip_handler = CommandHandler('dogetip', dogetip, pass_args=True)
dispatcher.add_handler(dogetip_handler)
register_handler = CommandHandler('register', register)
dispatcher.add_handler(register_handler)
infos_handler = CommandHandler('infos', infos)
dispatcher.add_handler(infos_handler)
2017-10-05 09:37:34 +02:00
withdraw_handler = CommandHandler('withdraw', withdraw, pass_args=True)
dispatcher.add_handler(withdraw_handler)
2017-10-08 16:18:30 +02:00
2017-10-08 16:21:52 +02:00
testing_handler = CommandHandler('testing', testing)
dispatcher.add_handler(testing_handler)
2017-10-05 09:17:02 +02:00
updater.start_polling()
2017-10-08 16:18:30 +02:00
updater.idle()