Skip to content

Instantly share code, notes, and snippets.

@AlperRehaYAZGAN
Last active January 10, 2025 00:51
Show Gist options
  • Save AlperRehaYAZGAN/a860f76ac6207041c58a6e2435b38d79 to your computer and use it in GitHub Desktop.
Save AlperRehaYAZGAN/a860f76ac6207041c58a6e2435b38d79 to your computer and use it in GitHub Desktop.
a simple tg forwarder
from telethon import TelegramClient, events
import re
import os
from datetime import datetime
from colorama import init, Fore, Style
from solders.pubkey import Pubkey
import base58
# Initialize colorama for cross-platform colored output
init()
# load .env file
from dotenv import load_dotenv
load_dotenv()
def log_info(message):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"{Fore.CYAN}[{timestamp}] INFO: {message}{Style.RESET_ALL}")
def log_success(message):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"{Fore.GREEN}[{timestamp}] SUCCESS: {message}{Style.RESET_ALL}")
def log_error(message):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"{Fore.RED}[{timestamp}] ERROR: {message}{Style.RESET_ALL}")
# Rest of your imports and environment loading...
api_id = os.getenv("API_ID")
api_hash = os.getenv("API_HASH")
destination_chat = os.getenv("DESTINATION_CHAT")
source_channel_advisor = os.getenv("SOURCE_CHANNEL_ADVISOR")
# sample .env
# API_ID=1234567
# API_HASH=123456789abcdef123456789abcdef12
# DESTINATION_CHAT=@destination_chat
# SOURCE_CHANNEL_ADVISOR=@source_channel_advisor
if not api_id or not api_hash or not destination_chat or not source_channel_advisor:
log_error("Missing environment variables! Please check your .env file.")
raise Exception("API_ID, API_HASH, SOURCE_CHANNEL_ADVISOR ve DESTINATION_CHAT değerlerini .env dosyasına ekleyin.")
# Bot veya kullanıcı oturumu için bir Telegram istemcisi oluşturun
max_forwarded_message_count = 10000
current_forward_count = 0
# solana wallets are bs58 format try to find them from message
with TelegramClient('name', api_id, api_hash) as client:
log_info("=" * 50)
log_info("✅ Telegram Bot Started")
log_info(f"Listening User: {source_channel_advisor}")
log_info(f"Destination Chat: {destination_chat}")
log_info("=" * 50)
# await client.send_message('me', 'Hello, myself!')
@client.on(events.NewMessage(incoming=True, from_users=[source_channel_advisor]))
async def forward_solana_addresses(event):
sender = await event.get_sender()
message_text = event.message.message
log_info(f"🔥 New message received from {sender.username} Message: {message_text}")
wallets = []
# split messages by space and find bs58 encoded string then if bs58 encoded string exist check that a valid Pubkey.from_string and Pubkey.is_on_curve validations
for word in message_text.split():
try:
decoded = base58.b58decode(word)
if len(decoded) == 32:
encoded = base58.b58encode(decoded).decode('utf-8')
wallets.append(encoded)
except:
pass
if len(wallets) > 0:
# check forward_count (if 10000 reached notify me "limit reached" and stop bot)
global current_forward_count
current_forward_count += len(wallets)
if current_forward_count >= max_forwarded_message_count:
log_info(f"❌ Limit reached! Stopping the bot. Limit is {max_forwarded_message_count}")
await client.send_message(destination_chat, f"❌ Limit reached! Stopping the bot. Limit is {max_forwarded_message_count}")
client.disconnect()
return
# join wallets
forwarded_message = "\n".join(f"{wallet}" for wallet in wallets)
await client.send_message(destination_chat, forwarded_message)
log_success(f"✅ Successfully forwarded {len(wallets)} Solana addresses")
else:
pass
# log_info("❌ No Solana addresses found in the message")
client.run_until_disconnected()
log_info("❌ Telegram Bot Stopped")
# pip3 install python-dotenv telethon colorama base58 solders solana
# python3 main.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment