(myenv) root@xuquo7ti:~# python3 main.py
PyTgCalls v2.0.6 powered by NTgCalls v1.2.3, Copyright (C) 2021-2024 Laky-64 https://github.com/Laky-64
Licensed under the terms of the GNU Lesser General Public License v3 or later (LGPLv3+)
Telegram says: [400 MESSAGE_TOO_LONG] - The message text is too long (caused by "messages.EditMessage")
Traceback (most recent call last):
File "/root/myenv/lib/python3.12/site-packages/pyrogram/client.py", line 920, in get_file
await func()
File "/root/main.py", line 57, in progress_callback
await msg.edit(f"Downloading video: {current:.2f}% completed.\n{'#'*level_downloaded if level_downloaded != 0 else ''}{'.'*level_not_downloaded if level_not_downloaded != 0 else ''}")
File "/root/myenv/lib/python3.12/site-packages/pyrogram/types/messages_and_media/message.py", line 2803, in edit_text
return await self._client.edit_message_text(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/methods/messages/edit_message_text.py", line 82, in edit_message_text
r = await self.invoke(
^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
r = await self.session.invoke(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/session/session.py", line 389, in invoke
return await self.send(query, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/session/session.py", line 357, in send
RPCError.raise_it(result, type(data))
File "/root/myenv/lib/python3.12/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
raise getattr(
pyrogram.errors.exceptions.bad_request_400.MessageTooLong: Telegram says: [400 MESSAGE_TOO_LONG] - The message text is too long (caused by "messages.EditMessage")
کد من اینه
from pyrogram import Client # pip install pyrogram
from pyrogram import filters # pip install tgcrypto
from pyrogram.types import Message
from pytgcalls import filters as fl # pip install py-tgcalls -U
from pytgcalls import idle
from pytgcalls import PyTgCalls
from pytgcalls.types import ChatUpdate
from pytgcalls.types import GroupCallParticipant
from pytgcalls.types import MediaStream
from pytgcalls.types import Update
import os
import re
import subprocess
import random
import string
from time import sleep
def generate_random_string(length=10):
# ترکیب حروف کوچک، بزرگ و اعداد
characters = string.ascii_letters + string.digits
# ایجاد یک رشته رندوم از ترکیب حروف و اعداد
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
Assistant
خطای 400 MESSAGE_TOO_LONG که دریافت کردهاید به این دلیل است که متن پیامی که میخواهید ویرایش کنید، طولانیتر از حد مجاز تلگرام است. تلگرام محدودیتهایی برای طول پیامها دارد و اگر متن شما از این حد فراتر برود، این خطا ایجاد میشود.
برای حل این مشکل، میتوانید متن را به بخشهای کوچکتر تقسیم کنید و چندین بار آن را ویرایش کنید یا فقط دو بار در هر مرحله پیشرفت را بهروزرسانی کنید. به عنوان مثال، شما میتوانید بیانیه await msg.edit(...) را طوری تنظیم کنید که تنها درصدهای خاصی را ویرایش کند، مانند:
async def progress_callback(current, total, msg):
global perv_perc
if current != perv_perc and current > perv_perc:
level_downloaded = int(current // 5)
level_not_downloaded = 20 - level_downloaded
progress_text = f"Downloading video: {current:.2f}% completed.\n{'#' * level_downloaded if level_downloaded != 0 else ''}{'.' * level_not_downloaded if level_not_downloaded != 0 else ''}"
# اگر متن قبلی برابر با متن جدید باشد، ویرایش نکنید
if progress_text != msg.text:
await msg.edit(progress_text)
perv_perc = current
User
File "/root/myenv/lib/python3.12/site-packages/pyrogram/session/session.py", line 357, in send
RPCError.raise_it(result, type(data))
File "/root/myenv/lib/python3.12/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
raise getattr(
pyrogram.errors.exceptions.bad_request_400.MessageTooLong: Telegram says: [400 MESSAGE_TOO_LONG] - The message text is too long (caused by "messages.EditMessage")
'NoneType' object has no attribute 'write'
Traceback (most recent call last):
File "/root/myenv/lib/python3.12/site-packages/pyrogram/dispatcher.py", line 240, in handler_worker
await handler.callback(self.client, *args)
File "/root/main.py", line 119, in handle_link
await client.send_video(update.chat.id, video_file, progress=progress_callback_upload, progress_args=(progress_msg, video_file))
File "/root/myenv/lib/python3.12/site-packages/pyrogram/methods/messages/send_video.py", line 234, in send_video
r = await self.invoke(
^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
r = await self.session.invoke(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/session/session.py", line 389, in invoke
return await self.send(query, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/session/session.py", line 318, in send
message = self.msg_factory(data)
^^^^^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/session/internals/msg_factory.py", line 37, in
len(body)
File "/root/myenv/lib/python3.12/site-packages/pyrogram/raw/core/tl_object.py", line 79, in
return len(self.write())
^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/raw/functions/messages/send_media.py", line 167, in write
b.write(self.media.write())
^^^^^^^^^^^^^^^^^^
File "/root/myenv/lib/python3.12/site-packages/pyrogram/raw/types/input_media_uploaded_document.py", line 122, in write
b.write(self.file.write())
^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'write'
Assistant
خطای AttributeError: 'NoneType' object has no attribute 'write' نشان میدهد که شما در تلاش هستید به شیء None دسترسی پیدا کنید. به عبارت دیگر، تابع یا متغیری که انتظار دارید یک شیء معتبر (مانند یک فایل یا رسانه) باشد، در واقع مقدار None را برمیگرداند. در این مورد به نظر میرسد که مشکل بههنگام ارسال ویدیو ایجاد شده است.
به طور خاص، این خطا معمولاً وقتی اتفاق میافتد که فایل ویدیویی که میخواهید ارسال کنید، نتوانسته به درستی دانلود شود و در نهایت متغیر مقدار را پیدا میکند.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
async def progress_callback(current, total, msg):
global perv_perc
if current != perv_perc and current > perv_perc:
level_downloaded = int(current // 5)
level_not_downloaded = 20 - level_downloaded
await msg.edit(f"Downloading video: {current:.2f}% completed.\n{'#'*level_downloaded if level_downloaded != 0 else ''}{'.'*level_not_downloaded if level_not_downloaded != 0 else ''}")
perv_perc = current
async def download_video(url: str, update_msg: Message) -> str:
output_file = f'video_{generate_random_string()}.mp4' # نام فایل خروجی با زمان منحصر به فرد
try:
# فراخوانی yt-dlp
process = subprocess.Popen(
['yt-dlp', '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]', '-o', output_file, url],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
current_size = 0
total_size = 0
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
# درصد دانلود را از خروجی استخراج کنید
if re.search(r"download",output) and re.search(r'\d+.\d+%', output):
percent_ = re.findall(r'\d+.\d+%',output)[0].replace("%","")
current_size = float(percent_)
# با استفاده از تابع progress_callback درصد را به کاربر ارسال کنید
await progress_callback(current_size, 100, update_msg) # فرض بر اینکه دانلود 100 درصدی است
sleep(0.5)
# بررسی خطا
stderr_output = process.stderr.read()
if stderr_output:
print("Error:", stderr_output)
return output_file
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
return None
if video_file and os.path.exists(video_file):
await client.send_video(update.chat.id, video_file)
os.remove(video_file) # حذف فایل بعد از ارسال
else:
await update.reply_text('خطا در دانلود ویدیو.')
@app.on_message(filters.regex('/help'))
async def play_handler(_: Client, message: Message):
files = os.listdir("./downloads")
song_name = "لیست اهنگ ها به شرح زیر است\n"
film_name = "لیست فیلم ها به شرح زیر است\n"
for name in files:
if re.search(r"mp3",name):
song_name += name.replace(".mp3","") + "\n"
if re.search(r"mkv",name):
film_name += name.replace(".mkv","") + "\n"
text = """شما با فعال کردن تماس گروه میتونین از دستور زیر استفاده کرده تا اهنگ مد نظر رو اجرا یا کنترل کنین همچنین در انتهای لیست نام اهنگ های فعلی موجود ذکر شده
/play برای اجرای اهنگ مد نظر
به طور مثال
/play Qrargah
/stop برای متوقف کردن
/pause برای استپ کردن
/resume اجرای ادامه
/change_volume برای تغییر ولوم
به طور مثال
/change_volume 50
""" + song_name + film_name
await app.send_message(message.chat.id, text)
@app.on_message(filters.regex('/del'))
async def play_handler(_: Client, message: Message):
text = message.text
if re.search(r"film",text):
file_name = text.replace("/del film ","")+".mkv"
elif re.search(r"music",text):
file_name = text.replace("/del music ","")+".mp4"
os.remove(f"./downloads/{file_name}")
await app.send_message(message.chat.id, f"remove successfull {file_name}")
@app.on_message(filters.regex('/play'))
async def play_handler(_: Client, message: Message):
text = message.text
if re.search(r"music",text):
song_name = text.replace("/play music ","")
test_stream = f'./downloads/{song_name}.mp3'
if re.search(r"film",text):
film_name = text.replace("/play film ","")
test_stream = f'./downloads/{film_name}.mkv'
await call_py.play(
message.chat.id,
MediaStream(
test_stream,
),
)
await app.send_message(message.chat.id, f"Bot Play {song_name}")
این تغییر از ویرایش دوباره همان متن جلوگیری میکند، که میتواند به کاهش تعداد ویرایشها و در نتیجه جلوگیری از خطای طول پیام کمک کند.
بهطور کلی، سعی کنید از ارسال متنهای طولانی و اضافی پرهیز کنید و در صورت امکان آنها را شکسته و منعطف تر کنید.
call
len
video_file
None
برای حل این مشکل، میتوانید اقداماتی را انجام دهید:
بررسی دانلود ویدیو: اطمینان حاصل کنید که ویدیو به درستی دانلود شده است و فایل وجود دارد قبل از اینکه بخواهید آن را ارسال کنید. میتوانید این کار را با استفاده از یک شرط ساده انجام دهید:
if video_file and os.path.exists(video_file):
await client.send_video(update.chat.id, video_file, progress=progress_callback_upload, progress_args=(progress_msg, video_file))
else:
await update.reply_text('خطا در دانلود ویدیو.')
بررسی خروجی دانلود: در تابع download_video، مطمئن شوید که در صورت بروز خطا در فرآیند دانلود، مقدار بازگشتی به درستی مقداردهی میشود. پیش از این برای بررسی stderr استفاده کردهاید، اما مطمئن شوید که خروجی ویدیو در پایان به درستی تعیین شده است:
if process.returncode != 0:
print("Error:", stderr_output)
return None # اطمینان حاصل کنید که در صورت بروز خطا None برگردانده میشود
اضافه کردن لاگینگ: میتوانید اطلاعات بیشتری را چاپ کنید تا ببینید چه اتفاقی میافتد. به عنوان مثال، در download_video میتوانید مقدار output_file و وضعیت دانلود را برای اشکالزدایی چک کنید.
بررسی توابع ناهمزمان: اطمینان حاصل کنید که توابع شما به درستی بهصورت ناهمزمان فراخوانی میشوند و به طور صحیح به جریان کار ادامه میدهند. بهروزرسانی ویدیو باید به صورت همزمان با وضعیت دانلود انجام شود.
با این تغییرات، زمان بیشتری برای شناسایی و اشکالزدایی این نوع خطا خواهید داشت و میتوانید از بروز خطاهای مشابه جلوگیری کنید.