|
| 1 | +star_gifts |
| 2 | +========== |
| 3 | + |
| 4 | +This example shows how to handle Star Gift service messages. |
| 5 | + |
| 6 | +Star Gifts are a Telegram feature that allows users to send gifts to each other. When a gift is received, |
| 7 | +a service message is sent with the gift details. This example demonstrates how to detect and handle these messages. |
| 8 | + |
| 9 | +.. code-block:: python |
| 10 | +
|
| 11 | + from pyrogram import Client, filters |
| 12 | + from pyrogram.enums import MessageServiceType |
| 13 | +
|
| 14 | + app = Client("my_account") |
| 15 | +
|
| 16 | +
|
| 17 | + @app.on_message(filters.service) |
| 18 | + async def handle_service_messages(client, message): |
| 19 | + """Handle service messages including star gifts.""" |
| 20 | +
|
| 21 | + # Check if this is a star gift message |
| 22 | + if message.service == MessageServiceType.STAR_GIFT: |
| 23 | + print(f"Received a Star Gift!") |
| 24 | + print(f"Message ID: {message.id}") |
| 25 | + print(f"From: {message.from_user.first_name if message.from_user else 'Anonymous'}") |
| 26 | + print(f"Date: {message.date}") |
| 27 | +
|
| 28 | + # You can access the raw action for more details |
| 29 | + # The raw action contains: gift, name_hidden, saved, converted, etc. |
| 30 | +
|
| 31 | + elif message.service == MessageServiceType.STAR_GIFT_UNIQUE: |
| 32 | + print(f"Received a Unique/Collectible Star Gift!") |
| 33 | + print(f"Message ID: {message.id}") |
| 34 | + print(f"This is a special collectible gift that can be traded as NFT") |
| 35 | +
|
| 36 | + elif message.service == MessageServiceType.NEW_CREATOR_PENDING: |
| 37 | + print(f"Creator transfer is pending!") |
| 38 | + print(f"A new creator has been nominated for this channel/group") |
| 39 | +
|
| 40 | + elif message.service == MessageServiceType.CHANGE_CREATOR: |
| 41 | + print(f"Creator has been changed!") |
| 42 | + print(f"The ownership of this channel/group has been transferred") |
| 43 | +
|
| 44 | +
|
| 45 | + @app.on_message(filters.service) |
| 46 | + async def log_all_service_types(client, message): |
| 47 | + """Log all service message types for debugging.""" |
| 48 | + if message.service: |
| 49 | + print(f"Service message type: {message.service}") |
| 50 | +
|
| 51 | +
|
| 52 | + app.run() |
| 53 | +
|
| 54 | +
|
| 55 | +Using StarGift and StarGiftUnique Types |
| 56 | +--------------------------------------- |
| 57 | + |
| 58 | +The high-level ``StarGift`` and ``StarGiftUnique`` types provide easy access to gift properties: |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | +
|
| 62 | + from pyrogram.types import StarGift, StarGiftUnique |
| 63 | +
|
| 64 | + # StarGift properties: |
| 65 | + # - id: Unique identifier of the gift |
| 66 | + # - sticker: Sticker representing the gift |
| 67 | + # - stars: Price in Telegram Stars |
| 68 | + # - convert_stars: Stars the receiver can convert to |
| 69 | + # - limited: Whether it's a limited-supply gift |
| 70 | + # - sold_out: Whether the gift sold out |
| 71 | + # - birthday: Whether it's a birthday-themed gift |
| 72 | + # - can_upgrade: Whether it can be upgraded to collectible |
| 73 | + # - availability_remains: Remaining gifts (for limited) |
| 74 | + # - availability_total: Total supply (for limited) |
| 75 | +
|
| 76 | + # StarGiftUnique properties (collectible gifts): |
| 77 | + # - id: Unique identifier |
| 78 | + # - gift_id: Base gift type ID |
| 79 | + # - title: Collectible title |
| 80 | + # - slug: For creating deep links |
| 81 | + # - num: Unique number among collectibles of same type |
| 82 | + # - burned: Whether the gift was burned |
| 83 | + # - crafted: Whether it was crafted |
| 84 | + # - owner_id: User ID of owner |
| 85 | + # - owner_address: TON blockchain address |
| 86 | + # - gift_address: NFT address on blockchain |
| 87 | +
|
| 88 | +
|
| 89 | +Filtering Star Gift Messages |
| 90 | +---------------------------- |
| 91 | + |
| 92 | +You can create a custom filter for star gift messages: |
| 93 | + |
| 94 | +.. code-block:: python |
| 95 | +
|
| 96 | + from pyrogram import Client, filters |
| 97 | + from pyrogram.enums import MessageServiceType |
| 98 | +
|
| 99 | + # Custom filter for star gifts |
| 100 | + star_gift_filter = filters.create( |
| 101 | + lambda _, __, m: m.service in ( |
| 102 | + MessageServiceType.STAR_GIFT, |
| 103 | + MessageServiceType.STAR_GIFT_UNIQUE |
| 104 | + ) |
| 105 | + ) |
| 106 | +
|
| 107 | + app = Client("my_account") |
| 108 | +
|
| 109 | +
|
| 110 | + @app.on_message(star_gift_filter) |
| 111 | + async def on_star_gift(client, message): |
| 112 | + """Handle only star gift messages.""" |
| 113 | + if message.service == MessageServiceType.STAR_GIFT: |
| 114 | + await message.reply("Thank you for the star gift! ⭐") |
| 115 | + else: |
| 116 | + await message.reply("Wow, a unique collectible gift! 🎁") |
| 117 | +
|
| 118 | +
|
| 119 | + app.run() |
0 commit comments