Skip to content

Instantly share code, notes, and snippets.

@Sh4yy
Created May 5, 2020 23:12
Show Gist options
  • Save Sh4yy/3bf8d5386efa826b734fcf1eab26f9ea to your computer and use it in GitHub Desktop.
Save Sh4yy/3bf8d5386efa826b734fcf1eab26f9ea to your computer and use it in GitHub Desktop.
A simple Telegram Logging Script
from enum import Enum
import requests
class Condition(Enum):
WARNING = "⚠️"
MESSAGE = "🤖"
INFO = "💬"
class Logger:
def __init__(self, token, chat_id):
self._token = token
self._chat_id = chat_id
self._url = f"https://api.telegram.org/bot{token}/"
def log(self, message, condition=None):
text = message
if condition is not None:
text = f"{condition.value} {text}"
url = self._url + "sendMessage"
resp = requests.post(
url=url,
params={
"chat_id": self._chat_id,
"text": text
}
)
resp.raise_for_status()
def warning(self, message):
self.log(message, Condition.WARNING)
def message(self, message):
self.log(message, Condition.MESSAGE)
def info(self, message):
self.log(message, Condition.INFO)
# testing
token = "telegram bot token"
chat_id = "your telegram chat id"
logger = Logger(token=token, chat_id=chat_id)
logger.info("something is happening")
logger.warning("something went wrong")
logger.message("here's a message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment