Bot based on Binance API (decrecated due to delisting)

This commit is contained in:
Theo 2023-12-08 04:19:39 -08:00
commit d0b91106fc
5 changed files with 54 additions and 0 deletions

2
.env.example Normal file

@ -0,0 +1,2 @@
BOT_TOKEN=
TELEGRAM_CHANNEL_NAME=@torn_prices

2
.gitignore vendored Normal file

@ -0,0 +1,2 @@
.idea
.env

8
README.md Normal file

@ -0,0 +1,8 @@
## Startup on your own server
1. Create telegram bot using [@BotFather](https://t.me/BotFather) and get token;
2. Create telegram public channel, add a bot to it and give him admin rights (At least, `Post messages` permission);
3. Copy `.env.example` file content to `.env`;
4. Fill in `.env` file `BOT_TOKEN` value with a string containing your telegram bot token;
5. Replace in `.env` file `TELEGRAM_CHANNEL_NAME` value with a string containing inner telegram link to your public channel (for example, `@torn_prices`);
6. Start script in the background: `python3 main.py &`

42
main.py Normal file

@ -0,0 +1,42 @@
from urllib.error import HTTPError
from urllib.request import urlopen as get
from threading import Timer
import json
import os
from dotenv import load_dotenv
load_dotenv()
BINANCE_API_GET_ETH_PRICE_LINK = "https://api.binance.com/api/v3/ticker/price?symbol=TORNBUSD"
BOT_TOKEN = os.getenv("BOT_TOKEN")
TELEGRAM_CHANNEL_NAME = os.getenv("TELEGRAM_CHANNEL_NAME")
TELEGRAM_API_SEND_MESSAGE_LINK = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={TELEGRAM_CHANNEL_NAME}&text="
CHECK_PRICE_INTERVAL_IN_SECONDS = 10
def send_price_to_channel(price: float):
fixed_price = "{:10.2f}".format(price).strip()
full_request_link = f'{TELEGRAM_API_SEND_MESSAGE_LINK}{fixed_price}$'
try:
get(full_request_link)
except HTTPError:
pass
def get_eth_price_from_binance() -> float:
try:
data = json.load(get(BINANCE_API_GET_ETH_PRICE_LINK))
return float(data["price"])
except HTTPError:
return -1
def main():
Timer(CHECK_PRICE_INTERVAL_IN_SECONDS, main).start()
current_eth_price = get_eth_price_from_binance()
if current_eth_price == -1:
return
send_price_to_channel(current_eth_price)
main()

BIN
requirements.txt Normal file

Binary file not shown.