uniborg/stdplugins/snip.py

66 lines
2.0 KiB
Python

# 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 os
import sys
import uuid
from telethon import events, utils
from telethon.tl import types, functions
TYPE_TEXT = 0
TYPE_PHOTO = 1
TYPE_DOCUMENT = 2
# {name: {'text': text, 'id': id, 'hash': access_hash, 'type': type}}
snips = storage.snips or {}
@borg.on(events.NewMessage(pattern=r'.snip (\w+)'))
async def on_snip(event):
msg = await event.reply_message
name = event.pattern_match.group(1)
if msg:
snips.pop(name, None)
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
storage.snips = snips
elif name in snips:
snip = snips[name]
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.input_chat, snip['text'], file=media)
await event.delete()
@borg.on(events.NewMessage(pattern=r'.snipl'))
async def on_snip_list(event):
await event.respond('available snips: ' + ', '.join(snips.keys()))
await event.delete()
@borg.on(events.NewMessage(pattern=r'.snipd (\w+)'))
async def on_snip_delete(event):
snips.pop(event.pattern_match.group(1), None)
await event.delete()