mirror of
https://github.com/Nick80835/microbot
synced 2025-09-03 16:05:49 +00:00
add inline sauce links
This commit is contained in:
@@ -5,6 +5,8 @@ from re import escape, search
|
|||||||
|
|
||||||
from telethon import events, types
|
from telethon import events, types
|
||||||
|
|
||||||
|
from .fixes import inline_photos
|
||||||
|
|
||||||
|
|
||||||
class CommandHandler():
|
class CommandHandler():
|
||||||
def __init__(self, client, logger, settings):
|
def __init__(self, client, logger, settings):
|
||||||
@@ -85,16 +87,19 @@ class CommandHandler():
|
|||||||
event.pattern_match = pattern_match
|
event.pattern_match = pattern_match
|
||||||
event.args = pattern_match.groups()[-1]
|
event.args = pattern_match.groups()[-1]
|
||||||
|
|
||||||
url_list = await value["function"](event)
|
photo_list = await value["function"](event)
|
||||||
|
|
||||||
if not url_list:
|
if not photo_list:
|
||||||
return
|
return
|
||||||
|
|
||||||
photo_coros = []
|
photo_coros = []
|
||||||
|
|
||||||
for url in url_list:
|
for photo in photo_list:
|
||||||
try:
|
try:
|
||||||
photo_coros += [self.try_coro(builder.photo(url))]
|
if isinstance(photo, list):
|
||||||
|
photo_coros += [self.try_coro(inline_photos.photo(event.client, photo[0], text=photo[1]))]
|
||||||
|
else:
|
||||||
|
photo_coros += [self.try_coro(builder.photo(photo))]
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
37
ubot/fixes/inline_photos.py
Normal file
37
ubot/fixes/inline_photos.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import hashlib
|
||||||
|
|
||||||
|
from telethon import Button, utils
|
||||||
|
from telethon.tl import functions, types
|
||||||
|
|
||||||
|
|
||||||
|
async def photo(client, file, text='', parse_mode=None, buttons=None):
|
||||||
|
try:
|
||||||
|
fh = utils.get_input_photo(file)
|
||||||
|
except TypeError:
|
||||||
|
_, media, _ = await client._file_to_media(file, allow_cache=True, as_image=True)
|
||||||
|
|
||||||
|
if isinstance(media, types.InputPhoto):
|
||||||
|
fh = media
|
||||||
|
else:
|
||||||
|
r = await client(functions.messages.UploadMediaRequest(types.InputPeerSelf(), media=media))
|
||||||
|
fh = utils.get_input_photo(r.photo)
|
||||||
|
|
||||||
|
markup = None
|
||||||
|
msg_entities = None
|
||||||
|
|
||||||
|
if buttons:
|
||||||
|
markup = client.build_reply_markup(buttons, inline_only=True)
|
||||||
|
|
||||||
|
if text:
|
||||||
|
text, msg_entities = await client._parse_message_text(text, parse_mode or client.parse_mode)
|
||||||
|
|
||||||
|
result = types.InputBotInlineResultPhoto(
|
||||||
|
id='',
|
||||||
|
type='photo',
|
||||||
|
photo=fh,
|
||||||
|
send_message=types.InputBotInlineMessageMediaAuto(text, entities=msg_entities, reply_markup=markup)
|
||||||
|
)
|
||||||
|
|
||||||
|
result.id = hashlib.sha256(bytes(result)).hexdigest()
|
||||||
|
|
||||||
|
return result
|
@@ -99,7 +99,7 @@ async def danbooru_inline(event):
|
|||||||
|
|
||||||
for post in response:
|
for post in response:
|
||||||
if 'file_url' in post.keys():
|
if 'file_url' in post.keys():
|
||||||
valid_urls.append(post['file_url'])
|
valid_urls.append([post['file_url'], f"[sauce]({DAN_SAUCE_URL}{post['id']})"])
|
||||||
|
|
||||||
if not valid_urls:
|
if not valid_urls:
|
||||||
return None
|
return None
|
||||||
|
@@ -107,7 +107,7 @@ async def gelbooru_inline(event):
|
|||||||
|
|
||||||
for post in response:
|
for post in response:
|
||||||
if 'file_url' in post.keys():
|
if 'file_url' in post.keys():
|
||||||
valid_urls.append(post['file_url'])
|
valid_urls.append([post['file_url'], f"[sauce]({GEL_SAUCE_URL}{post['id']})"])
|
||||||
|
|
||||||
if not valid_urls:
|
if not valid_urls:
|
||||||
return None
|
return None
|
||||||
|
@@ -100,7 +100,7 @@ async def sankaku_inline(event):
|
|||||||
|
|
||||||
for post in response:
|
for post in response:
|
||||||
if 'file_url' in post.keys():
|
if 'file_url' in post.keys():
|
||||||
valid_urls.append(post['file_url'])
|
valid_urls.append([post['file_url'], f"[sauce]({SAN_SAUCE_URL}{post['id']})"])
|
||||||
|
|
||||||
if not valid_urls:
|
if not valid_urls:
|
||||||
return None
|
return None
|
||||||
|
Reference in New Issue
Block a user