From d0b91106fc70bdd56d1c116d1d61be805d26e22f Mon Sep 17 00:00:00 2001 From: Theo Date: Fri, 8 Dec 2023 04:19:39 -0800 Subject: [PATCH] Bot based on Binance API (decrecated due to delisting) --- .env.example | 2 ++ .gitignore | 2 ++ README.md | 8 ++++++++ main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | Bin 0 -> 46 bytes 5 files changed, 54 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..5947edf --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +BOT_TOKEN= +TELEGRAM_CHANNEL_NAME=@torn_prices \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3bf780b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..81ebed5 --- /dev/null +++ b/README.md @@ -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 &` diff --git a/main.py b/main.py new file mode 100644 index 0000000..db0ea82 --- /dev/null +++ b/main.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..4b41642426c2433a0fabc424ec061a842042b254 GIT binary patch literal 46 tcmezWuYjSFp@boWA)g_SL6;!~NS83A0@-B@wm@jepvPbU#=Hz%3;-CX2-^Sv literal 0 HcmV?d00001