Rubka | مستندات کلاس Message

کلاس Message

مدل اصلی پیام در کتابخانه روبکا - دسترسی به اطلاعات پیام، ارسال پاسخ، ویرایش، حذف و کپی

Message v2.0
سازنده کلاس (init)
__init__
ساخت یک شیء پیام با اطلاعات دریافتی از سرور
پارامترنوعتوضیحات
botRobotنمونه ربات
chat_idstrشناسه چت
message_idstrشناسه پیام
sender_idstrشناسه فرستنده
textstr | Noneمتن پیام
raw_datadict | Noneداده خام از سرور
from rubka import Robot, Message

bot = Robot("TOKEN")

@bot.on_message()
async def handler(bot: Robot, message: Message):
    print(message.chat_id)
    print(message.message_id)
    print(message.text)
فیلدهای کلاس
فیلدهای اصلی
دسترسی به اطلاعات پایه پیام
فیلدنوعتوضیحات
chat_idstrشناسه چت
message_idstrشناسه پیام
sender_idstrشناسه فرستنده
timeintزمان ارسال (timestamp)
is_editedboolآیا پیام ویرایش شده
sender_typestrنوع فرستنده (User/Bot)
reply_to_message_idstr | Noneشناسه پیام پاسخ داده شده
forwarded_fromForwardedFrom | Noneاطلاعات فوروارد
fileFile | Noneاطلاعات فایل
stickerSticker | Noneاطلاعات استیکر
pollPoll | Noneاطلاعات نظرسنجی
locationLocation | Noneموقعیت مکانی
aux_dataAuxData | Noneداده‌های کمکی
پراپرتی‌های بولی (Boolean Properties)
پراپرتی‌های تشخیصی
تشخیص نوع پیام، وضعیت و ویژگی‌ها
is_text is_command is_user is_private is_group is_channel is_reply is_forwarded has_media is_photo is_video is_audio is_music is_voice is_document is_archive is_executable is_font is_gif has_link is_emoji is_pure_emoji has_metadata
@bot.on_message()
async def handler(bot: Robot, message: Message):
    if message.is_command:
        await message.reply("این یک دستور است!")
    
    if message.is_photo:
        await message.reply("عکس دریافت شد!")
    
    if message.is_private:
        await message.reply("این پیام خصوصی است")
    
    if message.is_pure_emoji:
        await message.reply("فقط ایموجی ارسال کردی")
پراپرتی‌های مقداری
پراپرتی‌های مقداری
دسترسی به متن، شمارش ایموجی، متاتایپ‌ها و...
پراپرتینوعتوضیحات
textstrمتن پیام
emoji_countintتعداد ایموجی‌های متن
message_typestrنوع پیام (photo, video, text, ...)
meta_typeslistلیست انواع متادیتا
@bot.on_message()
async def handler(bot: Robot, message: Message):
    print(f"متن: {message.text}")
    print(f"تعداد ایموجی: {message.emoji_count}")
    print(f"نوع پیام: {message.message_type}")
    print(f"انواع متادیتا: {message.meta_types}")
متد reply
reply()
پاسخ دادن به پیام (با قابلیت حذف خودکار و ویرایش)
نامنوعتوضیحات
textstrمتن پاسخ
delete_afterint | Noneحذف خودکار پس از n ثانیه
parse_modeLiteral["HTML","Markdown"] | Noneحالت فرمت‌بندی
Pick - شیء با متدهای edit() و delete()
@bot.on_message(commands=["start"])
async def start(bot: Robot, message: Message):
    # پاسخ ساده
    await message.reply("سلام")
    
    # پاسخ با حذف خودکار بعد از 5 ثانیه
    msg = await message.reply("این پیام بعد از 5 ثانیه حذف می‌شود", delete_after=5)
    
    # پاسخ با فرمت HTML
    await message.reply("متن بولد شده", parse_mode="HTML")
متد answer
answer()
پاسخ ساده به پیام (معادل reply)
@bot.on_message()
async def handler(bot: Robot, message: Message):
    await message.answer("پاسخ سریع")
متدهای reply_*
reply_image() / reply_photo()
پاسخ با تصویر
await message.reply_image("photo.jpg", text="کپشن عکس")
reply_file() / reply_document()
پاسخ با فایل
await message.reply_file("document.pdf", text="فایل پیوست")
reply_video()
پاسخ با ویدیو
await message.reply_video("video.mp4", text="ویدیو")
reply_music()
پاسخ با موزیک
await message.reply_music("song.mp3", text="آهنگ جدید")
reply_voice()
پاسخ با پیام صوتی
await message.reply_voice("voice.ogg")
reply_gif()
پاسخ با گیف
await message.reply_gif("animation.gif")
reply_location()
پاسخ با موقعیت مکانی
await message.reply_location("35.6892", "51.3890")
reply_contact()
پاسخ با مخاطب
await message.reply_contact("علی", "رضایی", "09123456789")
reply_poll()
پاسخ با نظرسنجی
await message.reply_poll(
    question="رنگ مورد علاقه شما؟",
    options=["قرمز", "آبی", "سبز"],
    type="Regular"
)
reply_sticker()
پاسخ با استیکر
await message.reply_sticker("sticker_id_here")
reply_keypad()
پاسخ با کیبورد معمولی
keypad = {"rows": [[{"text": "دکمه 1"}]]}
await message.reply_keypad("متن پیام", keypad)
reply_inline()
پاسخ با کیبورد شیشه‌ای (Inline)
from rubka.button import InlineBuilder

builder = InlineBuilder().row(
    InlineBuilder().button_simple(id="btn1", text="دکمه 1")
).build()
await message.reply_inline("متن پیام", builder)
متدهای copy و copy_message
copy() / copy_message()
کپی کردن پیام به چت دیگر
نامنوعتوضیحات
to_chat_idstrشناسه چت مقصد
message_idstr | Noneشناسه پیام (اختیاری)
@bot.on_message(commands=["copy"])
async def copy_command(bot: Robot, message: Message):
    # کپی پیام به چت دیگر
    await message.copy("target_chat_id")
    
    # کپی با پاسخ به پیام خاص
    await message.copy_message("target_chat_id", "message_id")
متد edit
edit()
ویرایش متن پیام
new_text str - متن جدید
@bot.on_message()
async def handler(bot: Robot, message: Message):
    if message.text == "edit":
        await message.edit("متن جدید ویرایش شده")
متد delete
delete()
حذف پیام
@bot.on_message(commands=["del"])
async def delete_command(bot: Robot, message: Message):
    await message.delete()
    await message.reply("پیام حذف شد")
پراپرتی‌های Async
author_name / name / username / author_info
دریافت اطلاعات فرستنده به صورت async
@bot.on_message()
async def handler(bot: Robot, message: Message):
    # دریافت نام فرستنده
    name = await message.author_name
    print(f"نام: {name}")
    
    # دریافت نام کاربری
    username = await message.username
    print(f"یوزرنیم: {username}")
    
    # دریافت اطلاعات کامل چت
    info = await message.author_info
    print(f"اطلاعات: {info}")
مثال کامل
نمونه کد کامل
استفاده از کلاس Message در یک ربات
from rubka import Robot, Message

bot = Robot("YOUR_TOKEN")

@bot.on_message()
async def handler(bot: Robot, message: Message):
    # تشخیص نوع پیام
    if message.is_command:
        if message.text == "/start":
            await message.reply("سلام! به ربات خوش آمدید")
    
    elif message.is_photo:
        await message.reply("عکس زیبایی دریافت کردم")
    
    elif message.is_emoji:
        await message.reply(f"{message.emoji_count} تا ایموجی فرستادی")
    
    elif message.has_link:
        await message.reply("لینک در پیام شما وجود دارد")
    
    else:
        # پاسخ با کیبورد شیشه‌ای
        from rubka.button import InlineBuilder
        builder = InlineBuilder().row(
            InlineBuilder().button_simple(id="info", text="اطلاعات")
        ).build()
        await message.reply_inline("چی می‌خوای؟", builder)

@bot.on_callback(button_id="info")
async def info_callback(bot, query):
    await query.reply("این یک دکمه شیشه‌ای است")

bot.run()