TELEGRAM BOT API · ERTEBAT 0.1.0
مرجع کامل متدهای تلگرام
این صفحه با سورس فعلی ertebat/telegram/bot.py تطبیق داده شده است. این فهرست، wrapperهای آماده Ertebat را نشان میدهد؛ برای endpointهای دیگر Bot API از request() استفاده کنید.
ساخت کلاینت و خطا
from ertebat import telegram
bot = telegram("TELEGRAM_BOT_TOKEN", timeout=40)
سازنده: Ertebat(token=None, *, timeout=40, base_url=None) · خطا: TelegramAPIError · نام مستعار کلاس: telegram.Bot.
درخواست و دریافت اطلاعات
await bot.request(method, payload=None)— فراخوانی مستقیم Telegram Bot API.await bot.get_me()— دریافت اطلاعات ربات.await bot.get_chat(chat_id)— دریافت اطلاعات گفتگو.await bot.get_updates(*, offset=None, limit=100, timeout=30, allowed_updates=None)— دریافت Updateها با long polling؛ limit بین 1 تا 100 و timeout نامنفی.
ارسال و مدیریت پیام
await bot.send_message(chat_id, text, **kwargs)— گزینههای پشتیبانیشده در wrapper:parse_mode،entities،link_preview_options،disable_notification،protect_content،reply_parameters،reply_markup،message_thread_idوbusiness_connection_id.await bot.send_photo(chat_id, photo, caption=None, **kwargs)— ارسال عکس با file_id یا URL؛ گزینهها:parse_mode،reply_markup،reply_parameters،message_thread_idوdisable_notification.await bot.send_document(chat_id, document, caption=None, **kwargs)— ارسال سند با file_id یا URL؛ گزینهها مانند send_photo.await bot.forward_message(chat_id, from_chat_id, message_id, **kwargs)— فوروارد؛ گزینهها:message_thread_id،disable_notificationوprotect_content.await bot.edit_message_text(chat_id, message_id, text, **kwargs)— ویرایش متن؛ گزینهها:parse_mode،entities،link_preview_optionsوreply_markup.await bot.delete_message(chat_id, message_id)— حذف پیام.await bot.answer_callback_query(callback_query_id, **kwargs)— پاسخ به callback؛ گزینهها:text،show_alert،urlوcache_time.await event.reply(text, **kwargs)— پاسخ به پیام جاری؛ در تلگرام بهصورت پیشفرضreply_parametersرا تنظیم میکند.
ثبت handler و Event
@bot.command(names)— ثبت یک یا چند فرمان.@bot.message(flt=None)— ثبت handler پیام با فیلتر callable اختیاری.@bot.callback()— ثبت handler callback query.
فیلدهای Event: raw و update، message، chat، chat_id، message_id، text، sender، sender_id، callback_query، button_id، callback_query_id و type.
from ertebat import telegram
bot = telegram("TELEGRAM_BOT_TOKEN")
@bot.command(["start", "help"])
async def commands(bot, event):
await event.reply("سلام از تلگرام")
@bot.message(lambda event: event.text == "سلام")
async def hello(bot, event):
await event.reply("سلام!")
@bot.callback()
async def buttons(bot, event):
await bot.answer_callback_query(
event.callback_query_id,
text="انتخاب شما ثبت شد"
)
bot.run()
اجرای polling
await bot.start(*, timeout=30, retry_delay=2)— شروع long polling.bot.stop()— درخواست توقف polling.bot.run(*, timeout=30, retry_delay=2)— اجرای ساده؛ اگر event loop فعال باشد coroutine شروع را برمیگرداند.
محدودیت مهم: در Telegram Bot API، استفاده از getUpdates و webhook همزمان ممکن نیست. در کلاینت فعلی Ertebat متدهای اختصاصی set_webhook، delete_webhook، get_webhook_info، send_audio، send_video، send_location و send_file وجود ندارند؛ برای endpointهای دیگر میتوانید از request() با پارامترهای مستند API استفاده کنید.
مستندات رسمی Telegram Bot API · راهنمای شروع سریع · نمونهکدهای همه کلاسها