Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from telethon import TelegramClient, events, Button
- from telethon.types import (
- InputKeyboardButtonRequestPeer as peer_button,
- RequestPeerTypeBroadcast as broadcast,
- RequestPeerTypeChat as group,
- RequestPeerTypeUser as user,
- UpdateNewMessage,
- MessageService,
- MessageActionRequestedPeerSentMe as action
- )
- bot = TelegramClient("bot", 1, "...")
- @bot.on(events.NewMessage(pattern="/start"))
- async def send_button(event):
- if not event.is_private: return
- button_list = [
- [
- peer_button(text="Select Channel", button_id=1, max_quantity=1, peer_type=broadcast(), username_requested=True),
- peer_button(text="Select Group", button_id=2, max_quantity=1, peer_type=group(), username_requested=True)],
- [
- peer_button(text="Select User", button_id=3, max_quantity=1, peer_type=user(bot=False), username_requested=True),
- peer_button(text="Select Bot", button_id=4, max_quantity=1, peer_type=user(bot=True), username_requested=True),
- ]
- ]
- buttons = bot.build_reply_markup(button_list)
- buttons.resize = True
- await event.respond("Pick a chat:", buttons=buttons)
- chat_types = {1: "Channel", 2: "Group", 3: "User", 4: "Bot"}
- def format_peer_info(p, ty):
- if hasattr(p, "user_id"):
- chat_id = p.user_id
- elif hasattr(p, "chat_id"):
- chat_id = -p.chat_id
- ty = "Small Group"
- elif hasattr(p, "channel_id"):
- if ty == "Group":
- ty = "Supergroup"
- chat_id = f"-100{p.channel_id}"
- u = getattr(p, "username", None)
- return f"You have shared a `{ty}` with ID: `{chat_id}`" + (
- f"\nUsername: @{u}" if u else ""
- )
- @bot.on(events.Raw(UpdateNewMessage))
- async def get_selected_chat(event):
- if not isinstance(event.message, MessageService): return
- if not isinstance(event.message.action, action): return
- msg = event.message
- bot.loop.create_task(bot.delete_messages(msg.chat_id, msg.id))
- chat = msg.action.peers[0]
- chat_type = chat_types.get(msg.action.button_id)
- await bot.send_message(
- msg.chat_id, format_peer_info(chat, chat_type), buttons=Button.clear()
- )
- bot.start()
- bot.run_until_disconnected()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement