Last active
January 13, 2021 09:04
-
-
Save 11philip22/67e50639210024a3ed0ff6c1a59a8871 to your computer and use it in GitHub Desktop.
Simple Class to add to your script for telegram notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from json import loads | |
from requests import post | |
class TelegramBot: | |
def __init__(self, id_): | |
self.id = id_ | |
@staticmethod | |
def post_request(request): | |
response = post(request).content | |
return loads(response) | |
def send_message(self, message: str, chat: str): | |
formatted_message = message.replace(" ", "%20") | |
request = f"https://api.telegram.org/bot{self.id}/sendMessage?chat_id={chat}&text={formatted_message}" | |
return self.post_request(request) | |
def get_updates(self): | |
request = f"https://api.telegram.org/bot{self.id}/getUpdates" | |
return self.post_request(request) | |
def get_last_chat_id(self): | |
updates = self.get_updates() | |
last_message = updates["result"][-1] | |
chat_id = last_message["message"]["chat"]["id"] | |
return chat_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment