Skip to content

Instantly share code, notes, and snippets.

@Kyborg2011
Last active August 6, 2019 18:05
Show Gist options
  • Save Kyborg2011/e4a23b9a2dfd274beb6b991fd06298a6 to your computer and use it in GitHub Desktop.
Save Kyborg2011/e4a23b9a2dfd274beb6b991fd06298a6 to your computer and use it in GitHub Desktop.
A program for basic algorithm of a simplest mailing in Telegram Groups or/and Channels. Using a Telethon library (Python 3).
# For start up make in bash: 'python3 telegram-mailing.py'
from telethon import TelegramClient, sync
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.channels import InviteToChannelRequest
from telethon.errors.rpcerrorlist import PeerFloodError
import time
import random
import sys
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 700000 # YOUR UNIQUE API ID
api_hash = 'qkV0b32ahfyjZqKgrq7z1nC4GuuDbsmop' # YOUR UNIQUE API HASH
# For group https://t.me/some_crypto_group (only for examples)
channel = 'some_crypto_group' # WORKING ONLY FOR GROUPS!!! NOT CHANNELS!!! (if you are not an admin)
offset = 8400
limit = 100
index = 0
i = 0
all_participants = []
client = TelegramClient('session_name', api_id, api_hash).start()
'''
From documentation:
Starting as an user account from code:
client.start(phone, force_sms=true)
phone = '+34 123 123 123'
client.sign_in(phone)
code = input('enter code: ')
client.sign_in(phone, code)
Auth ready code:
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone, force_sms=true)
client.sign_in(phone, auth_code)
Logging out from Telegram account:
client.log_out()
P.S.:AND/OR: client.disconnect()
'''
print('Start getting participants from some_crypto_group group...')
all_participants = client.get_participants(channel, aggressive=True)
print('Finded {} users'.format(all_participants.total))
our_channel = 'some_new_crypto_channel_or_group'
for user in all_participants:
if (index < 50 and i > offset): # Select users considering innitial offset and send no more than 50 invites from one Telegram account (because of some spam protection)
print('Success: index - {} Name: {}'.format(i, user.first_name))
try:
client(InviteToChannelRequest(
our_channel,
[user]
))
index += 1
except PeerFloodError:
print("Getting Flood Error from telegram.")
# Here a program must sign out from Telegram account and then connect to another Telegram account.
except:
e = sys.exc_info()
print(e)
# An important line of code! It's a preventing of Flood Error exception (in the most of cases...)
time.sleep(20)
i += 1
'''
Using CSV (for storing phone numbers db):
import csv
with open(input_file, encoding='UTF-8') as f:
rows = csv.reader(f,delimiter=",",lineterminator="\n")
next(rows, None)
for row in rows:
user = {}
user['username'] = row[0]
(only example of simplest using of CSV in Python!)
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment