Put the TelegramPush.py file in config/userplugins/hooks/TelegramPush.py and restart pyLoad and fill the bot token and chat ids in the plugin settings
Created
November 25, 2019 13:35
-
-
Save dominik-th/71c3a0dcbf0a09f1437d685e0011052e to your computer and use it in GitHub Desktop.
pyLoad Telegram push notifications plugin
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
# -*- coding: utf-8 -*- | |
# based on PyLoadiOSPush.py from the pyLoad for iOS app | |
from ..internal.Addon import Addon | |
import httplib, urllib | |
class TelegramPush(Addon): | |
__name__ = "TelegramPush" | |
__type__ = "hook" | |
__version__ = "1.0" | |
__status__ = "working" | |
__config__ = [("botToken", "str", "Telegram bot token (without bot prefix)", ""), | |
("chatIds", "str", "Telegram chat ids (comma separated)", ""), | |
("pushCaptcha", "bool", "Push Captcha", True), | |
("pushDlFail", "bool", "Push Download Failed", True), | |
("pushPackFinish", "bool", "Push Package Finished", True), | |
("pushAllFinish", "bool", "Push All Packages Finished", True), | |
("pushUnrarFinish", "bool", "Push Unrar Finished", True), | |
("activated", "bool", "Activated", True)] | |
__description__ = "Push for pyLoad for iOS App" | |
__license__ = "MIT License" | |
__authors__ = [("Dominik Thiemermann", "[email protected]")] | |
TELEGRAM_API_URL = "api.telegram.org" | |
event_map = {"coreReady": "initialize"} | |
def initialize(self): | |
if self.config.get("deviceKey") == "": | |
self.config.set("activated", False) | |
def captcha_task(self, task): | |
if self.config.get("pushCaptcha"): | |
self.send_notification("captchaAvail", None) | |
def download_failed(self, pyfile): | |
if self.config.get("pushDlFail"): | |
self.send_notification("dlFail", pyfile.name) | |
def package_finished(self, pypack): | |
if self.config.get("pushPackFinish"): | |
self.send_notification("pkgFinish", pypack.name) | |
def all_downloads_finished(self): | |
if self.config.get("pushAllFinish"): | |
self.send_notification("allFinish", None) | |
def package_extracted(self, fname): | |
fnamesplit = fname.split("/") | |
name = fnamesplit[-1] | |
if self.config.get("pushUnrarFinish"): | |
self.send_notification("unrarFinish", name) | |
def send_notification(self, msg, val): | |
chat_ids = self.config.get("chatIds").split(",") | |
for chat_id in chat_ids: | |
self.log_debug("Pushing notification '" + msg + "' to chat " + chat_id) | |
self.telegram_api("sendMessage", { | |
"chat_id": chat_id, | |
"text": msg + (" " + val if val else "") | |
}) | |
def telegram_api(self, method, params): | |
self.log_debug("Call Telegram API: '" + method) | |
encoded_params = urllib.urlencode(params) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} | |
conn = httplib.HTTPSConnection(self.TELEGRAM_API_URL) | |
endpoint = "/bot" + self.config.get("botToken") + "/" + method | |
conn.request("POST", endpoint, encoded_params, headers) | |
response = conn.getresponse() | |
data = response.read() | |
conn.close() | |
self.log_debug("Telegram API response: '" + (data if data else "No response message")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment