This commit is contained in:
74
bot/commands/status.py
Normal file
74
bot/commands/status.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import datetime
|
||||
import requests
|
||||
import socket
|
||||
from telegram.ext import ConversationHandler, CommandHandler, CallbackQueryHandler, ParseMode
|
||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
||||
|
||||
|
||||
|
||||
FIRST = 1
|
||||
from .basehandler import BaseHandler
|
||||
class StatusHandler(BaseHandler):
|
||||
"""Shows a short status of the program."""
|
||||
|
||||
def __init__(self, entry_string, models):
|
||||
self.start_time = datetime.datetime.now()
|
||||
self.entry_string = entry_string
|
||||
self.models = models
|
||||
self.handler = ConversationHandler(
|
||||
entry_points=[CommandHandler(self.entry_string, self.entry_point)],
|
||||
states={
|
||||
FIRST: [
|
||||
CallbackQueryHandler(self.send_log, pattern="^full$"),
|
||||
]
|
||||
},
|
||||
fallbacks=[CommandHandler('status', self.entry_point)],
|
||||
)
|
||||
|
||||
|
||||
async def entry_point(self, update, context) -> None:
|
||||
super().entry_point(update, context)
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton("And the log?", callback_data="full"),
|
||||
]
|
||||
]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
delta = str(datetime.datetime.now() - self.start_time)
|
||||
message = "BeebBop, this is Norbit)\n"
|
||||
|
||||
try:
|
||||
ip = requests.get('https://api.ipify.org').text
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
s.connect(('8.8.8.8', 80))
|
||||
(addr, port) = s.getsockname()
|
||||
local_ips = addr
|
||||
except:
|
||||
ip = "not fetchable"
|
||||
local_ips = "not fetchable"
|
||||
|
||||
message += "Status: Running 🟢\n"
|
||||
message += "Uptime: `" + delta[:delta.rfind(".")] + "`\n"
|
||||
message += "IP (public): `" + ip + "`\n"
|
||||
message += "IP (private): `" + str(local_ips) + "`\n"
|
||||
|
||||
|
||||
if update.message:
|
||||
await update.message.reply_text(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN)
|
||||
else:
|
||||
await update._effective_chat.send_message(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN)
|
||||
|
||||
return FIRST
|
||||
|
||||
|
||||
async def send_log(self, update, context) -> None:
|
||||
query = update.callback_query
|
||||
wanted = query.data.replace("status-","")
|
||||
query.answer()
|
||||
# with open("persistence/server.log") as l:
|
||||
# query.message.reply_document(l)
|
||||
await update.message.reply_text("Not implemented yet.")
|
||||
super().log_activity(read = False, execute = False, send = True)
|
||||
return ConversationHandler.END
|
||||
|
Reference in New Issue
Block a user