diff --git a/.gitignore b/.gitignore index 1d76580..979c0d3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ venv __pycache__ *.session +*.session-journal data/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..a50a48b --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# 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. + +## 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/). diff --git a/requirements.txt b/requirements.txt index 15ef170..5b0996c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -telethon-aio +telethon 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})") 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)) 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: