From d9e0f2e03c149f1c52c328688e7681eedf1cd248 Mon Sep 17 00:00:00 2001 From: mctaylors Date: Mon, 18 Nov 2024 21:27:51 +0500 Subject: [PATCH] Initial commit --- .gitignore | 6 ++++++ LICENSE | 13 +++++++++++++ README.md | 3 +++ main.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a56dfdd --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/ +.venv/ +build/ +dist/ +__pycache__/ +*.spec \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5c93f45 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/README.md b/README.md new file mode 100644 index 0000000..53eb4c4 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Stuckach + +A simple Telegram bot for checking server availability. \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..4beedde --- /dev/null +++ b/main.py @@ -0,0 +1,57 @@ +import logging +import os + +import requests +from telegram import Update +from telegram.constants import ParseMode +from telegram.ext import Application, CommandHandler, ContextTypes + +# Enable logging +logging.basicConfig( + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO +) +# set higher logging level for httpx to avoid all GET and POST requests being logged +logging.getLogger("httpx").setLevel(logging.WARNING) +logger = logging.getLogger(__name__) + + +async def ping_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + ready_message = await context.bot.send_message(chat_id=update.effective_chat.id, + text="*️⃣ Requesting...") + try: + resp = requests.post(os.getenv("REQUEST_URL"), headers={'User-Agent': 'Custom'}) + message = f"✅ Server responded with HTTP status code *{resp.status_code}*." + except Exception as e: + message = "❌ Caught exception. Server is probably *offline*." + dev_message = (f"Caught exception @ {update.effective_user.mention_markdown()} " + f"(`{update.effective_chat.id}`)\n" + f"```\n{e}```") + await context.bot.send_message(chat_id=os.getenv("DEVELOPER_ID"), + text=dev_message, + parse_mode=ParseMode.MARKDOWN) + await context.bot.edit_message_text(text=str(message), + chat_id=ready_message.chat.id, + message_id=ready_message.message_id, + parse_mode=ParseMode.MARKDOWN) + + +def main() -> None: + envs_not_set = False + envs = ['BOT_TOKEN', 'REQUEST_URL', 'DEVELOPER_ID'] + for env in envs: + if os.getenv(env) is None: + envs_not_set = True + print(f"{env} is not set.") + if envs_not_set: exit(1) + + # Create the Application and pass it your bot's token. + application = Application.builder().token(os.getenv("BOT_TOKEN")).build() + + application.add_handler(CommandHandler("ping", ping_command)) + + # Run the bot until the user presses Ctrl-C + application.run_polling(allowed_updates=Update.ALL_TYPES) + + +if __name__ == "__main__": + main()