Created
May 27, 2017 12:57
-
-
Save kyujin-cho/e88f6421febafee697bba5900c804118 to your computer and use it in GitHub Desktop.
Coinone XRP Noti Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import telebot | |
import threading | |
import requests | |
import json | |
import time | |
import atexit | |
api_link = 'https://api.coinone.co.kr/orderbook/' | |
users = None | |
try: | |
file = open('user_data.json', 'r').read() | |
print(file) | |
users = json.loads(file) | |
except FileNotFoundError as e: | |
print(e) | |
users = {} | |
bot = telebot.TeleBot('TOKEN') | |
current_lowest_value = -1 | |
def on_exit(): | |
with open('user_data.json', 'w') as f: | |
f.write(json.dumps(users)) | |
atexit.register(on_exit) | |
class CheckCurrency(threading.Thread): | |
def run(self): | |
global current_lowest_value | |
while True: | |
data = requests.get(api_link, params={'currency': 'xrp'}).json() | |
t = 9999999999999 | |
print(data['currency']) | |
for ask in data['ask']: | |
# print(ask) | |
if t > float(ask['price']): | |
t = float(ask['price']) | |
print(t) | |
for user in users.items(): | |
if user[1] >= t: | |
bot.send_message(user[0], '현재 최저가: ' + str(t) + ' KRW, https://coinone.co.kr/exchange/trade/xrp/') | |
current_lowest_value = t | |
time.sleep(10) | |
@bot.message_handler(commands=['register']) | |
def register_id(message): | |
print(message.text) | |
print(message.chat.id) | |
if not message.text.replace('/register ', '').strip().isdigit(): | |
return | |
users[str(message.chat.id)] = int(message.text.replace('/register ', '').strip()) | |
print(users) | |
bot.reply_to(message, 'Success') | |
@bot.message_handler(commands=['un_register']) | |
def de_register(message): | |
if str(message.chat.id) not in users.keys(): | |
return | |
del users[str(message.chat.id)] | |
bot.reply_to(message, 'Unregistered') | |
@bot.message_handler(commands=['my_expectation']) | |
def get_expectation(message): | |
if message.chat.id in users.keys(): | |
bot.reply_to(message, str(users[message.chat.id]) + ' KRW') | |
else: | |
bot.reply_to(message, 'no registered expectation value') | |
@bot.message_handler(commands=['current_currency']) | |
def get_current_currency(message): | |
if current_lowest_value is -1: | |
bot.reply_to(message, 'Data not yet fetched!') | |
else: | |
bot.reply_to(message, str(current_lowest_value) + ' KRW') | |
receive = CheckCurrency(name='Check XRP currency') | |
receive.start() | |
bot.polling() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment