Advertisement
disk6969

Telethon request peer button

Jan 17th, 2025 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. from telethon import TelegramClient, events, Button
  2.  
  3. from telethon.types import (
  4.     InputKeyboardButtonRequestPeer as peer_button,
  5.  
  6.     RequestPeerTypeBroadcast as broadcast,
  7.     RequestPeerTypeChat as group,
  8.     RequestPeerTypeUser as user,
  9.  
  10.     UpdateNewMessage,
  11.     MessageService,
  12.     MessageActionRequestedPeerSentMe as action
  13. )
  14.  
  15. bot = TelegramClient("bot", 1, "...")
  16.  
  17. @bot.on(events.NewMessage(pattern="/start"))
  18. async def send_button(event):
  19.     if not event.is_private: return
  20.  
  21.     button_list = [
  22.         [
  23.             peer_button(text="Select Channel", button_id=1, max_quantity=1, peer_type=broadcast(), username_requested=True),
  24.             peer_button(text="Select Group", button_id=2, max_quantity=1, peer_type=group(), username_requested=True)],
  25.         [
  26.             peer_button(text="Select User", button_id=3, max_quantity=1, peer_type=user(bot=False), username_requested=True),
  27.             peer_button(text="Select Bot", button_id=4, max_quantity=1, peer_type=user(bot=True), username_requested=True),
  28.         ]
  29.     ]
  30.     buttons = bot.build_reply_markup(button_list)
  31.     buttons.resize = True
  32.     await event.respond("Pick a chat:", buttons=buttons)
  33.  
  34.  
  35. chat_types = {1: "Channel", 2: "Group", 3: "User", 4: "Bot"}
  36.  
  37. def format_peer_info(p, ty):
  38.     if hasattr(p, "user_id"):
  39.         chat_id = p.user_id
  40.     elif hasattr(p, "chat_id"):
  41.         chat_id = -p.chat_id
  42.         ty = "Small Group"
  43.     elif hasattr(p, "channel_id"):
  44.         if ty == "Group":
  45.             ty = "Supergroup"
  46.         chat_id = f"-100{p.channel_id}"
  47.  
  48.     u = getattr(p, "username", None)
  49.     return f"You have shared a `{ty}` with ID: `{chat_id}`" + (
  50.         f"\nUsername: @{u}" if u else ""
  51.     )
  52.  
  53. @bot.on(events.Raw(UpdateNewMessage))
  54. async def get_selected_chat(event):
  55.     if not isinstance(event.message, MessageService): return
  56.     if not isinstance(event.message.action, action): return
  57.     msg = event.message
  58.     bot.loop.create_task(bot.delete_messages(msg.chat_id, msg.id))
  59.  
  60.  
  61.     chat = msg.action.peers[0]
  62.     chat_type = chat_types.get(msg.action.button_id)
  63.  
  64.     await bot.send_message(
  65.         msg.chat_id, format_peer_info(chat, chat_type), buttons=Button.clear()
  66.     )
  67.  
  68. bot.start()
  69. bot.run_until_disconnected()
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement