2018-04-23 11:57:52 +00:00
|
|
|
# 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, utils
|
2018-06-10 17:12:53 +00:00
|
|
|
from telethon.tl import types
|
2018-04-23 11:57:52 +00:00
|
|
|
|
2018-04-23 13:58:32 +00:00
|
|
|
TYPE_TEXT = 0
|
|
|
|
TYPE_PHOTO = 1
|
|
|
|
TYPE_DOCUMENT = 2
|
2018-04-23 11:57:52 +00:00
|
|
|
|
|
|
|
|
2018-04-23 13:58:32 +00:00
|
|
|
# {name: {'text': text, 'id': id, 'hash': access_hash, 'type': type}}
|
|
|
|
snips = storage.snips or {}
|
2018-04-23 11:57:52 +00:00
|
|
|
|
|
|
|
|
2018-06-14 18:03:14 +00:00
|
|
|
@borg.on(events.NewMessage(pattern=r'\.snip (\S+)', outgoing=True))
|
2018-04-23 12:09:46 +00:00
|
|
|
async def on_snip(event):
|
2019-01-17 09:33:13 +00:00
|
|
|
await event.delete()
|
2018-04-23 11:57:52 +00:00
|
|
|
name = event.pattern_match.group(1)
|
2018-04-23 14:17:31 +00:00
|
|
|
if name not in snips:
|
2018-12-28 21:27:23 +00:00
|
|
|
return
|
2018-04-23 14:10:32 +00:00
|
|
|
|
2018-12-28 21:27:23 +00:00
|
|
|
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:
|
|
|
|
media = None
|
2018-04-23 14:10:32 +00:00
|
|
|
|
2018-12-28 21:27:23 +00:00
|
|
|
await borg.send_message(await event.get_input_chat(), snip['text'],
|
|
|
|
file=media,
|
|
|
|
reply_to=event.message.reply_to_msg_id)
|
2018-04-23 14:10:32 +00:00
|
|
|
|
|
|
|
|
2018-06-14 18:03:14 +00:00
|
|
|
@borg.on(events.NewMessage(pattern=r'\.snips (\S+)', outgoing=True))
|
2018-04-23 14:10:32 +00:00
|
|
|
async def on_snip_save(event):
|
2019-01-17 09:33:13 +00:00
|
|
|
await event.delete()
|
2018-04-23 14:10:32 +00:00
|
|
|
name = event.pattern_match.group(1)
|
2018-06-22 08:46:39 +00:00
|
|
|
msg = await event.get_reply_message()
|
2018-12-28 21:27:23 +00:00
|
|
|
if not msg:
|
|
|
|
return
|
|
|
|
|
|
|
|
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
|
2018-04-23 12:09:46 +00:00
|
|
|
|
|
|
|
|
2018-06-14 18:03:14 +00:00
|
|
|
@borg.on(events.NewMessage(pattern=r'\.snipl', outgoing=True))
|
2018-04-23 12:09:46 +00:00
|
|
|
async def on_snip_list(event):
|
2019-01-17 09:33:13 +00:00
|
|
|
await event.delete()
|
2018-04-23 12:09:46 +00:00
|
|
|
await event.respond('available snips: ' + ', '.join(snips.keys()))
|
|
|
|
|
|
|
|
|
2018-06-14 18:03:14 +00:00
|
|
|
@borg.on(events.NewMessage(pattern=r'\.snipd (\S+)', outgoing=True))
|
2018-04-23 12:09:46 +00:00
|
|
|
async def on_snip_delete(event):
|
2019-01-17 09:33:13 +00:00
|
|
|
await event.delete()
|
2018-04-23 13:58:32 +00:00
|
|
|
snips.pop(event.pattern_match.group(1), None)
|
2018-06-01 00:29:51 +00:00
|
|
|
storage.snips = snips
|
2018-04-23 14:14:08 +00:00
|
|
|
|
|
|
|
|
2018-06-14 18:03:14 +00:00
|
|
|
@borg.on(events.NewMessage(pattern=r'\.snipr (\S+)\s+(\S+)', outgoing=True))
|
2018-04-23 14:14:08 +00:00
|
|
|
async def on_snip_rename(event):
|
2019-01-17 09:33:13 +00:00
|
|
|
await event.delete()
|
2018-04-23 14:14:08 +00:00
|
|
|
snip = snips.pop(event.pattern_match.group(1), None)
|
|
|
|
if snip:
|
|
|
|
snips[event.pattern_match.group(2)] = snip
|
|
|
|
storage.snips = snips
|