-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
91 lines (80 loc) · 3.4 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import telebot
import os
import json
from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager
#ATENÇÃO: NÃO DECLARAR ESTAS VARIAVEIS PUBLICAMENTE! USAR SECRETS
#API KEY da Binance (Gerado dentro do site da mesma)
API_KEY = ''
#API SECRET da Binance (Gerado dentro do site da mesma)
API_SECRET = ''
#KEY do BOT (obitida com o botfather no Telegram)
BOT_KEY = ''
#KEY do grupo onde a mensagem será enviada (obitida em https://api.telegram.org/bot<BOT_KEY>/getUpdates)
GROUP_KEY = ''
#Coletando Variaveis do Ambiente (Criadas em Settings>Secrets)
if os.environ.get('API_KEY', '') != '':
API_KEY = os.environ['API_KEY']
if os.environ.get('API_SECRET', '') != '':
API_SECRET = os.environ['API_SECRET']
if os.environ.get('BOT_KEY', '') != '':
BOT_KEY = os.environ['BOT_KEY']
if os.environ.get('GROUP_KEY', '') != '':
GROUP_KEY = os.environ['GROUP_KEY']
api_key = API_KEY
api_secret = API_SECRET
bot = telebot.TeleBot(BOT_KEY)
client = Client(api_key, api_secret)
txt_price = "Cotação Crypto Binance:\n\n+ variação no dia\n\n"
#Pegando todas as cotações e selecionando apenas as necessárias e enviando ao grupo
prices = client.get_all_tickers()
for i in prices:
moeda = i['symbol'][-3:]
if(moeda=="BRL"):
cripto = i['symbol'][:3]
criptoName = ""
if(cripto=="BTC"):
criptoName = "Bitcoin"
criptoInfo = client.get_ticker(symbol=i['symbol'])
json_str = json.dumps(criptoInfo)
criptoInfo = json.loads(json_str)
txt_price += ('%.2f' % round(float(criptoInfo['priceChangePercent']),2)) + "% "
txt_price += cripto + " (" +criptoName+ ")" +" = R$ "+ ('%.2f' % round(float(i['price']),2)) + "\n"
if(cripto=="ETH"):
criptoName = "Ethereum"
criptoInfo = client.get_ticker(symbol=i['symbol'])
json_str = json.dumps(criptoInfo)
criptoInfo = json.loads(json_str)
txt_price += ('%.2f' % round(float(criptoInfo['priceChangePercent']),2)) + "% "
txt_price += cripto + " (" +criptoName+ ")" +" = R$ "+ ('%.2f' % round(float(i['price']),2)) + "\n"
if(cripto=="ADA"):
criptoName = "Cardano"
criptoInfo = client.get_ticker(symbol=i['symbol'])
json_str = json.dumps(criptoInfo)
criptoInfo = json.loads(json_str)
txt_price += ('%.2f' % round(float(criptoInfo['priceChangePercent']),2)) + "% "
txt_price += cripto + " (" +criptoName+ ")" +" = R$ "+ ('%.2f' % round(float(i['price']),2)) + "\n"
if(cripto=="XRP"):
criptoName = "Ripple"
criptoInfo = client.get_ticker(symbol=i['symbol'])
json_str = json.dumps(criptoInfo)
criptoInfo = json.loads(json_str)
txt_price += ('%.2f' % round(float(criptoInfo['priceChangePercent']),2)) + "% "
txt_price += cripto + " (" +criptoName+ ")" +" = R$ "+ ('%.2f' % round(float(i['price']),2)) + "\n"
if(cripto=="DOT"):
criptoName = "Polkadot"
criptoInfo = client.get_ticker(symbol=i['symbol'])
json_str = json.dumps(criptoInfo)
criptoInfo = json.loads(json_str)
txt_price += ('%.2f' % round(float(criptoInfo['priceChangePercent']),2)) + "% "
txt_price += cripto + " (" +criptoName+ ")" +" = R$ "+ ('%.2f' % round(float(i['price']),2)) + "\n"
groups_list = GROUP_KEY.split('#')
for i in range(len(groups_list)):
bot.send_message(groups_list[i], txt_price)
#f = open("test.txt", "r")
#print(f.read())
#text_file = open("test.txt", "w")
#text_file.write("ok2")
#text_file.close()
f = open("test.txt", "r")
#print(f.read())
#bot.polling()