Spaces:
Runtime error
Runtime error
File size: 21,166 Bytes
dfdbd3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# Moon-Userbot - telegram userbot
# Copyright (C) 2020-present Moon Userbot Organization
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import base64
from io import BytesIO
import requests
from pyrogram import Client, filters, errors, types
from pyrogram.types import Message
from utils.misc import modules_help, prefix
from utils.scripts import with_reply, format_exc, resize_image
@Client.on_message(filters.command(["q", "quote"], prefix) & filters.me)
@with_reply
async def quote_cmd(client: Client, message: Message):
if len(message.command) > 1 and message.command[1].isdigit():
count = int(message.command[1])
if count < 1:
count = 1
elif count > 15:
count = 15
else:
count = 1
is_png = "!png" in message.command or "!file" in message.command
send_for_me = "!me" in message.command or "!ls" in message.command
no_reply = "!noreply" in message.command or "!nr" in message.command
messages = []
async for msg in client.get_chat_history(
message.chat.id,
offset_id=message.reply_to_message.id + count,
limit=count,
):
if msg.empty:
continue
if msg.id >= message.id:
break
if no_reply:
msg.reply_to_message = None
messages.append(msg)
if len(messages) >= count:
break
messages.reverse()
if send_for_me:
await message.delete()
message = await client.send_message("me", "<b>Generating...</b>")
else:
await message.edit("<b>Generating...</b>")
url = "https://quotes.fl1yd.su/generate"
params = {
"messages": [
await render_message(client, msg) for msg in messages if not msg.empty
],
"quote_color": "#162330",
"text_color": "#fff",
}
response = requests.post(url, json=params)
if not response.ok:
return await message.edit(
f"<b>Quotes API error!</b>\n<code>{response.text}</code>"
)
resized = resize_image(
BytesIO(response.content), img_type="PNG" if is_png else "WEBP"
)
await message.edit("<b>Sending...</b>")
try:
func = client.send_document if is_png else client.send_sticker
chat_id = "me" if send_for_me else message.chat.id
await func(chat_id, resized)
except errors.RPCError as e: # no rights to send stickers, etc
await message.edit(format_exc(e))
else:
await message.delete()
@Client.on_message(filters.command(["fq", "fakequote"], prefix) & filters.me)
@with_reply
async def fake_quote_cmd(client: Client, message: types.Message):
is_png = "!png" in message.command or "!file" in message.command
send_for_me = "!me" in message.command or "!ls" in message.command
no_reply = "!noreply" in message.command or "!nr" in message.command
fake_quote_text = " ".join(
[
arg
for arg in message.command[1:]
if arg not in ["!png", "!file", "!me", "!ls", "!noreply", "!nr"]
] # remove some special arg words
)
if not fake_quote_text:
return await message.edit("<b>Fake quote text is empty</b>")
q_message = await client.get_messages(message.chat.id, message.reply_to_message.id)
q_message.text = fake_quote_text
q_message.entities = None
if no_reply:
q_message.reply_to_message = None
if send_for_me:
await message.delete()
message = await client.send_message("me", "<b>Generating...</b>")
else:
await message.edit("<b>Generating...</b>")
url = "https://quotes.fl1yd.su/generate"
params = {
"messages": [await render_message(client, q_message)],
"quote_color": "#162330",
"text_color": "#fff",
}
response = requests.post(url, json=params)
if not response.ok:
return await message.edit(
f"<b>Quotes API error!</b>\n<code>{response.text}</code>"
)
resized = resize_image(
BytesIO(response.content), img_type="PNG" if is_png else "WEBP"
)
await message.edit("<b>Sending...</b>")
try:
func = client.send_document if is_png else client.send_sticker
chat_id = "me" if send_for_me else message.chat.id
await func(chat_id, resized)
except errors.RPCError as e: # no rights to send stickers, etc
await message.edit(format_exc(e))
else:
await message.delete()
files_cache = {}
async def render_message(app: Client, message: types.Message) -> dict:
async def get_file(file_id) -> str:
if file_id in files_cache:
return files_cache[file_id]
content = await app.download_media(file_id, in_memory=True)
data = base64.b64encode(bytes(content.getbuffer())).decode()
files_cache[file_id] = data
return data
# text
if message.photo:
text = message.caption if message.caption else ""
elif message.poll:
text = get_poll_text(message.poll)
elif message.sticker:
text = ""
else:
text = get_reply_text(message)
# media
if message.photo:
media = await get_file(message.photo.file_id)
elif message.sticker:
media = await get_file(message.sticker.file_id)
else:
media = ""
# entities
entities = []
if message.entities:
for entity in message.entities:
entities.append(
{
"offset": entity.offset,
"length": entity.length,
"type": str(entity.type).split(".")[-1].lower(),
}
)
def move_forwards(msg: types.Message):
if msg.forward_origin:
if isinstance(msg.forward_origin, types.MessageOriginUser):
msg.from_user = msg.forward_origin.sender_user
elif isinstance(msg.forward_origin, types.MessageOriginHiddenUser):
msg.from_user.id = 0
msg.from_user.first_name = msg.forward_origin.sender_user_name
msg.from_user.last_name = ""
elif isinstance(msg.forward_origin, types.MessageOriginChat):
msg.sender_chat = msg.forward_origin.sender_chat
msg.from_user.id = 0
if msg.forward_origin.author_signature:
msg.author_signature = msg.forward_origin.author_signature
move_forwards(message)
# author
author = {}
if message.from_user and message.from_user.id != 0:
from_user = message.from_user
author["id"] = from_user.id
author["name"] = get_full_name(from_user)
if message.author_signature:
author["rank"] = message.author_signature
elif message.chat.type != "supergroup" or message.forward_date:
author["rank"] = ""
else:
try:
member = await message.chat.get_member(from_user.id)
except errors.UserNotParticipant:
author["rank"] = ""
else:
author["rank"] = getattr(member, "title", "") or (
"owner"
if member.status == "creator"
else "admin"
if member.status == "administrator"
else ""
)
if from_user.photo:
author["avatar"] = await get_file(from_user.photo.big_file_id)
elif not from_user.photo and from_user.username:
# may be user blocked us, we will try to get avatar via t.me
t_me_page = requests.get(f"https://t.me/{from_user.username}").text
sub = '<meta property="og:image" content='
index = t_me_page.find(sub)
if index != -1:
link = t_me_page[index + 35 :].split('"')
if (
len(link) > 0
and link[0]
and link[0] != "https://telegram.org/img/t_logo.png"
):
# found valid link
avatar = requests.get(link[0]).content
author["avatar"] = base64.b64encode(avatar).decode()
else:
author["avatar"] = ""
else:
author["avatar"] = ""
else:
author["avatar"] = ""
elif message.from_user and message.from_user.id == 0:
author["id"] = 0
author["name"] = message.from_user.first_name
author["rank"] = ""
else:
author["id"] = message.sender_chat.id
author["name"] = message.sender_chat.title
author["rank"] = "channel" if message.sender_chat.type == "channel" else ""
if message.sender_chat.photo:
author["avatar"] = await get_file(message.sender_chat.photo.big_file_id)
else:
author["avatar"] = ""
author["via_bot"] = message.via_bot.username if message.via_bot else ""
# reply
reply = {}
reply_msg = message.reply_to_message
if reply_msg and not reply_msg.empty:
move_forwards(reply_msg)
if reply_msg.from_user:
reply["id"] = reply_msg.from_user.id
reply["name"] = get_full_name(reply_msg.from_user)
else:
reply["id"] = reply_msg.sender_chat.id
reply["name"] = reply_msg.sender_chat.title
reply["text"] = get_reply_text(reply_msg)
return {
"text": text,
"media": media,
"entities": entities,
"author": author,
"reply": reply,
}
def get_audio_text(audio: types.Audio) -> str:
if audio.title and audio.performer:
return f" ({audio.title} โ {audio.performer})"
if audio.title:
return f" ({audio.title})"
if audio.performer:
return f" ({audio.performer})"
return ""
def get_reply_text(reply: types.Message) -> str:
return (
"๐ท Photo" + ("\n" + reply.caption if reply.caption else "")
if reply.photo
else (
get_reply_poll_text(reply.poll)
if reply.poll
else (
"๐ Location"
if reply.location or reply.venue
else (
"๐ค Contact"
if reply.contact
else (
"๐ผ GIF"
if reply.animation
else (
"๐ง Music" + get_audio_text(reply.audio)
if reply.audio
else (
"๐น Video"
if reply.video
else (
"๐น Videomessage"
if reply.video_note
else (
"๐ต Voice"
if reply.voice
else (
(
reply.sticker.emoji + " "
if reply.sticker.emoji
else ""
)
+ "Sticker"
if reply.sticker
else (
"๐พ File " + reply.document.file_name
if reply.document
else (
"๐ฎ Game"
if reply.game
else (
"๐ฎ set new record"
if reply.game_high_score
else (
f"{reply.dice.emoji} - {reply.dice.value}"
if reply.dice
else (
(
"๐ค joined the group"
if reply.new_chat_members[
0
].id
== reply.from_user.id
else "๐ค invited %s to the group"
% (
get_full_name(
reply.new_chat_members[
0
]
)
)
)
if reply.new_chat_members
else (
(
"๐ค left the group"
if reply.left_chat_member.id
== reply.from_user.id
else "๐ค removed %s"
% (
get_full_name(
reply.left_chat_member
)
)
)
if reply.left_chat_member
else (
f"โ changed group name to {reply.new_chat_title}"
if reply.new_chat_title
else (
"๐ผ changed group photo"
if reply.new_chat_photo
else (
"๐ผ removed group photo"
if reply.delete_chat_photo
else (
"๐ pinned message"
if reply.pinned_message
else (
"๐ค started a new video chat"
if reply.video_chat_started
else (
"๐ค ended the video chat"
if reply.video_chat_ended
else (
"๐ค invited participants to the video chat"
if reply.video_chat_members_invited
else (
"๐ฅ created the group"
if reply.group_chat_created
or reply.supergroup_chat_created
else (
"๐ฅ created the channel"
if reply.channel_chat_created
else reply.text
or "unsupported message"
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
def get_poll_text(poll: types.Poll) -> str:
text = get_reply_poll_text(poll) + "\n"
text += poll.question + "\n"
for option in poll.options:
text += f"- {option.text}"
if option.voter_count > 0:
text += f" ({option.voter_count} voted)"
text += "\n"
text += f"Total: {poll.total_voter_count} voted"
return text
def get_reply_poll_text(poll: types.Poll) -> str:
if poll.is_anonymous:
text = "๐ Anonymous poll" if poll.type == "regular" else "๐ Anonymous quiz"
else:
text = "๐ Poll" if poll.type == "regular" else "๐ Quiz"
if poll.is_closed:
text += " (closed)"
return text
def get_full_name(user: types.User) -> str:
name = user.first_name
if user.last_name:
name += " " + user.last_name
return name
modules_help["squotes"] = {
"q [reply]* [count 1-15] [!png] [!me] [!noreply]": "Generate a quote\n"
"Available options: !png โ send as PNG, !me โ send quote to"
"saved messages, !noreply โ generate quote without reply",
"fq [reply]* [!png] [!me] [!noreply] [text]*": "Generate a fake quote",
}
|