forked from kate/uniborg
Compare commits
34 Commits
Author | SHA1 | Date |
---|---|---|
udf | 27d7b5ed82 | |
Dan Elkouby | 9fe2e80742 | |
Dan Elkouby | 40c398833d | |
Lonami | 332ecdcaea | |
Lonami | 69ccfd1207 | |
Lonami | 6d67ebad06 | |
Lonami | 1dd6e63d6a | |
kate | fe6b878064 | |
udf | 573f36bf57 | |
udf | b21f5ec104 | |
Lonami | 168ee165b9 | |
Lonami | 93cbb44459 | |
Lonami | febe3c4bac | |
Lonami | c8b5179cb5 | |
Dan Elkouby | c2197e5280 | |
Dan Elkouby | ca5619b9cc | |
Lonami | b4be66162e | |
Lonami Exo | 59a0ebe9dc | |
Dan Elkouby | 88a8b4e0d3 | |
Dan Elkouby | a53b5fb147 | |
Lonami | dc2eefd581 | |
Lonami | 84e3c4bef8 | |
Dan Elkouby | d274f79d87 | |
Lonami | 03e922ed86 | |
Lonami | 319eb84c5a | |
Lonami | a254c113a7 | |
Lonami | c41792af6b | |
Lonami | facd436014 | |
Lonami | 2c75fb25a3 | |
Lonami | ca7b636696 | |
Lonami | f46a08adf1 | |
Lonami | c35dcbf26d | |
Lonami | 5438f95f34 | |
Dan Elkouby | d5f70423a3 |
10
README.md
10
README.md
|
@ -8,7 +8,7 @@ Pluggable [``asyncio``](https://docs.python.org/3/library/asyncio.html)
|
||||||
|
|
||||||
Simply clone the repository and run the main file:
|
Simply clone the repository and run the main file:
|
||||||
```sh
|
```sh
|
||||||
git clone https://github.com/udf/uniborg.git
|
git clone https://github.com/uniborg/uniborg.git
|
||||||
cd uniborg
|
cd uniborg
|
||||||
python stdborg.py
|
python stdborg.py
|
||||||
```
|
```
|
||||||
|
@ -16,11 +16,11 @@ python stdborg.py
|
||||||
## design
|
## design
|
||||||
|
|
||||||
The modular design of the project enhances your Telegram experience
|
The modular design of the project enhances your Telegram experience
|
||||||
through [plugins](https://github.com/udf/uniborg/tree/master/stdplugins)
|
through [plugins](https://github.com/uniborg/uniborg/tree/master/stdplugins)
|
||||||
which you can enable or disable on demand.
|
which you can enable or disable on demand.
|
||||||
|
|
||||||
Each plugin gets the `borg`, `logger` and `storage` magical
|
Each plugin gets the `borg`, `logger` and `storage` magical
|
||||||
[variables](https://github.com/udf/uniborg/blob/4805f2f6de7d734c341bb978318f44323ad525f1/uniborg/uniborg.py#L66-L68)
|
[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
|
to ease their use. Thus creating a plugin as easy as adding
|
||||||
a new file under the plugin directory to do the job:
|
a new file under the plugin directory to do the job:
|
||||||
|
|
||||||
|
@ -36,12 +36,12 @@ async def handler(event):
|
||||||
## internals
|
## internals
|
||||||
|
|
||||||
The core features offered by the custom `TelegramClient` live under the
|
The core features offered by the custom `TelegramClient` live under the
|
||||||
[`uniborg/`](https://github.com/udf/uniborg/tree/master/uniborg)
|
[`uniborg/`](https://github.com/uniborg/uniborg/tree/master/uniborg)
|
||||||
directory, with some utilities, enhancements and the core plugin.
|
directory, with some utilities, enhancements and the core plugin.
|
||||||
|
|
||||||
## learning
|
## learning
|
||||||
|
|
||||||
Check out the already-mentioned
|
Check out the already-mentioned
|
||||||
[plugins](https://github.com/udf/uniborg/tree/master/stdplugins)
|
[plugins](https://github.com/uniborg/uniborg/tree/master/stdplugins)
|
||||||
directory to learn how to write your own, and consider reading
|
directory to learn how to write your own, and consider reading
|
||||||
[Telethon's documentation](http://telethon.readthedocs.io/).
|
[Telethon's documentation](http://telethon.readthedocs.io/).
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
import asyncio
|
|
||||||
from telethon import events
|
|
||||||
from telethon.tl import types
|
|
||||||
|
|
||||||
EPIC_MEME = (
|
|
||||||
"""
|
|
||||||
⢠⠤⢠⢄⠠⡤⢀⠤⠀⠀⠀⠀⢀⣔⢔⢔⢕⢔⢕⢔⢐⢀⠀⠀⠀⠀⠀⠀
|
|
||||||
⠸⠥⠸⠁⠠⠧⠘⠤⠀⠀⠀⣐⢗⢕⢗⢕⢕⢕⢕⢔⢑⢐⢀⠀⠀⠀⠀⠀
|
|
||||||
⢰⠑⢱⢸⠭⢰⠑⢱⢸⠭⢐⣟⣗⣕⢗⣗⢑⢕⢑⢕⢑⠐⢑⢐⢐⢐⢀⠀
|
|
||||||
⢨⢄⢨⢨⢩⢌⢠⠬⢨⠉⢐⣟⢗⢕⢑⣿⣷⣕⣑⠕⢑⢐⣗⠐⢐⢐⢐⢐
|
|
||||||
⠸⠜⠘⠜⠸⠜⠸⠥⠨⢀⢐⢑⢕⢕⢑⢛⣿⣿⣷⣔⣴⡟⠓⠀⢀⢔⢑⠑
|
|
||||||
⠀⠀⠀⠀⠀⠀⠀⠀⢐⢗⢑⠐⠐⢷⣷⣷⣿⣿⣿⣿⣿⢕⢐⢔⢑⢔⠁⠀
|
|
||||||
⠀⠀⠀⠀⠀⠀⠀⠰⢗⢕⢗⢕⢕⢔⠙⢟⢿⣕⣷⣟⣗⣔⠀⠀⠀⠀⠀⠀
|
|
||||||
⠀⠀⠀⠀⠀⠘⢶⠄⠀⠐⠁⠐⠁⠀⠀⢀⣐⣖⢷⣟⣷⣿⣷⣔⣀⠀⠀⠀
|
|
||||||
⠀⠀⠀⠀⠀⠀⢘⣗⣴⣴⠀⠀⠀⢀⣴⣿⣿⢟⢗⢛⣿⣟⣟⣟⣿⣟⣗⠀
|
|
||||||
⠀⠀⠀⠀⠀⠀⢐⣿⣿⣿⡇⠀⢐⣿⣿⣟⣗⢐⢐⣔⣿⣗⣷⣟⣿⣟⣿⣗
|
|
||||||
⠀⠀⠀⠀⠀⠀⢰⣿⣿⣟⣁⢐⢿⣟⣿⣿⣗⠐⢐⣟⣿⣟⣟⣟⣷⣟⣿⣗
|
|
||||||
⠀⠀⠀⠀⠀⠀⢙⣟⣿⣿⣷⣗⣖⣿⣿⣟⢑⠐⢐⣟⣗⣟⣗⣗⢗⠟⢓⣑
|
|
||||||
⠀⠀⠀⠀⠀⠀⠐⠑⠛⠛⠛⠑⠓⠙⠛⠓⠁⠐⠐⠛⠓⠛⠓⠓⠑⠒⠓⠛
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r"^h$"))
|
|
||||||
async def on_h(event):
|
|
||||||
await borg.reply(file="CAADAQADpgIAAna32gVJ62JcFcDnqwI")
|
|
||||||
|
|
||||||
async def del_filter(del_event):
|
|
||||||
if del_event.chat_id and del_event.chat_id != event.chat_id:
|
|
||||||
return False
|
|
||||||
return del_event.deleted_id == message.id
|
|
||||||
fut = borg.await_event(events.MessageDeleted, del_filter)
|
|
||||||
|
|
||||||
try:
|
|
||||||
await asyncio.wait_for(fut, timeout=5)
|
|
||||||
await event.reply(EPIC_MEME)
|
|
||||||
except asyncio.TimeoutError:
|
|
||||||
pass
|
|
|
@ -1,26 +0,0 @@
|
||||||
# kicks people who join a group by invite
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from telethon import events
|
|
||||||
|
|
||||||
from telethon.tl.functions.channels import EditBannedRequest
|
|
||||||
from telethon.tl.types import ChannelBannedRights
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
channel_id = 1252035294
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.ChatAction(chats=channel_id))
|
|
||||||
async def on_join(event):
|
|
||||||
if event.user_joined:
|
|
||||||
await borg(
|
|
||||||
EditBannedRequest(
|
|
||||||
channel=channel_id,
|
|
||||||
user_id=await event.get_user(),
|
|
||||||
banned_rights=ChannelBannedRights(
|
|
||||||
time.time() + 60,
|
|
||||||
True, True, True, True, True, True, True, True
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
# 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/.
|
||||||
|
|
||||||
|
from telethon import events
|
||||||
|
|
||||||
|
|
||||||
|
@borg.on(events.NewMessage(pattern=r"\.all", outgoing=True))
|
||||||
|
async def _(event):
|
||||||
|
if event.fwd_from:
|
||||||
|
return
|
||||||
|
await event.delete()
|
||||||
|
mentions = "@all"
|
||||||
|
chat = await event.get_input_chat()
|
||||||
|
async for x in borg.iter_participants(chat, 100):
|
||||||
|
mentions += f"[\u2063](tg://user?id={x.id})"
|
||||||
|
await borg.send_message(
|
||||||
|
chat, mentions, reply_to=event.message.reply_to_msg_id)
|
|
@ -0,0 +1,27 @@
|
||||||
|
# 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 asyncio
|
||||||
|
|
||||||
|
from telethon import events
|
||||||
|
|
||||||
|
|
||||||
|
_last_messages = {}
|
||||||
|
|
||||||
|
|
||||||
|
@borg.on(events.NewMessage(outgoing=True))
|
||||||
|
async def _(event):
|
||||||
|
_last_messages[event.chat_id] = event.message
|
||||||
|
|
||||||
|
|
||||||
|
@borg.on(events.NewMessage(pattern=r"\.(fix)?reply", outgoing=True))
|
||||||
|
async def _(event):
|
||||||
|
if not event.is_reply or event.chat_id not in _last_messages:
|
||||||
|
return
|
||||||
|
|
||||||
|
message = _last_messages[event.chat_id]
|
||||||
|
chat = await event.get_input_chat()
|
||||||
|
await asyncio.wait([
|
||||||
|
borg.delete_messages(chat, [event.id, message.id]),
|
||||||
|
borg.send_message(chat, message, reply_to=event.reply_to_msg_id)
|
||||||
|
])
|
|
@ -0,0 +1,27 @@
|
||||||
|
# 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 string
|
||||||
|
|
||||||
|
from telethon import events
|
||||||
|
from telethon.tl import types
|
||||||
|
|
||||||
|
msg_cache = {}
|
||||||
|
|
||||||
|
|
||||||
|
@borg.on(events.NewMessage(pattern=r"\.fpost\s+(.*)", outgoing=True))
|
||||||
|
async def _(event):
|
||||||
|
await event.delete()
|
||||||
|
text = event.pattern_match.group(1)
|
||||||
|
destination = await event.get_input_chat()
|
||||||
|
|
||||||
|
for c in text.lower():
|
||||||
|
if c not in string.ascii_lowercase:
|
||||||
|
continue
|
||||||
|
if c not in msg_cache:
|
||||||
|
async for msg in borg.iter_messages(None, search=c):
|
||||||
|
if msg.raw_text.lower() == c and msg.media is None:
|
||||||
|
msg_cache[c] = msg
|
||||||
|
break
|
||||||
|
await borg.forward_messages(destination, msg_cache[c])
|
|
@ -2,15 +2,12 @@
|
||||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
# 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/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
import string
|
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon.utils import add_surrogate
|
from telethon.utils import add_surrogate
|
||||||
from telethon.tl.types import MessageEntityPre
|
from telethon.tl.types import MessageEntityPre
|
||||||
from telethon.tl.tlobject import TLObject
|
from telethon.tl.tlobject import TLObject
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
PRINTABLE_SET = set(string.printable.encode())
|
|
||||||
STR_LEN_MAX = 256
|
STR_LEN_MAX = 256
|
||||||
BYTE_LEN_MAX = 64
|
BYTE_LEN_MAX = 64
|
||||||
|
|
||||||
|
@ -40,7 +37,7 @@ def yaml_format(obj, indent=0):
|
||||||
has_multiple_items = len(items) > 2
|
has_multiple_items = len(items) > 2
|
||||||
if has_multiple_items:
|
if has_multiple_items:
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
indent += 2
|
indent += 2
|
||||||
for k, v in items:
|
for k, v in items:
|
||||||
if k == '_' or v is None:
|
if k == '_' or v is None:
|
||||||
continue
|
continue
|
||||||
|
@ -48,11 +45,14 @@ def yaml_format(obj, indent=0):
|
||||||
if not formatted.strip():
|
if not formatted.strip():
|
||||||
continue
|
continue
|
||||||
result.append(' ' * (indent if has_multiple_items else 1))
|
result.append(' ' * (indent if has_multiple_items else 1))
|
||||||
result.append(f'{k}: {formatted}')
|
result.append(f'{k}:')
|
||||||
|
if not formatted[0].isspace():
|
||||||
|
result.append(' ')
|
||||||
|
result.append(f'{formatted}')
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
result.pop()
|
result.pop()
|
||||||
indent -= 2
|
if has_multiple_items:
|
||||||
result.append(' ' * indent)
|
indent -= 2
|
||||||
elif isinstance(obj, str):
|
elif isinstance(obj, str):
|
||||||
# truncate long strings and display elipsis
|
# truncate long strings and display elipsis
|
||||||
result = repr(obj[:STR_LEN_MAX])
|
result = repr(obj[:STR_LEN_MAX])
|
||||||
|
@ -61,7 +61,7 @@ def yaml_format(obj, indent=0):
|
||||||
return result
|
return result
|
||||||
elif isinstance(obj, bytes):
|
elif isinstance(obj, bytes):
|
||||||
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
|
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
|
||||||
if all(c in PRINTABLE_SET for c in obj):
|
if all(0x20 <= c < 0x7f for c in obj):
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
else:
|
else:
|
||||||
return ('<…>' if len(obj) > BYTE_LEN_MAX else
|
return ('<…>' if len(obj) > BYTE_LEN_MAX else
|
||||||
|
@ -74,12 +74,10 @@ def yaml_format(obj, indent=0):
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
indent += 2
|
indent += 2
|
||||||
for x in obj:
|
for x in obj:
|
||||||
|
|
||||||
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
|
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
result.pop()
|
result.pop()
|
||||||
indent -= 2
|
indent -= 2
|
||||||
result.append(' ' * indent)
|
|
||||||
else:
|
else:
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
|
|
||||||
|
@ -92,7 +90,4 @@ async def _(event):
|
||||||
return
|
return
|
||||||
msg = await event.message.get_reply_message()
|
msg = await event.message.get_reply_message()
|
||||||
yaml_text = yaml_format(msg)
|
yaml_text = yaml_format(msg)
|
||||||
await event.edit(
|
await event.edit(yaml_text, parse_mode=parse_pre)
|
||||||
yaml_text,
|
|
||||||
parse_mode=parse_pre
|
|
||||||
)
|
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
"""
|
|
||||||
Contains code used by other kbass_* plugins
|
|
||||||
"""
|
|
||||||
from uniborg import util
|
|
||||||
|
|
||||||
|
|
||||||
def self_reply_cmd(borg, pattern):
|
|
||||||
def wrapper(function):
|
|
||||||
@borg.on(util.admin_cmd(pattern))
|
|
||||||
async def wrapped(event, *args, **kwargs):
|
|
||||||
await event.delete()
|
|
||||||
target = await util.get_target_message(borg, event)
|
|
||||||
if not target:
|
|
||||||
return
|
|
||||||
return await function(event, target, *args, **kwargs)
|
|
||||||
return wrapped
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def self_reply_selector(borg, cmd):
|
|
||||||
def wrapper(function):
|
|
||||||
@borg.on(util.admin_cmd(cmd + r"( [+-]?\d+)?$"))
|
|
||||||
async def wrapped(event, *args, **kwargs):
|
|
||||||
await event.delete()
|
|
||||||
reply = await event.get_reply_message()
|
|
||||||
if not reply:
|
|
||||||
return
|
|
||||||
num_offset = int(event.pattern_match.group(1) or 0)
|
|
||||||
reverse = num_offset > 0
|
|
||||||
|
|
||||||
targets = [reply] if reverse else []
|
|
||||||
targets.extend(await borg.get_messages(
|
|
||||||
await event.get_input_chat(),
|
|
||||||
limit=abs(num_offset),
|
|
||||||
offset_id=reply.id,
|
|
||||||
reverse=reverse
|
|
||||||
))
|
|
||||||
if not reverse:
|
|
||||||
# reverse the list because we want things to always be in
|
|
||||||
# history order
|
|
||||||
targets.reverse()
|
|
||||||
targets.append(reply)
|
|
||||||
|
|
||||||
return await function(event, targets, num_offset, *args, **kwargs)
|
|
||||||
return wrapped
|
|
||||||
return wrapper
|
|
|
@ -1,43 +0,0 @@
|
||||||
"""
|
|
||||||
Reply to a message with .e to make a draft of it (with '\n.e' appended)
|
|
||||||
Reply to your own message with <text>.e to edit the message to <text>
|
|
||||||
if <text> is ".", the message is edited to empty/deleted
|
|
||||||
"""
|
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from telethon.errors import MessageEmptyError
|
|
||||||
from telethon.tl.functions.messages import SaveDraftRequest
|
|
||||||
from telethon.tl.functions.messages import EditMessageRequest
|
|
||||||
|
|
||||||
from stdplugins.kbass_core import self_reply_cmd
|
|
||||||
|
|
||||||
|
|
||||||
@self_reply_cmd(borg, r"^\.e$")
|
|
||||||
async def on_edit_start(event, target):
|
|
||||||
await asyncio.sleep(3) # tdesktop doesn't sync drafts when the window is active
|
|
||||||
await borg(SaveDraftRequest(
|
|
||||||
peer=await event.get_input_chat(),
|
|
||||||
message=(target.message or '.') + '\n.e',
|
|
||||||
entities=target.entities,
|
|
||||||
no_webpage=not target.media,
|
|
||||||
reply_to_msg_id=target.id
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
@self_reply_cmd(borg, r'(?ms)^(.+)\.e$')
|
|
||||||
async def on_edit_end(event, target):
|
|
||||||
text = event.pattern_match.group(1)
|
|
||||||
message = event.message.message[:-2]
|
|
||||||
if message.strip() == '.':
|
|
||||||
message = ''
|
|
||||||
try:
|
|
||||||
await borg(EditMessageRequest(
|
|
||||||
peer=await event.get_input_chat(),
|
|
||||||
id=target.id,
|
|
||||||
no_webpage=not target.media,
|
|
||||||
message=message,
|
|
||||||
entities=event.message.entities
|
|
||||||
))
|
|
||||||
except MessageEmptyError:
|
|
||||||
# Can't make text message empty, so delete it
|
|
||||||
await borg.delete_messages(chat, target)
|
|
|
@ -1,34 +0,0 @@
|
||||||
"""
|
|
||||||
Reply to a file with .f to send it as a photo
|
|
||||||
"""
|
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
from stdplugins.kbass_core import self_reply_cmd
|
|
||||||
from telethon import types
|
|
||||||
from telethon.errors import PhotoInvalidDimensionsError
|
|
||||||
|
|
||||||
|
|
||||||
@self_reply_cmd(borg, r"^\.f$")
|
|
||||||
async def on_file_to_photo(event, target):
|
|
||||||
try:
|
|
||||||
image = target.media.document
|
|
||||||
except AttributeError:
|
|
||||||
return
|
|
||||||
if not image.mime_type.startswith('image/'):
|
|
||||||
return # This isn't an image
|
|
||||||
if image.mime_type == 'image/webp':
|
|
||||||
return # Telegram doesn't let you directly send stickers as photos
|
|
||||||
if image.size > 10 * 1024 * 1024:
|
|
||||||
return # We'd get PhotoSaveFileInvalidError otherwise
|
|
||||||
|
|
||||||
file = await borg.download_media(target, file=BytesIO())
|
|
||||||
file.seek(0)
|
|
||||||
img = await borg.upload_file(file)
|
|
||||||
|
|
||||||
try:
|
|
||||||
await event.respond(
|
|
||||||
reply_to=target,
|
|
||||||
file=types.InputMediaUploadedPhoto(img)
|
|
||||||
)
|
|
||||||
except PhotoInvalidDimensionsError:
|
|
||||||
return
|
|
|
@ -1,15 +0,0 @@
|
||||||
"""
|
|
||||||
Reply to a message with .s <n> to forward <n> messages from that point to your
|
|
||||||
saved messages (negative values of <n> go backwards in history)
|
|
||||||
"""
|
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from stdplugins.kbass_core import self_reply_selector
|
|
||||||
|
|
||||||
|
|
||||||
@self_reply_selector(borg, r'\.s')
|
|
||||||
async def on_save(event, targets, num_offset):
|
|
||||||
await borg.forward_messages('me', targets)
|
|
||||||
msg = await event.respond(f'Saved {abs(num_offset) + 1} messages!')
|
|
||||||
await asyncio.sleep(3)
|
|
||||||
await borg.delete_messages(msg.to_id, msg)
|
|
|
@ -1,25 +0,0 @@
|
||||||
"""
|
|
||||||
Reply to a message with .p <y/n> to toggle the webpage preview of a message
|
|
||||||
"""
|
|
||||||
from telethon.errors import MessageNotModifiedError
|
|
||||||
from telethon.tl.functions.messages import EditMessageRequest
|
|
||||||
|
|
||||||
from stdplugins.kbass_core import self_reply_cmd
|
|
||||||
|
|
||||||
|
|
||||||
@self_reply_cmd(borg, r"^\.p(?: ?)([yn])?$")
|
|
||||||
async def on_edit_preview(event, target):
|
|
||||||
preview = event.pattern_match.group(1) == 'y'
|
|
||||||
if not event.pattern_match.group(1):
|
|
||||||
preview = not bool(target.media)
|
|
||||||
try:
|
|
||||||
await borg(EditMessageRequest(
|
|
||||||
peer=await event.get_input_chat(),
|
|
||||||
id=target.id,
|
|
||||||
no_webpage=not preview,
|
|
||||||
message=target.message,
|
|
||||||
entities=target.entities
|
|
||||||
))
|
|
||||||
except MessageNotModifiedError:
|
|
||||||
# There was no preview to modify
|
|
||||||
pass
|
|
|
@ -1,28 +0,0 @@
|
||||||
"""
|
|
||||||
Like save but makes a draft in your chat of all the messages concatenated with
|
|
||||||
two newlines
|
|
||||||
"""
|
|
||||||
import html
|
|
||||||
|
|
||||||
from telethon.tl.functions.messages import SaveDraftRequest
|
|
||||||
from telethon.extensions import html as thtml
|
|
||||||
|
|
||||||
from stdplugins.kbass_core import self_reply_selector
|
|
||||||
|
|
||||||
|
|
||||||
def get_message_html(message):
|
|
||||||
if message.action:
|
|
||||||
return html.escape(str(message.action))
|
|
||||||
return thtml.unparse(message.message, message.entities)
|
|
||||||
|
|
||||||
|
|
||||||
@self_reply_selector(borg, r'\.y')
|
|
||||||
async def on_yank(event, targets, num_offset):
|
|
||||||
message = '\n\n'.join(get_message_html(target) for target in targets)
|
|
||||||
message, entities = thtml.parse(message)
|
|
||||||
await borg(SaveDraftRequest(
|
|
||||||
peer='me',
|
|
||||||
message=message,
|
|
||||||
entities=entities,
|
|
||||||
no_webpage=True,
|
|
||||||
))
|
|
|
@ -8,7 +8,10 @@ from telethon import events
|
||||||
from telethon.tl.functions.messages import EditMessageRequest
|
from telethon.tl.functions.messages import EditMessageRequest
|
||||||
from telethon.extensions.markdown import DEFAULT_URL_RE
|
from telethon.extensions.markdown import DEFAULT_URL_RE
|
||||||
from telethon.utils import add_surrogate, del_surrogate
|
from telethon.utils import add_surrogate, del_surrogate
|
||||||
from telethon.tl.types import MessageEntityTextUrl
|
from telethon.tl.types import (
|
||||||
|
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
|
||||||
|
MessageEntityPre, MessageEntityTextUrl
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def parse_url_match(m):
|
def parse_url_match(m):
|
||||||
|
@ -20,19 +23,34 @@ def parse_url_match(m):
|
||||||
return m.group(1), entity
|
return m.group(1), entity
|
||||||
|
|
||||||
|
|
||||||
PRINTABLE_ASCII = range(0x21, 0x7f)
|
def get_tag_parser(tag, entity):
|
||||||
|
# TODO unescape escaped tags?
|
||||||
|
def tag_parser(m):
|
||||||
|
return m.group(1), entity(offset=m.start(), length=len(m.group(1)))
|
||||||
|
tag = re.escape(tag)
|
||||||
|
return re.compile(tag + r'(.+?)' + tag, re.DOTALL), tag_parser
|
||||||
|
|
||||||
|
|
||||||
def parse_aesthetics(m):
|
def parse_aesthetics(m):
|
||||||
def aesthetify(string):
|
def aesthetify(string):
|
||||||
for c in string:
|
for c in string:
|
||||||
c = ord(c)
|
if " " < c <= "~":
|
||||||
if c in PRINTABLE_ASCII:
|
yield chr(ord(c) + 0xFF00 - 0x20)
|
||||||
c += 0xFF00 - 0x20
|
elif c == " ":
|
||||||
elif c == ord(" "):
|
yield "\u3000"
|
||||||
c = 0x3000
|
else:
|
||||||
yield chr(c)
|
yield c
|
||||||
return "".join(aesthetify(m[1])), None
|
return "".join(aesthetify(m[1])), None
|
||||||
|
|
||||||
|
|
||||||
|
def parse_strikethrough(m):
|
||||||
|
return ("\u0336".join(m[1]) + "\u0336"), None
|
||||||
|
|
||||||
|
|
||||||
|
def parse_enclosing_circle(m):
|
||||||
|
return ("\u20e0".join(m[1]) + "\u20e0"), None
|
||||||
|
|
||||||
|
|
||||||
def parse_subreddit(m):
|
def parse_subreddit(m):
|
||||||
text = '/' + m.group(3)
|
text = '/' + m.group(3)
|
||||||
entity = MessageEntityTextUrl(
|
entity = MessageEntityTextUrl(
|
||||||
|
@ -54,11 +72,21 @@ def parse_snip(m):
|
||||||
return m.group(1), None
|
return m.group(1), None
|
||||||
|
|
||||||
|
|
||||||
|
PARSED_ENTITIES = (
|
||||||
|
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
|
||||||
|
MessageEntityPre, MessageEntityTextUrl
|
||||||
|
)
|
||||||
# A matcher is a tuple of (regex pattern, parse function)
|
# A matcher is a tuple of (regex pattern, parse function)
|
||||||
# where the parse function takes the match and returns (text, entity)
|
# where the parse function takes the match and returns (text, entity)
|
||||||
MATCHERS = [
|
MATCHERS = [
|
||||||
(DEFAULT_URL_RE, parse_url_match),
|
(DEFAULT_URL_RE, parse_url_match),
|
||||||
|
(get_tag_parser('**', MessageEntityBold)),
|
||||||
|
(get_tag_parser('__', MessageEntityItalic)),
|
||||||
|
(get_tag_parser('```', partial(MessageEntityPre, language=''))),
|
||||||
|
(get_tag_parser('`', MessageEntityCode)),
|
||||||
(re.compile(r'\+\+(.+?)\+\+'), parse_aesthetics),
|
(re.compile(r'\+\+(.+?)\+\+'), parse_aesthetics),
|
||||||
|
(re.compile(r'~~(.+?)~~'), parse_strikethrough),
|
||||||
|
(re.compile(r'@@(.+?)@@'), parse_enclosing_circle),
|
||||||
(re.compile(r'([^/\w]|^)(/?(r/\w+))'), parse_subreddit),
|
(re.compile(r'([^/\w]|^)(/?(r/\w+))'), parse_subreddit),
|
||||||
(re.compile(r'(!\w+)'), parse_snip)
|
(re.compile(r'(!\w+)'), parse_snip)
|
||||||
]
|
]
|
||||||
|
@ -79,6 +107,8 @@ def parse(message, old_entities=None):
|
||||||
# Skip already existing entities if we're at one
|
# Skip already existing entities if we're at one
|
||||||
if i == e.offset:
|
if i == e.offset:
|
||||||
i += e.length
|
i += e.length
|
||||||
|
else:
|
||||||
|
after += 1
|
||||||
|
|
||||||
# Find the first pattern that matches
|
# Find the first pattern that matches
|
||||||
for pattern, parser in MATCHERS:
|
for pattern, parser in MATCHERS:
|
||||||
|
|
|
@ -11,6 +11,15 @@ import telethon.utils
|
||||||
from uniborg import util
|
from uniborg import util
|
||||||
|
|
||||||
|
|
||||||
|
async def get_target_message(event):
|
||||||
|
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
|
||||||
|
return await event.get_reply_message()
|
||||||
|
async for message in borg.iter_messages(
|
||||||
|
await event.get_input_chat(), limit=20):
|
||||||
|
if message.out:
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
async def await_read(chat, message):
|
async def await_read(chat, message):
|
||||||
if isinstance(chat, InputPeerSelf):
|
if isinstance(chat, InputPeerSelf):
|
||||||
return
|
return
|
||||||
|
@ -37,7 +46,7 @@ async def delete(event):
|
||||||
text = event.pattern_match.group(2)
|
text = event.pattern_match.group(2)
|
||||||
if not text:
|
if not text:
|
||||||
return
|
return
|
||||||
target = await util.get_target_message(borg, event)
|
target = await get_target_message(event)
|
||||||
if target:
|
if target:
|
||||||
chat = await event.get_input_chat()
|
chat = await event.get_input_chat()
|
||||||
await await_read(chat, target)
|
await await_read(chat, target)
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
# 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 random
|
||||||
|
|
||||||
|
from telethon import events, types, functions, utils
|
||||||
|
|
||||||
|
|
||||||
|
def choser(cmd, pack, blacklist={}):
|
||||||
|
docs = None
|
||||||
|
@borg.on(events.NewMessage(pattern=rf'\.{cmd}', outgoing=True))
|
||||||
|
async def handler(event):
|
||||||
|
await event.delete()
|
||||||
|
|
||||||
|
nonlocal docs
|
||||||
|
if docs is None:
|
||||||
|
docs = [
|
||||||
|
utils.get_input_document(x)
|
||||||
|
for x in (await borg(functions.messages.GetStickerSetRequest(types.InputStickerSetShortName(pack)))).documents
|
||||||
|
if x.id not in blacklist
|
||||||
|
]
|
||||||
|
|
||||||
|
await event.respond(file=random.choice(docs))
|
||||||
|
|
||||||
|
|
||||||
|
choser('brain', 'supermind')
|
||||||
|
choser('dab', 'DabOnHaters', {
|
||||||
|
1653974154589768377,
|
||||||
|
1653974154589768312,
|
||||||
|
1653974154589767857,
|
||||||
|
1653974154589768311,
|
||||||
|
1653974154589767816,
|
||||||
|
1653974154589767939,
|
||||||
|
1653974154589767944,
|
||||||
|
1653974154589767912,
|
||||||
|
1653974154589767911,
|
||||||
|
1653974154589767910,
|
||||||
|
1653974154589767909,
|
||||||
|
1653974154589767863,
|
||||||
|
1653974154589767852,
|
||||||
|
1653974154589768677
|
||||||
|
})
|
|
@ -0,0 +1,112 @@
|
||||||
|
from collections import defaultdict, deque
|
||||||
|
import re
|
||||||
|
|
||||||
|
import regex
|
||||||
|
from telethon import events, utils
|
||||||
|
from telethon.tl import types, functions
|
||||||
|
|
||||||
|
from uniborg import util
|
||||||
|
|
||||||
|
HEADER = "「sed」\n"
|
||||||
|
KNOWN_RE_BOTS = re.compile(
|
||||||
|
r'(regex|moku|BananaButler_|rgx|l4mR)bot',
|
||||||
|
flags=re.IGNORECASE
|
||||||
|
)
|
||||||
|
|
||||||
|
# Heavily based on
|
||||||
|
# https://github.com/SijmenSchoon/regexbot/blob/master/regexbot.py
|
||||||
|
|
||||||
|
last_msgs = defaultdict(lambda: deque(maxlen=10))
|
||||||
|
|
||||||
|
|
||||||
|
@util.sync_timeout(1)
|
||||||
|
def doit(chat_id, match, original):
|
||||||
|
fr = match.group(1)
|
||||||
|
to = match.group(2)
|
||||||
|
to = to.replace('\\/', '/')
|
||||||
|
try:
|
||||||
|
fl = match.group(3)
|
||||||
|
if fl is None:
|
||||||
|
fl = ''
|
||||||
|
fl = fl[1:]
|
||||||
|
except IndexError:
|
||||||
|
fl = ''
|
||||||
|
|
||||||
|
# Build Python regex flags
|
||||||
|
count = 1
|
||||||
|
flags = 0
|
||||||
|
for f in fl:
|
||||||
|
if f == 'i':
|
||||||
|
flags |= regex.IGNORECASE
|
||||||
|
elif f == 'g':
|
||||||
|
count = 0
|
||||||
|
else:
|
||||||
|
return None, f"Unknown flag: {f}"
|
||||||
|
|
||||||
|
def actually_doit(original):
|
||||||
|
try:
|
||||||
|
s = original.message
|
||||||
|
if s.startswith(HEADER):
|
||||||
|
s = s[len(HEADER):]
|
||||||
|
s, i = regex.subn(fr, to, s, count=count, flags=flags)
|
||||||
|
if i > 0:
|
||||||
|
return original, s
|
||||||
|
except Exception as e:
|
||||||
|
return None, f"u dun goofed m8: {str(e)}"
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
if original is not None:
|
||||||
|
return actually_doit(original)
|
||||||
|
# Try matching the last few messages
|
||||||
|
for original in last_msgs[chat_id]:
|
||||||
|
m, s = actually_doit(original)
|
||||||
|
if s is not None:
|
||||||
|
return m, s
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
|
||||||
|
async def group_has_sedbot(group):
|
||||||
|
if isinstance(group, types.InputPeerChannel):
|
||||||
|
full = await borg(functions.channels.GetFullChannelRequest(group))
|
||||||
|
elif isinstance(group, types.InputPeerChat):
|
||||||
|
full = await borg(functions.messages.GetFullChatRequest(group.chat_id))
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return any(KNOWN_RE_BOTS.match(x.username or '') for x in full.users)
|
||||||
|
|
||||||
|
|
||||||
|
@borg.on(events.NewMessage)
|
||||||
|
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/((?:\\/|[^/])+)/((?:\\/|[^/])*)(/.*)?")))
|
||||||
|
async def on_regex(event):
|
||||||
|
if event.fwd_from:
|
||||||
|
return
|
||||||
|
if not event.is_private and\
|
||||||
|
await group_has_sedbot(await event.get_input_chat()):
|
||||||
|
return
|
||||||
|
|
||||||
|
chat_id = utils.get_peer_id(await event.get_input_chat())
|
||||||
|
|
||||||
|
m, s = doit(chat_id, event.pattern_match, await event.get_reply_message())
|
||||||
|
|
||||||
|
if m is not None:
|
||||||
|
s = f"{HEADER}{s}"
|
||||||
|
out = await borg.send_message(
|
||||||
|
await event.get_input_chat(), s, reply_to=m.id, parse_mode=None
|
||||||
|
)
|
||||||
|
last_msgs[chat_id].appendleft(out)
|
||||||
|
elif s is not None:
|
||||||
|
await event.reply(s)
|
||||||
|
|
||||||
|
raise events.StopPropagation
|
|
@ -1,9 +1,11 @@
|
||||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
# 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
|
# 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/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
import asyncio
|
||||||
from telethon import events, utils
|
from telethon import events, utils
|
||||||
from telethon.tl import types
|
from telethon.tl import types
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
TYPE_TEXT = 0
|
TYPE_TEXT = 0
|
||||||
TYPE_PHOTO = 1
|
TYPE_PHOTO = 1
|
||||||
|
@ -14,70 +16,69 @@ TYPE_DOCUMENT = 2
|
||||||
snips = storage.snips or {}
|
snips = storage.snips or {}
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'\.snip (\S+)', outgoing=True))
|
@borg.on(events.NewMessage(pattern=r'(?:\.snip +|!)(\w+)$', outgoing=True))
|
||||||
async def on_snip(event):
|
async def on_snip(event):
|
||||||
|
loop.create_task(event.delete())
|
||||||
name = event.pattern_match.group(1)
|
name = event.pattern_match.group(1)
|
||||||
if name not in snips:
|
if name not in snips:
|
||||||
await on_snip_save(event)
|
return
|
||||||
|
|
||||||
|
snip = snips[name]
|
||||||
|
if snip['type'] == TYPE_PHOTO:
|
||||||
|
media = types.InputPhoto(snip['id'], snip['hash'], file_reference=b'')
|
||||||
|
elif snip['type'] == TYPE_DOCUMENT:
|
||||||
|
media = types.InputDocument(snip['id'], snip['hash'], file_reference=b'')
|
||||||
else:
|
else:
|
||||||
snip = snips[name]
|
media = None
|
||||||
if snip['type'] == TYPE_PHOTO:
|
|
||||||
media = types.InputPhoto(snip['id'], snip['hash'])
|
|
||||||
elif snip['type'] == TYPE_DOCUMENT:
|
|
||||||
media = types.InputDocument(snip['id'], snip['hash'])
|
|
||||||
else:
|
|
||||||
media = None
|
|
||||||
|
|
||||||
await borg.send_message(await event.get_input_chat(), snip['text'],
|
await borg.send_message(await event.get_input_chat(), snip['text'],
|
||||||
file=media,
|
file=media,
|
||||||
reply_to=event.message.reply_to_msg_id)
|
reply_to=event.message.reply_to_msg_id)
|
||||||
|
|
||||||
await event.delete()
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'\.snips (\S+)', outgoing=True))
|
@borg.on(events.NewMessage(pattern=r'\.snips (\S+)', outgoing=True))
|
||||||
async def on_snip_save(event):
|
async def on_snip_save(event):
|
||||||
|
loop.create_task(event.delete())
|
||||||
name = event.pattern_match.group(1)
|
name = event.pattern_match.group(1)
|
||||||
msg = await event.get_reply_message()
|
msg = await event.get_reply_message()
|
||||||
if msg:
|
if not msg:
|
||||||
snips.pop(name, None)
|
return
|
||||||
snip = {'type': TYPE_TEXT, 'text': msg.message or ''}
|
|
||||||
if msg.media:
|
|
||||||
media = None
|
|
||||||
if isinstance(msg.media, types.MessageMediaPhoto):
|
|
||||||
media = utils.get_input_photo(msg.media.photo)
|
|
||||||
snip['type'] = TYPE_PHOTO
|
|
||||||
elif isinstance(msg.media, types.MessageMediaDocument):
|
|
||||||
media = utils.get_input_document(msg.media.document)
|
|
||||||
snip['type'] = TYPE_DOCUMENT
|
|
||||||
if media:
|
|
||||||
snip['id'] = media.id
|
|
||||||
snip['hash'] = media.access_hash
|
|
||||||
|
|
||||||
snips[name] = snip
|
snips.pop(name, None)
|
||||||
storage.snips = snips
|
snip = {'type': TYPE_TEXT, 'text': msg.message or ''}
|
||||||
|
if msg.media:
|
||||||
|
media = None
|
||||||
|
if isinstance(msg.media, types.MessageMediaPhoto):
|
||||||
|
media = utils.get_input_photo(msg.media.photo)
|
||||||
|
snip['type'] = TYPE_PHOTO
|
||||||
|
elif isinstance(msg.media, types.MessageMediaDocument):
|
||||||
|
media = utils.get_input_document(msg.media.document)
|
||||||
|
snip['type'] = TYPE_DOCUMENT
|
||||||
|
if media:
|
||||||
|
snip['id'] = media.id
|
||||||
|
snip['hash'] = media.access_hash
|
||||||
|
|
||||||
await event.delete()
|
snips[name] = snip
|
||||||
|
storage.snips = snips
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'\.snipl', outgoing=True))
|
@borg.on(events.NewMessage(pattern=r'\.snipl', outgoing=True))
|
||||||
async def on_snip_list(event):
|
async def on_snip_list(event):
|
||||||
|
loop.create_task(event.delete())
|
||||||
await event.respond('available snips: ' + ', '.join(snips.keys()))
|
await event.respond('available snips: ' + ', '.join(snips.keys()))
|
||||||
await event.delete()
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'\.snipd (\S+)', outgoing=True))
|
@borg.on(events.NewMessage(pattern=r'\.snipd (\S+)', outgoing=True))
|
||||||
async def on_snip_delete(event):
|
async def on_snip_delete(event):
|
||||||
|
loop.create_task(event.delete())
|
||||||
snips.pop(event.pattern_match.group(1), None)
|
snips.pop(event.pattern_match.group(1), None)
|
||||||
storage.snips = snips
|
storage.snips = snips
|
||||||
await event.delete()
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'\.snipr (\S+)\s+(\S+)', outgoing=True))
|
@borg.on(events.NewMessage(pattern=r'\.snipr (\S+)\s+(\S+)', outgoing=True))
|
||||||
async def on_snip_rename(event):
|
async def on_snip_rename(event):
|
||||||
|
loop.create_task(event.delete())
|
||||||
snip = snips.pop(event.pattern_match.group(1), None)
|
snip = snips.pop(event.pattern_match.group(1), None)
|
||||||
if snip:
|
if snip:
|
||||||
snips[event.pattern_match.group(2)] = snip
|
snips[event.pattern_match.group(2)] = snip
|
||||||
storage.snips = snips
|
storage.snips = snips
|
||||||
|
|
||||||
await event.delete()
|
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
import asyncio
|
|
||||||
from telethon import events
|
|
||||||
from telethon.tl import types
|
|
||||||
import re
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=re.compile(r"^bar$"), chats=1040270887))
|
|
||||||
async def on_bar(event):
|
|
||||||
if event.from_id == 151462131:
|
|
||||||
return
|
|
||||||
await event.reply("foo")
|
|
|
@ -1,71 +0,0 @@
|
||||||
import asyncio
|
|
||||||
import re
|
|
||||||
|
|
||||||
from telethon import events
|
|
||||||
from telethon.tl.functions.channels import EditTitleRequest
|
|
||||||
from telethon.errors.rpcerrorlist import ChatNotModifiedError
|
|
||||||
|
|
||||||
MULTI_EDIT_TIMEOUT = 80
|
|
||||||
REVERT_TIMEOUT = 2 * 60 * 60
|
|
||||||
CHANNEL_ID = 1040270887
|
|
||||||
DEFAULT_TITLE = "Programming & Tech"
|
|
||||||
prog_tech_channel = None
|
|
||||||
lock = asyncio.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
def fix_title(s):
|
|
||||||
# Ideally this would be a state machine, but ¯\_(ツ)_/¯
|
|
||||||
def replace(m):
|
|
||||||
token = m.group(1)
|
|
||||||
if token.lower() == 'and':
|
|
||||||
token = '&'
|
|
||||||
return token[0].upper() + token[1:] + (' ' if m.group(2) else '')
|
|
||||||
return re.sub(r'(\S+)(\s+)?', replace, s)
|
|
||||||
|
|
||||||
|
|
||||||
async def edit_title(title):
|
|
||||||
global prog_tech_channel
|
|
||||||
if prog_tech_channel is None:
|
|
||||||
prog_tech_channel = await borg.get_entity(CHANNEL_ID)
|
|
||||||
try:
|
|
||||||
await borg(EditTitleRequest(
|
|
||||||
channel=prog_tech_channel, title=title
|
|
||||||
))
|
|
||||||
except ChatNotModifiedError:
|
|
||||||
pass # Everything is ok
|
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_delete(deleted_fut, timeout):
|
|
||||||
try:
|
|
||||||
await asyncio.wait_for(deleted_fut, timeout)
|
|
||||||
await edit_title(DEFAULT_TITLE)
|
|
||||||
return True
|
|
||||||
except asyncio.TimeoutError:
|
|
||||||
pass
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(
|
|
||||||
pattern=re.compile(r"(?i)programming (?:&|and) (.+)"), chats=CHANNEL_ID))
|
|
||||||
async def on_name(event):
|
|
||||||
new_topic = fix_title(event.pattern_match.group(1))
|
|
||||||
new_title = f"Programming & {new_topic}"
|
|
||||||
if "Tech" not in new_title:
|
|
||||||
new_title += " & Tech"
|
|
||||||
|
|
||||||
if len(new_title) > 255 or lock.locked():
|
|
||||||
return
|
|
||||||
|
|
||||||
with (await lock):
|
|
||||||
await edit_title(new_title)
|
|
||||||
deleted_fut = borg.await_event(events.MessageDeleted(
|
|
||||||
chats=CHANNEL_ID,
|
|
||||||
func=lambda e: e.deleted_id == event.message.id
|
|
||||||
))
|
|
||||||
if await wait_for_delete(asyncio.shield(deleted_fut), MULTI_EDIT_TIMEOUT):
|
|
||||||
await asyncio.sleep(MULTI_EDIT_TIMEOUT)
|
|
||||||
return
|
|
||||||
if await wait_for_delete(deleted_fut, REVERT_TIMEOUT) or lock.locked():
|
|
||||||
return
|
|
||||||
with (await lock):
|
|
||||||
await edit_title(DEFAULT_TITLE)
|
|
|
@ -1,58 +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 asyncio
|
|
||||||
|
|
||||||
from telethon import events
|
|
||||||
import telethon.tl.functions as tlf
|
|
||||||
from telethon.tl.types import InputPeerChannel, UpdateUserBlocked
|
|
||||||
from telethon.tl.functions.contacts import GetBlockedRequest
|
|
||||||
|
|
||||||
|
|
||||||
# How often to fetch the full list of blocked users
|
|
||||||
REFETCH_TIME = 60
|
|
||||||
|
|
||||||
blocked_user_ids = set()
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(incoming=True, func=lambda e: e.message.mentioned))
|
|
||||||
async def on_mentioned(event):
|
|
||||||
if not event.message.from_id: # Channel messages don't have a from_id
|
|
||||||
return
|
|
||||||
if event.from_id not in blocked_user_ids:
|
|
||||||
return
|
|
||||||
|
|
||||||
peer = await borg.get_input_entity(event.chat_id)
|
|
||||||
if isinstance(peer, InputPeerChannel):
|
|
||||||
o = tlf.channels.ReadMessageContentsRequest(peer, [event.message.id])
|
|
||||||
else:
|
|
||||||
o = tlf.messages.ReadMessageContentsRequest([event.message.id])
|
|
||||||
await borg(o)
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.Raw(types=UpdateUserBlocked))
|
|
||||||
async def on_blocked(event):
|
|
||||||
if event.blocked:
|
|
||||||
blocked_user_ids.add(event.user_id)
|
|
||||||
else:
|
|
||||||
blocked_user_ids.discard(event.user_id)
|
|
||||||
|
|
||||||
|
|
||||||
async def fetch_blocked_users():
|
|
||||||
global blocked_user_ids
|
|
||||||
while 1:
|
|
||||||
offset = 0
|
|
||||||
blocked_ids = set()
|
|
||||||
while 1:
|
|
||||||
blocked = await borg(GetBlockedRequest(offset=offset, limit=100))
|
|
||||||
offset += 100
|
|
||||||
for contact in blocked.blocked:
|
|
||||||
blocked_ids.add(contact.user_id)
|
|
||||||
if not blocked.blocked:
|
|
||||||
break
|
|
||||||
blocked_user_ids = blocked_ids
|
|
||||||
await asyncio.sleep(REFETCH_TIME)
|
|
||||||
|
|
||||||
|
|
||||||
asyncio.ensure_future(fetch_blocked_users())
|
|
|
@ -1,12 +1,21 @@
|
||||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
# 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
|
# 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/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
import html
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon import utils
|
from telethon import utils
|
||||||
from telethon.tl import types
|
from telethon.tl import types
|
||||||
|
|
||||||
|
|
||||||
|
def get_who_string(who):
|
||||||
|
who_string = html.escape(utils.get_display_name(who))
|
||||||
|
if isinstance(who, (types.User, types.Channel)) and who.username:
|
||||||
|
who_string += f" <i>(@{who.username})</i>"
|
||||||
|
who_string += f", <a href='tg://user?id={who.id}'>#{who.id}</a>"
|
||||||
|
return who_string
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r"\.who", outgoing=True))
|
@borg.on(events.NewMessage(pattern=r"\.who", outgoing=True))
|
||||||
async def _(event):
|
async def _(event):
|
||||||
if not event.message.is_reply:
|
if not event.message.is_reply:
|
||||||
|
@ -14,14 +23,30 @@ async def _(event):
|
||||||
else:
|
else:
|
||||||
msg = await event.message.get_reply_message()
|
msg = await event.message.get_reply_message()
|
||||||
if msg.forward:
|
if msg.forward:
|
||||||
|
# FIXME forward privacy memes
|
||||||
who = await borg.get_entity(
|
who = await borg.get_entity(
|
||||||
msg.forward.from_id or msg.forward.channel_id)
|
msg.forward.from_id or msg.forward.channel_id)
|
||||||
else:
|
else:
|
||||||
who = await msg.get_sender()
|
who = await msg.get_sender()
|
||||||
|
|
||||||
who_string = utils.get_display_name(who)
|
await event.edit(get_who_string(who), parse_mode='html')
|
||||||
if isinstance(who, (types.User, types.Channel)) and who.username:
|
|
||||||
who_string += f" (@{who.username})"
|
|
||||||
who_string += f", #{who.id}"
|
|
||||||
|
|
||||||
await event.edit(who_string)
|
|
||||||
|
@borg.on(events.NewMessage(pattern=r"\.members", outgoing=True))
|
||||||
|
async def _(event):
|
||||||
|
members = []
|
||||||
|
async for member in borg.iter_participants(event.chat_id):
|
||||||
|
messages = await borg.get_messages(
|
||||||
|
event.chat_id,
|
||||||
|
from_user=member,
|
||||||
|
limit=0
|
||||||
|
)
|
||||||
|
members.append((
|
||||||
|
messages.total,
|
||||||
|
f"{messages.total} - {get_who_string(member)}"
|
||||||
|
))
|
||||||
|
members = (
|
||||||
|
m[1] for m in sorted(members, key=lambda m: m[0], reverse=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
await event.edit("\n".join(members), parse_mode='html')
|
||||||
|
|
|
@ -10,14 +10,14 @@ from uniborg import util
|
||||||
DELETE_TIMEOUT = 2
|
DELETE_TIMEOUT = 2
|
||||||
|
|
||||||
|
|
||||||
@borg.on(util.admin_cmd(r"^\.load (?P<shortname>\w+)$"))
|
@borg.on(util.admin_cmd(r"^\.(?:re)?load (?P<shortname>\w+)$"))
|
||||||
async def load_reload(event):
|
async def load_reload(event):
|
||||||
await event.delete()
|
await event.delete()
|
||||||
shortname = event.pattern_match["shortname"]
|
shortname = event.pattern_match["shortname"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if shortname in borg._plugins:
|
if shortname in borg._plugins:
|
||||||
borg.remove_plugin(shortname)
|
await borg.remove_plugin(shortname)
|
||||||
borg.load_plugin(shortname)
|
borg.load_plugin(shortname)
|
||||||
|
|
||||||
msg = await event.respond(
|
msg = await event.respond(
|
||||||
|
@ -39,7 +39,7 @@ async def remove(event):
|
||||||
if shortname == "_core":
|
if shortname == "_core":
|
||||||
msg = await event.respond(f"Not removing {shortname}")
|
msg = await event.respond(f"Not removing {shortname}")
|
||||||
elif shortname in borg._plugins:
|
elif shortname in borg._plugins:
|
||||||
borg.remove_plugin(shortname)
|
await borg.remove_plugin(shortname)
|
||||||
msg = await event.respond(f"Removed plugin {shortname}")
|
msg = await event.respond(f"Removed plugin {shortname}")
|
||||||
else:
|
else:
|
||||||
msg = await event.respond(f"Plugin {shortname} is not loaded")
|
msg = await event.respond(f"Plugin {shortname} is not loaded")
|
||||||
|
|
|
@ -48,6 +48,6 @@ class Storage:
|
||||||
|
|
||||||
def _save(self):
|
def _save(self):
|
||||||
if not self._root.is_dir():
|
if not self._root.is_dir():
|
||||||
self._root(parents=True, exist_ok=True)
|
self._root.mkdir(parents=True, exist_ok=True)
|
||||||
with open(self._root / FILE_NAME, 'w') as fp:
|
with open(self._root / FILE_NAME, 'w') as fp:
|
||||||
json.dump(self._data, fp)
|
json.dump(self._data, fp)
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
import asyncio
|
import asyncio
|
||||||
import importlib.util
|
import importlib.util
|
||||||
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -71,7 +72,7 @@ class Uniborg(TelegramClient):
|
||||||
self._plugins[shortname] = mod
|
self._plugins[shortname] = mod
|
||||||
self._logger.info(f"Successfully loaded plugin {shortname}")
|
self._logger.info(f"Successfully loaded plugin {shortname}")
|
||||||
|
|
||||||
def remove_plugin(self, shortname):
|
async def remove_plugin(self, shortname):
|
||||||
name = self._plugins[shortname].__name__
|
name = self._plugins[shortname].__name__
|
||||||
|
|
||||||
for i in reversed(range(len(self._event_builders))):
|
for i in reversed(range(len(self._event_builders))):
|
||||||
|
@ -79,7 +80,16 @@ class Uniborg(TelegramClient):
|
||||||
if cb.__module__ == name:
|
if cb.__module__ == name:
|
||||||
del self._event_builders[i]
|
del self._event_builders[i]
|
||||||
|
|
||||||
del self._plugins[shortname]
|
plugin = self._plugins.pop(shortname)
|
||||||
|
if callable(getattr(plugin, 'unload', None)):
|
||||||
|
try:
|
||||||
|
unload = plugin.unload()
|
||||||
|
if inspect.isawaitable(unload):
|
||||||
|
await unload
|
||||||
|
except Exception:
|
||||||
|
self._logger.exception(f'Unhandled exception unloading {shortname}')
|
||||||
|
|
||||||
|
del plugin
|
||||||
self._logger.info(f"Removed plugin {shortname}")
|
self._logger.info(f"Removed plugin {shortname}")
|
||||||
|
|
||||||
def await_event(self, event_matcher, filter=None):
|
def await_event(self, event_matcher, filter=None):
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
# 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/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
import functools
|
||||||
import re
|
import re
|
||||||
|
import signal
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon.tl.functions.messages import GetPeerDialogsRequest
|
from telethon.tl.functions.messages import GetPeerDialogsRequest
|
||||||
|
@ -29,11 +31,19 @@ async def is_read(borg, entity, message, is_out=None):
|
||||||
max_id = dialog.read_outbox_max_id if is_out else dialog.read_inbox_max_id
|
max_id = dialog.read_outbox_max_id if is_out else dialog.read_inbox_max_id
|
||||||
return message_id <= max_id
|
return message_id <= max_id
|
||||||
|
|
||||||
|
def _handle_timeout(signum, frame):
|
||||||
|
raise TimeoutError("Execution took too long")
|
||||||
|
|
||||||
async def get_target_message(borg, event):
|
def sync_timeout(seconds):
|
||||||
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
|
def decorator(func):
|
||||||
return await event.get_reply_message()
|
@functools.wraps(func)
|
||||||
async for message in borg.iter_messages(
|
def wrapper(*args, **kwargs):
|
||||||
await event.get_input_chat(), limit=20):
|
signal.signal(signal.SIGALRM, _handle_timeout)
|
||||||
if message.out:
|
signal.setitimer(signal.ITIMER_REAL, seconds)
|
||||||
return message
|
try:
|
||||||
|
r = func(*args, **kwargs)
|
||||||
|
finally:
|
||||||
|
signal.alarm(0)
|
||||||
|
return r
|
||||||
|
return wrapper
|
||||||
|
return decorator
|
||||||
|
|
Loading…
Reference in New Issue