Skip to content

Instantly share code, notes, and snippets.

@jwinterm
Created January 13, 2018 17:40
Show Gist options
  • Save jwinterm/b634b02b2d6278e4ed74c91af63c3a05 to your computer and use it in GitHub Desktop.
Save jwinterm/b634b02b2d6278e4ed74c91af63c3a05 to your computer and use it in GitHub Desktop.
Telegram tipbot based on reddcoin bot
import json
import codecs
import requests
from bs4 import BeautifulSoup, SoupStrainer
import re
import subprocess
from telegram.ext.dispatcher import run_async
from telegram.ext import Updater
from html import escape
updater = Updater(token='PUT TOKEN HERE')
dispatcher = updater.dispatcher
price_url = "https://api.coinmarketcap.com/v1/ticker/myriad/"
core_loc = "/path/to/your/myriadcoin-cli"
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
def commands(bot, update):
user = update.message.from_user.username
bot.send_message(chat_id=update.message.chat_id, text="Initiating commands /tip & /withdraw have a specfic format,\n use them like so:" + "\n \n Parameters: \n <user> = target user to tip \n <amount> = amount of reddcoin to utilise \n <address> = reddcoin address to withdraw to \n \n Tipping format: \n /tip <user> <amount> \n \n Withdrawing format: \n /withdraw <address> <amount>")
def help(bot, update):
# bot.send_message(chat_id=update.message.chat_id, text="The following commands are at your disposal: /hi , /commands , /deposit , /tip , /withdraw , /price , /marketcap or /balance")
bot.send_message(chat_id=update.message.chat_id, text="The following commands are at your disposal: /hi , /commands , /deposit , /tip , /withdraw , or /balance")
def deposit(bot, update):
user = update.message.from_user.username
if user is None:
bot.send_message(chat_id=update.message.chat_id, text="Please set a telegram username in your profile settings!")
else:
result = subprocess.run([core_loc,"getaccountaddress",user],stdout=subprocess.PIPE)
clean = (result.stdout.strip()).decode("utf-8")
bot.send_message(chat_id=update.message.chat_id, text="@{0} your depositing address is: {1}".format(user,clean))
def tip(bot,update):
user = update.message.from_user.username
target = update.message.text[5:]
amount = target.split(" ")[1]
target = target.split(" ")[0]
if user is None:
bot.send_message(chat_id=update.message.chat_id, text="Please set a telegram username in your profile settings!")
else:
machine = "@myriad_bot"
if target == machine:
bot.send_message(chat_id=update.message.chat_id, text="HODL.")
elif "@" in target:
target = target[1:]
user = update.message.from_user.username
result = subprocess.run([core_loc,"getbalance",user],stdout=subprocess.PIPE)
balance = float((result.stdout.strip()).decode("utf-8"))
amount = float(amount)
if balance < amount:
bot.send_message(chat_id=update.message.chat_id, text="@{0} you have insufficent funds.".format(user))
elif target == user:
bot.send_message(chat_id=update.message.chat_id, text="You can't tip yourself silly.")
else:
balance = str(balance)
amount = str(amount)
tx = subprocess.run([core_loc,"move",user,target,amount],stdout=subprocess.PIPE)
bot.send_message(chat_id=update.message.chat_id, text="@{0} tipped @{1} of {2} XMY".format(user, target, amount))
else:
bot.send_message(chat_id=update.message.chat_id, text="Error that user is not applicable.")
def balance(bot,update):
user = update.message.from_user.username
if user is None:
bot.send_message(chat_id=update.message.chat_id, text="Please set a telegram username in your profile settings!")
else:
result = subprocess.run([core_loc,"getbalance",user],stdout=subprocess.PIPE)
clean = (result.stdout.strip()).decode("utf-8")
balance = float(clean)
balance = str(round(balance,3))
bot.send_message(chat_id=update.message.chat_id, text="@{0} your current balance is: {1} XMY ".format(user,balance))
def price(bot,update):
try:
quote_page = requests.get(price_url)
j = quote_page.json()
price_usd = float(j[0]['price_usd'])
price_btc = float(j[0]['price_btc'])
bot.send_message(chat_id=update.message.chat_id, text="Myriad is valued at {0:.3f} = {1:8f}".format(price_usd, price_btc + " ฿"))
except:
bot.send_message("Error retrieving price data")
def withdraw(bot,update):
user = update.message.from_user.username
if user is None:
bot.send_message(chat_id=update.message.chat_id, text="Please set a telegram username in your profile settings!")
else:
target = update.message.text[9:]
address = target[:35]
address = ''.join(str(e) for e in address)
target = target.replace(target[:35], '')
amount = float(target)
result = subprocess.run([core_loc,"getbalance",user],stdout=subprocess.PIPE)
clean = (result.stdout.strip()).decode("utf-8")
balance = float(clean)
if balance < amount:
bot.send_message(chat_id=update.message.chat_id, text="@{0} you have insufficent funds.".format(user))
else:
amount = str(amount)
tx = subprocess.run([core,"sendfrom",user,address,amount],stdout=subprocess.PIPE)
bot.send_message(chat_id=update.message.chat_id, text="@{0} has successfully withdrew to address: {1} of {2} XMY" .format(user,address,amount))
def hi(bot,update):
user = update.message.from_user.username
bot.send_message(chat_id=update.message.chat_id, text="Hello @{0}, how are you doing today?".format(user))
def moon(bot,update):
bot.send_message(chat_id=update.message.chat_id, text="Moon mission inbound!")
def marketcap(bot,update):
try:
quote_page = requests.get(price_url)
j = quote_page.json()
bot.send_message(chat_id=update.message.chat_id, text="The market cap of Myriad is ${0:.2f}M".format(float(j[0]['market_cap_usd'])/1e6))
except:
bot.send_message("Error retrieving price data")
from telegram.ext import CommandHandler
commands_handler = CommandHandler('commands', commands)
dispatcher.add_handler(commands_handler)
moon_handler = CommandHandler('moon', moon)
dispatcher.add_handler(moon_handler)
hi_handler = CommandHandler('hi', hi)
dispatcher.add_handler(hi_handler)
withdraw_handler = CommandHandler('withdraw', withdraw)
dispatcher.add_handler(withdraw_handler)
# marketcap_handler = CommandHandler('marketcap', marketcap)
# dispatcher.add_handler(marketcap_handler)
deposit_handler = CommandHandler('deposit', deposit)
dispatcher.add_handler(deposit_handler)
# price_handler = CommandHandler('price', price)
# dispatcher.add_handler(price_handler)
tip_handler = CommandHandler('tip', tip)
dispatcher.add_handler(tip_handler)
balance_handler = CommandHandler('balance', balance)
dispatcher.add_handler(balance_handler)
help_handler = CommandHandler('help', help)
dispatcher.add_handler(help_handler)
updater.start_polling(poll_interval=2.0, timeout=10.0, network_delay=None, clean=False, bootstrap_retries=0, read_latency=2.0, allowed_updates=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment