Advertisement
disk6969

pyro RequestPeer example

Aug 13th, 2023 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. from pyrogram import Client, filters
  2. from pyrogram.types import ReplyKeyboardRemove
  3. from pyrogram.utils import get_peer_id
  4.  
  5. from pyrogram.raw.functions.messages import SendMessage
  6. from pyrogram.raw.types import (
  7.   ReplyKeyboardMarkup,
  8.   KeyboardButtonRow as row,
  9.   KeyboardButtonRequestPeer as btn,
  10.   RequestPeerTypeBroadcast as broadcast,
  11.   RequestPeerTypeChat as group,
  12.   RequestPeerTypeUser as user,
  13.   UpdateNewMessage,
  14.   MessageService,
  15.   MessageActionRequestedPeer as action,
  16. )
  17.  
  18. @Client.on_message(filters.private & filters.command("pick"))
  19. async def send_button(client, message):
  20.   reply_markup = ReplyKeyboardMarkup(
  21.     rows=[
  22.       row(
  23.         buttons=[
  24.           btn(text="Select Channel", button_id=1, peer_type=broadcast()),
  25.           btn(text="Select Group", button_id=2, peer_type=group()),
  26.         ]
  27.       ),
  28.       row(
  29.         buttons=[
  30.           btn(text="Select User", button_id=3, peer_type=user(bot=False)),
  31.           btn(text="Select Bot", button_id=4, peer_type=user(bot=True)),
  32.         ]
  33.       ),
  34.     ],
  35.     resize=True,
  36.   )
  37.   await client.invoke(
  38.     SendMessage(
  39.       peer=await client.resolve_peer(message.chat.id),
  40.       message="Pick a chat:",
  41.       reply_markup=reply_markup,
  42.       random_id=client.rnd_id(),
  43.     )
  44.   )
  45.  
  46.  
  47. chat_types = {1: "channel", 2: "group", 3: "user", 4: "bot"}
  48.  
  49.  
  50. @Client.on_raw_update()
  51. async def get_selected_chat(client, update, _, __):
  52.   if not isinstance(update, UpdateNewMessage):
  53.     return
  54.   if not isinstance(update.message, MessageService):
  55.     return
  56.   if not isinstance(update.message.action, action):
  57.     return
  58.  
  59.   from_user = get_peer_id(update.message.peer_id)
  60.   chat = get_peer_id(update.message.action.peer)
  61.   type = chat_types.get(update.message.action.button_id)
  62.   await client.send_message(
  63.     from_user,
  64.     f"`You have shared a {type} with ID: {chat}`",
  65.     reply_markup=ReplyKeyboardRemove(),
  66.   )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement