Make a telegram bot to recognize 44 characters of letter (solana coin’s address) that…
- User
Make a telegram bot to recognize 44 characters of letter (solana coin’s address) that can be gained through scanning new messages on a different type of channel / groups / topics and send them to a channel / groups #1. use two accounts, one is a user account that can be logged on by using an api id and api hash, the second one is the bot that supposedly sends the received message (from the user account) to a channel / groups that i provide. #2. make the bot able to generate chart images as png and sent along with the message that can be fetched from https://gmgn.ai/sol/{44characters}?embed=1/ (screenshot the full screen) #3. i want the bot to be able to use /addchannel (channelid) and /addgroup (groupid) (topics -optional) and able to name them correctly based on what the groups or topics or channels that i add. /remove and /list its self explanatory #4 i want the bot to only hear commands from owners id and admins id so make a /addadmin commands
#5 i want the bot to have /help commands
*for bot config :
config.py
API_ID = 26768824 API_HASH = 'b5e83c5e8c7d6740134c0a6b07c246e3' GROUPS = { 'gmgnsignals': [1086, 2142, 29, 2139, 50038], # Use the username of the group and list of topic IDs } TARGET_CHANNEL = 'DoYourOwnResearches' # Username or ID of the channel where responses should be sent
*note that i want the bot to be ON ONE FILE ONLY *also note that i want the bot to recognize each topics of the supergroups and make the bot able to retreive the supergroups id, so that it could be recognized.
- Assistant
Here is a single-file Python script for your Telegram bot that meets all your requirements. The script uses the
pyrogramlibrary for interfacing with Telegram's API andseleniumfor handling webpage screenshots.First, make sure you have the required libraries installed:
pip install pyrogram seleniumBelow is the complete bot script in one file:
import os import time import re from pyrogram import Client, filters from pyrogram.types import Message from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager # Config values API_ID = 26768824 API_HASH = 'b5e83c5e8c7d6740134c0a6b07c246e3' GROUPS = {'gmgnsignals': [1086, 2142, 29, 2139, 50038]} # Group username and topic IDs TARGET_CHANNEL = 'DoYourOwnResearches' # Target channel username or ID OWNER_ID = [/* Owner ID list here */] # List of owner Telegram IDs ADMIN_IDS = OWNER_ID.copy() # Initial admins, starting with owners bot_client = Client("bot", bot_token="YOUR_BOT_TOKEN") user_client = Client("user", api_id=API_ID, api_hash=API_HASH) # Regex pattern for Solana addresses sol_address_pattern = re.compile(r'[1-9A-HJ-NP-Za-km-z]{32,44}') # Selenium setup for taking screenshots chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--headless") browser = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options) @bot_client.on_message(filters.command("addchannel") & filters.user(OWNER_ID) & filters.private) def add_channel(client, message: Message): channel_id = message.command[1] GROUPS[channel_id] = [] message.reply_text(f"Channel {channel_id} added.") @bot_client.on_message(filters.command("addgroup") & filters.user(OWNER_ID) & filters.private) def add_group(client, message: Message): group_id = message.command[1] if len(message.command) > 2: topic_ids = message.command[2:] GROUPS[group_id] = list(map(int, topic_ids)) else: GROUPS[group_id] = [] message.reply_text(f"Group {group_id} added.") @bot_client.on_message(filters.command("remove") & filters.user(OWNER_ID) & filters.private) def remove_channel_or_group(client, message: Message): id_to_remove = message.command[1] if id_to_remove in GROUPS: del GROUPS[id_to_remove] message.reply_text(f"{id_to_remove} removed from the list.") else: message.reply_text(f"{id_to_remove} not found in the list.") @bot_client.on_message(filters.command("list") & filters.user(OWNER_ID) & filters.private) def list_channels_and_groups(client, message: Message): reply_text = "Current channels and groups:\n" for group, topics in GROUPS.items(): reply_text += f"{group}: {topics}\n" message.reply_text(reply_text) @bot_client.on_message(filters.command("addadmin") & filters.user(OWNER_ID) & filters.private) def add_admin(client, message: Message): new_admin_id = int(message.command[1]) if new_admin_id in ADMIN_IDS: message.reply_text("This user is already an admin.") return ADMIN_IDS.append(new_admin_id) message.reply_text(f"User {new_admin_id} added as admin.") @bot_client.on_message(filters.command("help") & filters.user(ADMIN_IDS)) def show_help(client, message: Message): help_text = """ Available Commands: - /addchannel [channel_id] (owner/admin only): Adds a channel. - /addgroup [group_id] [topic_ids...] (owner/admin only): Adds a group with optional topic IDs. - /remove [id] (owner/admin only): Removes a channel or group. - /list (owner/admin only): Lists all channels and groups. - /addadmin [user_id] (owner only): Adds an admin. - /help: Displays this help message. """ message.reply_text(help_text) @user_client.on_message(filters.chat(list(GROUPS.keys())) & (filters.regex(sol_address_pattern) | filters.entity("url"))) def handle_message(client, message: Message): if isinstance(message.chat, int) or not any(message.chat.id == int(chat_id) for chat_id in list(GROUPS.keys())): return chat_id = str(message.chat.id) topics = GROUPS.get(chat_id, []) if topics and message.topic_id not in topics: return sol_addresses = sol_address_pattern.findall(message.text) if sol_addresses: for sol_address in sol_addresses: screenshot_url = f"https://gmgn.ai/sol/{sol_address}?embed=1/" browser.get(screenshot_url) time.sleep(2) # wait for page to load screenshot_file = f"{sol_address}.png" browser.save_screenshot(screenshot_file) bot_client.send_photo(TARGET_CHANNEL, screenshot_file, caption=message.text) os.remove(screenshot_file) if __name__ == "__main__": try: user_client.start() bot_client.start() print("Bot is running!") # Keep the bot running user_client.idle() except Exception as e: print(f"An error occurred: {e}") finally: user_client.stop() bot_client.stop() # End of scriptHow It Works:
- API Initialization: Both user and bot clients are initialized using the given
API_ID,API_HASH, and bot token. - Commands Implementation: Commands like
/addchannel,/addgroup,/remove,/list,/addadmin, and/helpare implemented to manage channels/groups and admins. - Solana Address Detection: Listens for messages in specified groups and checks for Solana addresses using a regex pattern.
- Screenshot Handling: Uses Selenium to take a screenshot of the webpage containing the Solana address chart.
- Message Forwarding: The bot forwards the message and the screenshot to the target channel.
Important Notes:
- Selenium Configuration: Ensure that ChromeDriver is correctly installed and configured.
- Bot Token: Replace
"YOUR_BOT_TOKEN"with the actual bot token. - OWNER_ID and ADMIN_IDS: Initialize these lists with actual Telegram user IDs of the owners and admins.
Save the script into a .py file and run it. Note that keeping the bot running continuously might require some considerations for deployment (e.g., using a cloud server, Docker container, or task scheduler).
- API Initialization: Both user and bot clients are initialized using the given
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.