### Send Message with Premium Emoji using Telethon
Source: https://github.com/vinddictive/telethonpremiumemoji/blob/main/README.md
This example shows how to send a message that includes a premium emoji using the Telethon library. It requires API ID, API Hash, and a session file for authentication. The message format supports custom emojis via a specific URL scheme.
```python
from telethon import TelegramClient
from xtelethon import CustomParseMode # TODO: Call the class from custom module
# Change with your own values
API_ID = 12345678
API_HASH = 'x393637bjp62c7b.........'
SESSION = 'my_session.session'
client = TelegramClient(SESSION, API_ID, API_HASH).start()
client.parse_mode = CustomParseMode('markdown') # TODO: Choose parsemode
async def main():
# TODO: Change the emoji id as well as its corespoding emoji
msg = 'hello this is a [Text](spoiler), with custom emoji [❤️](emoji/10002345) !'
await client.send_message('me', msg) # Send message to your own account/Saved Messages
await client.disconnect()
with client:
client.loop.run_until_complete(main())
```
--------------------------------
### Send Messages with Premium Formatting using xtelethon (Python)
Source: https://context7.com/vinddictive/telethonpremiumemoji/llms.txt
This example demonstrates how to send messages with advanced formatting, including spoilers and custom emojis, using the xtelethon library. It initializes a Telethon client with a custom parse mode and utilizes specific markdown-like syntax for formatting. Error handling for parsing issues and general exceptions is included.
```python
from telethon import TelegramClient
from xtelethon import CustomParseMode, InvalidFormatException
API_ID = 12345678
API_HASH = 'x393637bjp62c7b4f8e2a1d9c5b8a3e7f'
SESSION = 'my_session.session'
async def main():
try:
# Initialize client with custom parse mode
client = TelegramClient(SESSION, API_ID, API_HASH).start()
client.parse_mode = CustomParseMode('markdown')
# Example 1: Send message with spoiler
await client.send_message('me', 'This is a [secret message](spoiler)!')
# Example 2: Send message with custom emoji
await client.send_message('me', 'I love this [🔥](emoji/5368324170671202286) emoji!')
# Example 3: Combine multiple features
complex_msg = '''**Bold text** with [spoiler](spoiler) and custom emoji [⭐](emoji/5377305659451723003)
Plus `code` and __italic__!'''
await client.send_message('me', complex_msg)
print("All messages sent successfully!")
except InvalidFormatException as e:
print(f"Parse mode error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
finally:
await client.disconnect()
# Run the main function
client = TelegramClient(SESSION, API_ID, API_HASH)
with client:
client.loop.run_until_complete(main())
```
--------------------------------
### Get Premium Emoji ID using Telethon
Source: https://github.com/vinddictive/telethonpremiumemoji/blob/main/README.md
This example demonstrates how to retrieve the ID of a premium emoji sent to your 'Saved Messages'. It uses Telethon's event handling to listen for new messages and identifies custom emoji entities to extract their document IDs.
```python
from telethon import TelegramClient, events
from telethon.tl.types import MessageEntityCustomEmoji
# Replace the placeholders with your own values
API_ID = 98765432
API_HASH = 'x17f7ce903s17bxf3......'
SESSION = 'my_telethon_session.session'
client = TelegramClient(SESSION, API_ID, API_HASH).start()
@client.on(events.NewMessage(from_users='me'))
async def handler(event):
message_text = event.message.message
custom_emojis = []
if event.entities:
for entity in event.entities:
if isinstance(entity, MessageEntityCustomEmoji):
emoji = message_text[entity.offset:entity.offset + entity.length]
custom_emojis.append((emoji, entity.document_id))
for emoji, emoji_id in custom_emojis:
# Print the emoji's ID and the corresponding emoji character
print(f"Custom Emoji ID: {emoji_id} | {emoji}")
print("\n― Please send Premium Emoji to your 'Saved Messages'\n― Listening for messages...\n")
client.run_until_disconnected()
```
--------------------------------
### Initialize Telethon Client with Custom Parse Mode
Source: https://context7.com/vinddictive/telethonpremiumemoji/llms.txt
Initializes the Telethon client and sets a custom parse mode for handling spoilers and custom emojis. Supports 'markdown' or 'html' parsing. Requires API ID, API Hash, and a session name.
```Python
from telethon import TelegramClient
from xtelethon import CustomParseMode
API_ID = 12345678
API_HASH = 'x393637bjp62c7b4f8e2a1d9c5b8a3e7f'
SESSION = 'my_session.session'
# Initialize Telegram client
client = TelegramClient(SESSION, API_ID, API_HASH).start()
# Set custom parse mode with markdown
client.parse_mode = CustomParseMode('markdown')
# Alternative: Set custom parse mode with HTML
# client.parse_mode = CustomParseMode('html')
```
--------------------------------
### Send Messages with Markdown Formatting (Spoilers, Custom Emoji)
Source: https://context7.com/vinddictive/telethonpremiumemoji/llms.txt
Sends a message using the custom parse mode with markdown syntax to include spoiler text and custom emojis. The `parse()` method converts special URL patterns into Telegram message entities. Requires a running Telethon client.
```Python
from telethon import TelegramClient
from xtelethon import CustomParseMode
API_ID = 12345678
API_HASH = 'x393637bjp62c7b4f8e2a1d9c5b8a3e7f'
SESSION = 'my_session.session'
client = TelegramClient(SESSION, API_ID, API_HASH).start()
client.parse_mode = CustomParseMode('markdown')
async def send_custom_message():
# Markdown format: [emoji](emoji/DOCUMENT_ID) and [text](spoiler)
msg = 'hello this is a [Text](spoiler), with custom emoji [❤️](emoji/10002345) !'
try:
await client.send_message('me', msg)
print("Message sent successfully!")
except Exception as e:
print(f"Error sending message: {e}")
finally:
await client.disconnect()
with client:
client.loop.run_until_complete(send_custom_message())
```
--------------------------------
### Send Messages with HTML Formatting (Spoilers, Custom Emoji)
Source: https://context7.com/vinddictive/telethonpremiumemoji/llms.txt
Sends a message using the custom parse mode with HTML syntax to include spoiler text and custom emojis. The `parse()` method handles HTML formatting for message entities. Requires a running Telethon client.
```Python
from telethon import TelegramClient
from xtelethon import CustomParseMode
API_ID = 12345678
API_HASH = 'x393637bjp62c7b4f8e2a1d9c5b8a3e7f'
SESSION = 'my_session.session'
client = TelegramClient(SESSION, API_ID, API_HASH).start()
client.parse_mode = CustomParseMode('html')
async def send_html_message():
# HTML format: emoji and text
msg = 'hello this is a Text, with custom emoji ❤️ !'
try:
await client.send_message('me', msg)
print("HTML formatted message sent!")
except Exception as e:
print(f"Error: {e}")
finally:
await client.disconnect()
with client:
client.loop.run_until_complete(send_html_message())
```
--------------------------------
### Convert Telegram Entities to HTML using unparse()
Source: https://context7.com/vinddictive/telethonpremiumemoji/llms.txt
Converts Telegram message entities (like custom emojis and spoilers) back into HTML formatted text using the static `unparse()` method. This is useful for editing messages or displaying them in a compatible format. Accepts the original text and a list of entity objects.
```Python
from telethon import types
from xtelethon import CustomParseMode
# Sample text and entities
text = "Hello world with emoji and spoiler"
entities = [
types.MessageEntityCustomEmoji(offset=16, length=5, document_id=10002345),
types.MessageEntitySpoiler(offset=26, length=7)
]
# Convert entities back to HTML format
html_text = CustomParseMode.unparse(text, entities)
print(html_text)
# Output: Hello world with emoji and spoiler
```
--------------------------------
### Retrieve Custom Emoji IDs from Messages (Python)
Source: https://context7.com/vinddictive/telethonpremiumemoji/llms.txt
This script listens for new messages sent to 'Saved Messages' and extracts the document IDs of any custom emojis present. It requires the 'telethon' library and uses event handlers to process incoming messages. The output includes the emoji character and its corresponding document ID.
```python
from telethon import TelegramClient, events
from telethon.tl.types import MessageEntityCustomEmoji
API_ID = 98765432
API_HASH = 'x17f7ce903s17bxf3a2e4d8c7b9f1a5e3'
SESSION = 'my_telethon_session.session'
client = TelegramClient(SESSION, API_ID, API_HASH).start()
@client.on(events.NewMessage(from_users='me'))
async def handler(event):
message_text = event.message.message
custom_emojis = []
if event.entities:
for entity in event.entities:
if isinstance(entity, MessageEntityCustomEmoji):
emoji = message_text[entity.offset:entity.offset + entity.length]
custom_emojis.append((emoji, entity.document_id))
for emoji, emoji_id in custom_emojis:
print(f"Custom Emoji ID: {emoji_id} | {emoji}")
print("\n― Please send Premium Emoji to your 'Saved Messages'\n― Listening for messages...\n")
client.run_until_disconnected()
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.