Creating a Telegram bot that fulfills all your requests is a somewhat complex task. We'll need to use the telethon library for the user account interactions and python-telegram-bot for the bot functionalities. Let's break down the requirements step by step and create a single Python script to accomplish the following features:
- Monitoring specific channels and groups for potential Solana addresses.
- Sending messages to another channel/group.
- Generating chart images.
- Handling admin-specific commands.
- Configuring and setting up groups and channels dynamically.
Requirements:
Install the required libraries via pip if you haven't already:
pip install telethon python-telegram-bot pyppeteer
Full Script Implementation
Here's a single script that integrates both telethon and python-telegram-bot to achieve the required functionality:
import asyncio
import re
import sys
from telethon import TelegramClient, events
from telethon.tl.types import PeerChannel
from telethon.tl.functions.channels import JoinChannelRequest
from pyppeteer import launch
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
# Configurations
API_ID = 26768824
API_HASH = 'b5e83c5e8c7d6740134c0a6b07c246e3'
BOT_TOKEN = 'YOUR_BOT_TOKEN'
OWNER_ID = [123456789] # Replace with the actual owner ID(s)
CHANNELS = {}
TARGET_CHANNEL = 'DoYourOwnResearches'
# Initialize Telethon client
client = TelegramClient('anon', API_ID, API_HASH)
# Function to extract chart from URL
async def screenshot_chart(sol_address):
browser = await launch()
page = await browser.newPage()
await page.goto(f'https://gmgn.ai/sol/{sol_address}?embed=1/')
img_path = f'{sol_address}.png'
await page.screenshot({'path': img_path, 'fullPage': True})
await browser.close()
return img_path
# Bot commands
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hello! Use /help to see available commands.')
def help_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Available commands: /addchannel, /addgroup, /remove, /list, /addadmin')
def addchannel(update: Update, context: CallbackContext) -> None:
if update.message.from_user.id not in OWNER_ID:
return
channel_id = context.args[0]
CHANNELS[channel_id] = []
update.message.reply_text(f'Channel {channel_id} added.')
def addgroup(update: Update, context: CallbackContext) -> None:
if update.message.from_user.id not in OWNER_ID:
return
group_id = context.args[0]
CHANNELS[group_id] = []
update.message.reply_text(f'Group {group_id} added.')
def remove(update: Update, context: CallbackContext) -> None:
if update.message.from_user.id not in OWNER_ID:
return
name = context.args[0]
if name in CHANNELS:
del CHANNELS[name]
update.message.reply_text(f'{name} removed.')
else:
update.message.reply_text(f'{name} not found.')
def list_channels(update: Update, context: CallbackContext) -> None:
if update.message.from_user.id not in OWNER_ID:
return
result = "Channels and Groups:\n" + "\n".join(CHANNELS.keys())
update.message.reply_text(result)
def addadmin(update: Update, context: CallbackContext) -> None:
if update.message.from_user.id not in OWNER_ID:
return
new_admin = int(context.args[0])
OWNER_ID.append(new_admin)
update.message.reply_text(f'Admin {new_admin} added.')
async def main():
# Start the bot and its handlers
updater = Updater(BOT_TOKEN)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('help', help_command))
dispatcher.add_handler(CommandHandler('addchannel', addchannel))
dispatcher.add_handler(CommandHandler('addgroup', addgroup))
dispatcher.add_handler(CommandHandler('remove', remove))
dispatcher.add_handler(CommandHandler('list', list_channels))
dispatcher.add_handler(CommandHandler('addadmin', addadmin))
updater.start_polling()
# Start Telethon client
await client.start(phone=lambda: input('Enter phone number: '))
for channel in CHANNELS.keys():
try:
await client(JoinChannelRequest(PeerChannel(int(channel))))
except Exception as e:
print(f"Could not join {channel}: {e}")
# Message handler
@client.on(events.NewMessage)
async def handler(event):
message = event.message.message
sol_address = re.findall(r'\b[A-Za-z0-9]{44}\b', message)
if sol_address:
sol_address = sol_address[0]
img_path = await screenshot_chart(sol_address)
context.bot.send_photo(chat_id=TARGET_CHANNEL, photo=open(img_path, 'rb'), caption=message)
await client.run_until_disconnected()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Explanation:
-
Bot Part:
- The bot part is managed by
python-telegram-bot. It defines the commands (/start, /help, /addchannel, /addgroup, /remove, /list, /addadmin).
- The owner/admin ID check is performed before executing commands.
-
User Account Part:
- The
Telethon client is used for listening to messages from specified groups/channels.
- Regular expressions extract any 44-character Solana address from the messages.
- Upon detecting a Solana address, a chart image is generated using
pyppeteer and sent along with the message to the target channel/group.
-
Integrations:
- The bot needs to generate PNGs from a dynamic URL using
pyppeteer.
- Asynchronous handling is required to concurrently run bot operations and the Telegram client.
-
Configuration:
- The script uses a dictionary for channels/groups and allows adding admins dynamically.
Make sure to replace 'YOUR_BOT_TOKEN' with the actual bot token from BotFather. Note that for production use, additional error handling and logging should be added.