-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telegram.py
69 lines (56 loc) · 2.64 KB
/
Telegram.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
import requests
import telegram
import time
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
# Get the bot token from the configuration file
bot_token = config['telegram']['bot_token']
# Create the bot using the token from the configuration file
bot = telegram.Bot(token=bot_token)
def handle_message(message):
# Check if the message is a command to get the miner's status
if message.text.lower() == '/status':
# Make an HTTP GET request to the Nanopool API to get the miner's status
# Ask the user for their Nanopool API key
bot.send_message(chat_id=message.chat_id, text='Please enter your Nanopool API key:')
api_key_message = bot.wait_for_message(chat_id=message.chat_id)
# Check if the user entered an API key
if api_key_message.text:
# Get the miner's status using the provided API key
check_status(api_key_message.text)
else:
bot.send_message(chat_id=message.chat_id, text='No API key provided. Please try again.')
response = requests.get(f'https://api.nanopool.org/v1/rvn/user/{API_KEY}')
data = response.json()
# Extract the relevant information from the API response and format it into a message
message_text = f'Miner status: {data["status"]}\n'
message_text2 = f'Hashrate: {data["data"]["hashrate"]} MH/s\n'
#message_text += f'Hashrate: {data["hashrate"]} MH/s\n'
message_text3 = f'Unpaid balance: {data["data"]["unconfirmed_balance"]} RVN\n'
message_text4 = f'Balance: {data["data"]["balance"]}'
# Use the Telegram API to send the message back to the user
bot.send_message(chat_id=message.chat_id, text=message_text + message_text2 + message_text3 + message_text4)
#def check_status():
# Make an HTTP GET request to the Nanopool API to get the miner's status
#3 response = requests.get(f'https://api.nanopool.org/v1/rvn/user/{API_KEY}')
#data = response.json()
# Check if the status is "false"
#if data["status"] == "false":
# Send a notification to the user
# bot.send_message(text='The miner is not running!')
# Set up a loop to check the status every 15 minutes
#while True:
# check_status()
# time.sleep(900) # pause for 15 minutes (900 seconds)
# updates = bot.get_updates(offset=update_id, timeout=10)
# for update in updates:
# update_id = update.update_id + 1
# handle_message(update.message)
# Set up a loop to continuously listen for incoming messages
update_id = None
while True:
updates = bot.get_updates(offset=update_id, timeout=10)
for update in updates:
update_id = update.update_id + 1
handle_message(update.message)