dogetipbot-telegram/dogetipbot_telegram.py

326 lines
9.5 KiB
Python
Raw Normal View History

2020-04-27 15:32:05 +02:00
from telegram.ext import Updater, CommandHandler, CallbackContext
from telegram import ParseMode, Update
from pycoin.symbols.doge import network
from pycoin.encoding.hexbytes import b2h_rev, h2b, h2b_rev
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
2017-10-06 11:23:04 +02:00
import logging
2017-10-05 09:37:34 +02:00
import os
2020-04-27 15:32:05 +02:00
import io
import requests
2017-12-21 19:56:14 +01:00
import json
2018-06-27 20:00:36 +02:00
import argparse
2017-10-05 09:17:02 +02:00
2018-06-27 20:00:36 +02:00
# Parsing arguments
2020-04-27 15:32:05 +02:00
parser = argparse.ArgumentParser(description="Dogetipbot telegram")
2020-04-27 16:39:57 +02:00
parser.add_argument("--telegram-api-key", default=os.environ.get("TELEGRAM_API_KEY"))
parser.add_argument("--private-key", default=os.environ.get("DOGE_WALLET_KEY"))
2020-04-27 15:32:05 +02:00
parser.add_argument("--db-path")
2018-06-27 20:00:36 +02:00
args = parser.parse_args()
TELEGRAM_API_KEY = args.telegram_api_key
2020-04-27 15:32:05 +02:00
# Dogecoin Wallet
DERIVATION_PATH = "44/3"
TX_FEE_PER_THOUSAND_BYTES = 100000000
2020-04-27 16:39:57 +02:00
DOGE_WALLET = network.keys.bip32_seed(args.private_key.encode())
2020-04-27 15:32:05 +02:00
# SQL
engine = create_engine(f"sqlite:///{args.db_path}")
sessionM = sessionmaker(bind=engine)
SESSION = sessionM()
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
@hybrid_property
def private_key(self):
return DOGE_WALLET.subkey_for_path(f"{DERIVATION_PATH}/{self.id}")
@hybrid_property
def address(self):
return self.private_key.address()
@hybrid_property
def balance(self):
address_infos = requests.get(
f"https://api.blockcypher.com/v1/doge/main/addrs/{self.address}"
).json()
return (address_infos["final_balance"], address_infos["unconfirmed_balance"])
@hybrid_property
def unspent(self):
unspent_outputs = requests.get(
f"https://api.blockcypher.com/v1/doge/main/addrs/{self.address}?unspentOnly=true&includeScript=true"
).json()
if unspent_outputs["final_balance"] > 0:
tx_refs = []
if "txrefs" in unspent_outputs:
tx_refs.extend(unspent_outputs["txrefs"])
if "unconfirmed_txrefs" in unspent_outputs:
tx_refs.extend(unspent_outputs["unconfirmed_txrefs"])
tx_ins = []
for output in tx_refs:
tx_ins.append(
network.Tx.Spendable(
output["value"],
h2b(output["script"]),
h2b_rev(output["tx_hash"]),
output["tx_output_n"],
)
)
return tx_ins, unspent_outputs["final_balance"]
def __repr__(self):
return f"{self.name}: {self.address}"
Base.metadata.create_all(engine)
2017-10-05 09:17:02 +02:00
2017-10-06 11:23:04 +02:00
# Logging
2020-04-27 15:32:05 +02:00
logging.basicConfig(
level=logging.ERROR,
format="%(asctime)s - %(name)s - %(levelname)s - \
%(message)s",
)
2017-10-06 11:23:04 +02:00
2017-10-05 09:17:02 +02:00
# Exceptions
2017-10-13 21:56:18 +02:00
2017-10-05 09:17:02 +02:00
class NoAccountError(Exception):
pass
2017-10-13 21:56:18 +02:00
2017-10-08 17:08:29 +02:00
class NotEnoughDoge(Exception):
2017-10-05 09:17:02 +02:00
pass
2017-10-13 21:56:18 +02:00
2017-10-05 09:17:02 +02:00
class AccountExisting(Exception):
pass
2017-10-13 21:56:18 +02:00
2017-10-08 17:41:05 +02:00
class NotValidUnit(Exception):
pass
2017-10-13 21:56:18 +02:00
2017-10-05 09:17:02 +02:00
# Core functions
2017-10-13 21:56:18 +02:00
2020-04-27 15:32:05 +02:00
def get_user(account):
query = SESSION.query(User).filter_by(name=account)
if query.count() > 0:
return query.first()
2017-10-05 09:17:02 +02:00
else:
raise NoAccountError(account)
2017-10-13 21:56:18 +02:00
2020-04-27 15:32:05 +02:00
# API broken
# def get_value(amount):
# data = requests.get("https://api.coinmarketcap.com/v1/ticker/dogecoin/?convert=EUR").json()
# return round(float(data[0]['price_eur'])*amount, 2)
def transaction(sender, receiver, amount=None):
pkey = get_user(sender).private_key
unspent, balance = get_user(sender).unspent
s = io.BytesIO()
if amount is None:
tx = network.tx_utils.create_signed_tx(unspent, [receiver], wifs=[pkey.wif()])
tx.stream(s)
tx_byte_count = len(s.getvalue())
tx = network.tx_utils.create_signed_tx(
unspent,
[receiver],
wifs=[pkey.wif()],
fee=TX_FEE_PER_THOUSAND_BYTES * ((999 + tx_byte_count) // 1000),
)
elif balance >= amount:
2020-04-27 16:39:57 +02:00
tx = network.tx_utils.create_signed_tx(
unspent, [(receiver, amount), pkey.address()], wifs=[pkey.wif()]
)
2020-04-27 15:32:05 +02:00
tx.stream(s)
tx_byte_count = len(s.getvalue())
tx = network.tx_utils.create_signed_tx(
unspent,
[(receiver, amount), pkey.address()],
wifs=[pkey.wif()],
fee=TX_FEE_PER_THOUSAND_BYTES * ((999 + tx_byte_count) // 1000),
)
else:
raise NotEnoughDoge
push_obj = {"tx": tx.as_hex()}
# print(push_obj)
pushed_tx = requests.post(
"https://api.blockcypher.com/v1/doge/main/txs/push", json=push_obj
)
pushed_tx.raise_for_status()
return pushed_tx.json()["tx"]["hash"]
# return "null"
2017-10-13 21:56:18 +02:00
2017-10-05 09:37:34 +02:00
2017-10-05 09:17:02 +02:00
# Telegram functions
2017-10-13 21:56:18 +02:00
2017-10-05 09:17:02 +02:00
def start(bot, update):
2020-04-27 15:32:05 +02:00
bot.send_message(
chat_id=update.message.chat_id,
text="Bark ! Je suis un tipbot Dogecoin ! \n\n \
Pour commencer envoyez moi /register",
)
2017-10-13 21:56:18 +02:00
2017-10-05 09:17:02 +02:00
2020-04-27 15:32:05 +02:00
def dogetip(update: Update, context: CallbackContext):
try:
2020-04-27 15:32:05 +02:00
montant = int(context.args[0])
unit = context.args[1]
destinataire = context.args[2][1:]
except (IndexError, ValueError):
2020-04-27 15:32:05 +02:00
context.bot.send_message(
chat_id=update.message.chat_id,
text="Syntaxe : /dogetip xxx doge @destinataire",
)
else:
2017-10-08 17:21:06 +02:00
try:
if unit == "doge":
2020-04-27 15:32:05 +02:00
txid = transaction(
update.message.from_user.username,
get_user(destinataire).address,
montant * 100000000,
)
2017-10-08 17:41:05 +02:00
else:
raise NotValidUnit(unit)
2017-10-08 17:21:06 +02:00
except NotEnoughDoge:
message = "Pas assez de doge @" + update.message.from_user.username
except NoAccountError as e:
2020-04-27 15:32:05 +02:00
message = (
"Vous n'avez pas de compte @"
+ str(e)
+ "\n\n"
+ "Utilisez /register pour démarrer"
)
2017-10-08 17:41:05 +02:00
except NotValidUnit as e:
2017-10-08 17:43:18 +02:00
message = str(e) + " n'est pas une unité valide"
2017-10-08 17:21:06 +02:00
else:
2020-04-27 15:32:05 +02:00
message = (
"🚀 Transaction effectuée 🚀\n\n"
+ f"{str(montant)} DOGE\n"
+ f"@{update.message.from_user.username} → @{destinataire}\n\n"
+ f'<a href="https://blockchair.com/dogecoin/transaction/{txid}">Voir la transaction</a>'
)
context.bot.send_message(
chat_id=update.message.chat_id, parse_mode=ParseMode.HTML, text=message
)
def register(update: Update, context: CallbackContext):
query = SESSION.query(User).filter_by(name=update.message.from_user.username)
if query.count() > 0:
update.message.reply_text("Vous avez déjà un compte")
2017-10-05 09:17:02 +02:00
else:
2020-04-27 15:32:05 +02:00
user = User(name=update.message.from_user.username)
SESSION.add(user)
SESSION.commit()
update.message.reply_text(get_user(update.message.from_user.username).address)
2017-10-05 09:17:02 +02:00
2017-10-13 21:56:18 +02:00
2020-04-27 15:32:05 +02:00
def infos(update: Update, context: CallbackContext):
2017-10-05 09:17:02 +02:00
try:
2020-04-27 15:32:05 +02:00
address = get_user(update.message.from_user.username).address
balance, unconfirmed_balance = get_user(
update.message.from_user.username
).balance
2017-10-05 09:17:02 +02:00
except NoAccountError as e:
2020-04-27 15:32:05 +02:00
update.message.reply_text(
"Vous n'avez pas de compte @"
+ str(e)
+ "\n\n"
+ "Utilisez /register pour démarrer"
)
2017-10-05 09:17:02 +02:00
else:
2020-04-27 15:32:05 +02:00
update.message.reply_text(
address
+ "\n\n"
+ str(balance / 100000000)
+ " DOGE\n"
+ str(unconfirmed_balance / 100000000)
+ " DOGE unconfirmed"
)
2017-10-13 21:56:18 +02:00
2017-10-05 09:17:02 +02:00
2020-04-27 15:32:05 +02:00
def withdraw(update: Update, context: CallbackContext):
try:
unit = context.args[1]
address = context.args[2]
except (IndexError, ValueError):
context.bot.send_message(
chat_id=update.message.chat_id, text="Syntaxe : /withdraw xxx doge adresse"
)
else:
if unit == "doge":
if context.args[0] == "all":
txid = transaction(update.message.from_user.username, address)
else:
montant = int(context.args[0])
txid = transaction(
update.message.from_user.username, address, montant * 100000000
)
2017-10-05 09:37:34 +02:00
2020-04-27 15:32:05 +02:00
context.bot.send_message(
chat_id=update.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
text="Transaction effectuée !\n"
+ f"[tx](https://blockchair.com/dogecoin/transaction/{txid})",
)
2017-10-05 09:37:34 +02:00
2020-04-27 15:32:05 +02:00
def users(update: Update, context: CallbackContext):
query = SESSION.query(User).all()
reply = ""
for user in query:
reply += (
f"\n{repr(user)}\thttps://blockchair.com/dogecoin/address/{user.address}"
)
update.message.reply_text(reply)
2017-10-05 09:37:34 +02:00
2017-10-08 16:21:52 +02:00
2017-10-05 09:17:02 +02:00
# Telegram initialisation
2020-04-27 15:32:05 +02:00
updater = Updater(token=TELEGRAM_API_KEY, use_context=True)
2017-10-05 09:17:02 +02:00
dispatcher = updater.dispatcher
2020-04-27 15:32:05 +02:00
start_handler = CommandHandler("start", start)
2017-10-05 09:17:02 +02:00
dispatcher.add_handler(start_handler)
2020-04-27 15:32:05 +02:00
dogetip_handler = CommandHandler("dogetip", dogetip)
2017-10-05 09:17:02 +02:00
dispatcher.add_handler(dogetip_handler)
2020-04-27 15:32:05 +02:00
register_handler = CommandHandler("register", register)
2017-10-05 09:17:02 +02:00
dispatcher.add_handler(register_handler)
2020-04-27 15:32:05 +02:00
infos_handler = CommandHandler("infos", infos)
dispatcher.add_handler(infos_handler)
infos_handler = CommandHandler("users", users)
2017-10-05 09:17:02 +02:00
dispatcher.add_handler(infos_handler)
2020-04-27 15:32:05 +02:00
withdraw_handler = CommandHandler("withdraw", withdraw)
2017-10-05 09:37:34 +02:00
dispatcher.add_handler(withdraw_handler)
2017-10-08 16:18:30 +02:00
2020-04-27 15:32:05 +02:00
2018-06-27 20:00:36 +02:00
def main():
updater.start_polling()
updater.idle()