manniru
12/26/2016 - 4:55 PM

bitcoin bot

bitcoin bot

from telegram.ext import CommandHandler, Updater, Job

from exchanges.bitfinex import Bitfinex
from exchanges.coindesk import CoinDesk

bitfinex = Bitfinex()
coindesk = CoinDesk()

up = Updater("YOUR CUSTOM BOT-FATHER-ROVIDED TOKEN")

listjobs = []


def stop(bot, update):
    bot.sendMessage(chat_id=update.message.chat_id, text="Stoping right now")
    for j in listjobs:
        if j.context["id"] == update.message.chat_id:
            j.schedule_removal()


def kill(bot, update):
    import sys
    up.job_queue.stop()
    up.stop()
    sys.exit(0)


def watch(bot, job):
    value = job.context['value']
    coin = coindesk.get_current_price()
    bit = bitfinex.get_current_price()
    if value > coin or value > bit:
        job.context["count"] = 0
        msg = "Current price coindesk: {} and bitfinex:{}"
        bot.sendMessage(chat_id=job.context['id'],
                        text=msg.format(coin, bit))
    else:
        if job.context.get('count') is None:
            job.context["count"] = 0
        if job.context["count"] == 360:
            job.context["count"] = 0
            bot.sendMessage(chat_id=job.context[
                            'id'], text="all ok {}".format(coin))
        else:
            job.context["count"] += 1


def updates(bot, update):
    value = update.message.text.split(" ")
    if len(value) >= 2:
        value = float(value[1])
        print value
        bot.sendMessage(chat_id=update.message.chat_id,
                        text="Starting right now: ")
        context = {
            'id': update.message.chat_id,
            'value': value
        }
        job_watch = Job(watch, 10.0, context=context)
        listjobs.append(job_watch)
        up.job_queue.put(job_watch, next_t=0.0)


up.dispatcher.add_handler(CommandHandler('stop', stop))
up.dispatcher.add_handler(CommandHandler('kill', kill))
up.dispatcher.add_handler(CommandHandler('updates', updates))
up.start_polling()
print "all rigth"