Initial commit
This commit is contained in:
parent
649d51102d
commit
736fb9e04a
4 changed files with 79 additions and 0 deletions
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
.idea/
|
||||||
|
.venv/
|
||||||
|
build/
|
||||||
|
dist/
|
||||||
|
__pycache__/
|
||||||
|
*.spec
|
13
LICENSE
Normal file
13
LICENSE
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
Version 2, December 2004
|
||||||
|
|
||||||
|
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
||||||
|
|
||||||
|
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.
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# Stuckach
|
||||||
|
|
||||||
|
A simple Telegram bot for checking server availability.
|
57
main.py
Normal file
57
main.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue