From 4805f2f6de7d734c341bb978318f44323ad525f1 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 1 Jul 2018 16:19:27 +0300 Subject: [PATCH 1/7] Fix #13 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 15ef170..5b0996c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -telethon-aio +telethon From f1141aa24fd5e5303e2330998ef0bf106e8945b5 Mon Sep 17 00:00:00 2001 From: Lonami Date: Sun, 1 Jul 2018 16:14:02 +0200 Subject: [PATCH 2/7] Create README.md --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b08237 --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# uniborg + +Pluggable [``asyncio``](https://docs.python.org/3/library/asyncio.html) +[Telegram](https://telegram.org) userbot based on +[Telethon](https://github.com/LonamiWebs/Telethon). + +## installing + +Simply clone the repository and run the main file: +```sh +git clone https://github.com/uniborg/uniborg.git +cd uniborg +python stdborg.py +``` + +## design + +The modular design of the project enhances your Telegram experience +through [plugins](https://github.com/uniborg/uniborg/tree/master/stdplugins) +which you can enable or disable on demand. + +Each plugin gets the `borg`, `logger` and `storage` magical +[variables](https://github.com/uniborg/uniborg/blob/4805f2f6de7d734c341bb978318f44323ad525f1/uniborg/uniborg.py#L66-L68) +to ease their use. Thus creating a plugin as easy as adding +a new file under the plugin directory to do the job: + +```python +# stdplugins/myplugin.py +from telethon import events + +@borg.on(events.NewMessage(pattern='hi')) +async def handler(event): + await event.reply('hey') +``` + +## internals + +The core features offered by the custom `TelegramClient` live under the +[`uniborg/`](https://github.com/uniborg/uniborg/tree/master/uniborg) +directory, with some utilities, enhancements and the core plugin, which +can be unloaded in itself (something fun to do, but you will probably +want to restart your borg after). + +## learning + +Check out the already-mentioned +[plugins](https://github.com/uniborg/uniborg/tree/master/stdplugins) +directory to learn how to write your own, and consider reading +[Telethon's documentation](http://telethon.readthedocs.io/). From c1e3b2f3c351b38370972ae1150a868fb64761b7 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 1 Jul 2018 17:43:54 +0300 Subject: [PATCH 3/7] Don't allow unloading _core Prevents getting stuck with no way of administrating the bot --- README.md | 4 +--- uniborg/_core.py | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6b08237..a50a48b 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,7 @@ async def handler(event): The core features offered by the custom `TelegramClient` live under the [`uniborg/`](https://github.com/uniborg/uniborg/tree/master/uniborg) -directory, with some utilities, enhancements and the core plugin, which -can be unloaded in itself (something fun to do, but you will probably -want to restart your borg after). +directory, with some utilities, enhancements and the core plugin. ## learning diff --git a/uniborg/_core.py b/uniborg/_core.py index 933a5ee..77d105b 100644 --- a/uniborg/_core.py +++ b/uniborg/_core.py @@ -36,7 +36,9 @@ async def remove(event): await event.delete() shortname = event.pattern_match["shortname"] - if shortname in borg._plugins: + if shortname == "_core": + msg = await event.respond(f"Not removing {shortname}") + elif shortname in borg._plugins: borg.remove_plugin(shortname) msg = await event.respond(f"Removed plugin {shortname}") else: From e809a721b44edf0ecca912fc55e65562f39299b6 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 1 Jul 2018 22:02:30 +0300 Subject: [PATCH 4/7] Remove headpat plugin --- stdplugins/headpat.py | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 stdplugins/headpat.py diff --git a/stdplugins/headpat.py b/stdplugins/headpat.py deleted file mode 100644 index 84d88f7..0000000 --- a/stdplugins/headpat.py +++ /dev/null @@ -1,42 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -import re -import random -import json -import urllib.request -import urllib.parse - -from telethon import events - -pats = [] -oops = "OOPSIE WOOPSIE!! Uwu We madea fucky wucky!! A wittle fucko boingo! " \ - "The code monkeys at our headquarters are working VEWY HAWD to fix " \ - "this!" - - -@borg.on(events.NewMessage) -async def on_pat(event): - if event.fwd_from: - return - - user = borg.me.username - - if not user or not re.match(fr"(?i)/headpat@{user}", event.raw_text): - return - - global pats - if not pats: - try: - pats = json.loads(urllib.request.urlopen(urllib.request.Request( - "http://headp.at/js/pats.json", - headers={"User-Agent": "Mozilla/5.0 (X11; U; Linux i686) " - "Gecko/20071127 Firefox/2.0.0.11"} - )).read().decode("utf-8")) - except Exception as e: - print(e) - await event.reply(oops) - return - - choice = urllib.parse.quote(random.choice(pats)) - await event.reply(f"[Pat!](https://headp.at/pats/{choice})") From 199d9b15eadbd87794674c81f8932158c362169c Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 2 Jul 2018 15:30:21 +0200 Subject: [PATCH 5/7] Fix sed plugin ignoring edits --- stdplugins/sed.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stdplugins/sed.py b/stdplugins/sed.py index 206cb1f..02422ef 100644 --- a/stdplugins/sed.py +++ b/stdplugins/sed.py @@ -77,6 +77,12 @@ async def group_has_sedbot(group): async def on_message(event): last_msgs[event.chat_id].appendleft(event.message) +@borg.on(events.MessageEdited) +async def on_edit(event): + for m in last_msgs[event.chat_id]: + if m.id == event.id: + m.raw_text = event.raw_text + break @borg.on(events.NewMessage( pattern=re.compile(r"^s/((?:\\/|[^/])+)/((?:\\/|[^/])*)(/.*)?"))) From f80b450fcffaed9c069c7c2a5281dcd7abcfb341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9A=D0=BE=D0=BD?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Sun, 1 Jul 2018 18:20:17 +0300 Subject: [PATCH 6/7] add *.session-journal to `.gitignore` --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1d76580..979c0d3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ venv __pycache__ *.session +*.session-journal data/ From 3efb5a09a0fbe8cdcae45ccb48b4a89479eded91 Mon Sep 17 00:00:00 2001 From: Lonami Date: Thu, 19 Jul 2018 09:59:33 +0200 Subject: [PATCH 7/7] Fix moved import That's what you get for importing private members --- stdplugins/markdown.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/stdplugins/markdown.py b/stdplugins/markdown.py index fa1a5cc..13fec87 100644 --- a/stdplugins/markdown.py +++ b/stdplugins/markdown.py @@ -6,9 +6,8 @@ from functools import partial from telethon import events from telethon.tl.functions.messages import EditMessageRequest -from telethon.extensions.markdown import ( - DEFAULT_URL_RE, _add_surrogate, _del_surrogate -) +from telethon.extensions.markdown import DEFAULT_URL_RE +from telethon.utils import add_surrogate, del_surrogate from telethon.tl.types import ( MessageEntityBold, MessageEntityItalic, MessageEntityCode, MessageEntityPre, MessageEntityTextUrl @@ -19,7 +18,7 @@ def parse_url_match(m): entity = MessageEntityTextUrl( offset=m.start(), length=len(m.group(1)), - url=_del_surrogate(m.group(2)) + url=del_surrogate(m.group(2)) ) return m.group(1), entity @@ -90,7 +89,7 @@ def parse(message, old_entities=None): i = 0 after = 0 - message = _add_surrogate(message) + message = add_surrogate(message) while i < len(message): for after, e in enumerate(old_entities[after:], start=after): # If the next entity is strictly to our right, we're done here @@ -131,7 +130,7 @@ def parse(message, old_entities=None): # Skip past the match i += len(text) - return _del_surrogate(message), entities + old_entities + return del_surrogate(message), entities + old_entities @borg.on(events.MessageEdited(outgoing=True))