From c7310c1c606b74ed4542568c44d02db0890da059 Mon Sep 17 00:00:00 2001 From: nyanloutre Date: Thu, 5 Oct 2017 09:17:02 +0200 Subject: [PATCH] Premier commit --- Dockerfile | 8 ++++ Makefile | 7 ++++ api_keys.env | 3 ++ run.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 api_keys.env create mode 100644 run.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..86f645c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:alpine + +RUN apk add --no-cache gcc g++ make libffi-dev openssl-dev && \ + pip install python-telegram-bot requests block-io + +COPY run.py / + +CMD ["python", "run.py"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..70d8102 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: build +build: + docker build -t dogetipbot . + +.PHONY: run +run: build + docker run --rm --name dogetipbot --env-file api_keys.env dogetipbot diff --git a/api_keys.env b/api_keys.env new file mode 100644 index 0000000..d5f6e3a --- /dev/null +++ b/api_keys.env @@ -0,0 +1,3 @@ +BLOCK_IO_API_KEY=123455678 +BLOCK_IO_PIN=12345678 +TELEGRAM_API_KEY=12345678 diff --git a/run.py b/run.py new file mode 100644 index 0000000..d4abda0 --- /dev/null +++ b/run.py @@ -0,0 +1,115 @@ +from telegram.ext import Updater, CommandHandler, MessageHandler, Filters +from telegram import ParseMode +from block_io import BlockIo, BlockIoAPIError +import io + +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'] + +# 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: + return float(response['data']['available_balance']) + +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: + if get_balance(sender) > amount: + address_receiver = get_address(receiver) + return block_io.withdraw_from_labels(amounts=amount, from_labels=sender, to_labels=receiver) + 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" + +# 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) + + txid = response['data']['txid'] + + bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text="Transaction effectuée !\n [tx](https://chain.so/tx/DOGETEST/" + txid + ")") + +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) + balance = get_balance(update.message.from_user.username) + except NoAccountError as e: + bot.send_message(chat_id=update.message.chat_id, text="Merci de vous créer un compte @" + str(e)) + else: + bot.send_message(chat_id=update.message.chat_id, text=address + "\n" + str(balance) + " DOGE") + +# 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) + +updater.start_polling()