def main(): _, api_id, api_hash, bot_token, destination_id, *args = sys.argv destination_id = int(destination_id) client = TelegramClient(MemorySession(), api_id, api_hash).start(bot_token=bot_token) Thread(target=stream_stdout, args=(['journalctl', '-f'] + args, )).start() last_message = None while True: if lines.empty(): time.sleep(0.1) else: send_lines = [] while True: try: send_lines.append(lines.get(block=False)) except Empty: break sending_message = '\n\n'.join(send_lines) editing_message = ( last_message.message + '\n\n' + sending_message) if last_message != None else None if editing_message and len(editing_message) < 4096: last_message = last_message.edit(editing_message) else: last_message = client.send_message(destination_id, sending_message) print(lines.qsize()) time.sleep(1)
def get_bot(): tg_api_id = int(os.environ['TG_API_ID']) tg_api_hash = os.environ['TG_API_HASH'] tg_bot_token = os.environ['TG_BOT_TOKEN'] client = TelegramClient(MemorySession(), tg_api_id, tg_api_hash) return client.start(bot_token=tg_bot_token)
def __new__(cls, configuration, loop): if type(configuration) is not Configuration: raise TypeError("'configuration' must be instance of 'configuration.Configuration'") self = super().__new__(cls) self.__configuration = configuration self.__client = TelegramClient( MemorySession(), configuration.api_id, configuration.api_hash, loop=loop ) self.__client.parse_mode = "html" self.__client.start(bot_token=configuration.bot_token) print("Native bot launched") asyncio.ensure_future(self.__init(configuration, loop), loop=loop) return self
SPB_MODE = keiconfig.getboolean('SPB_MODE', False) # SpamWatch sw = KInit.init_sw() from tg_bot.modules.sql import SESSION updater = tg.Updater(TOKEN, workers=min(32, os.cpu_count() + 4), request_kwargs={ "read_timeout": 10, "connect_timeout": 10 }, persistence=PostgresPersistence(SESSION)) telethn = TelegramClient(MemorySession(), APP_ID, API_HASH) dispatcher = updater.dispatcher kp = Client(":memory:", api_id=APP_ID, api_hash=API_HASH, bot_token=TOKEN, workers=min(32, os.cpu_count() + 4)) apps = [kp] async def get_entity(client, entity): entity_client = client if not isinstance(entity, Chat): try:
def clone(self, to_instance=None): return super().clone(MemorySession())
def client(token): return TelegramClient(MemorySession(), tg_app_id, tg_api_hash).start(bot_token=token)
async def handle_conv1(e): text = e.message.text uid = e.sender.id cid = e.chat_id cclient = None logged = None two = False async with client.conversation(uid) as conv: await conv.send_message('Phone Number?', buttons=client.build_reply_markup( Button.text("بازگشت به منوی اصلی"))) phone = await conv.get_response() phone = phone.text print(f"{phone=}") if "بازگشت" in phone: conv.cancel() return phone = phone.replace(" ", "").replace("(", "").replace(")", "") if not phone.replace("+", "").isnumeric(): await e.reply("شماره معتبر نیست") return try: Phone.select().where(Phone.number == phone).get() await e.reply(f"این شماره قبلا اهدا شده است!") return except peewee.DoesNotExist: pass cclient = TelegramClient(MemorySession(), API_ID, API_HASH) await cclient.connect() try: res = await cclient.send_code_request(phone) print(res) except errors.rpcerrorlist.FloodWaitError as ex: await e.reply( f" شماره مورد نظر موقتا دچار فلود شده است و تا {ex.seconds} ثانیه دیگر امکان ارسال کد ندارد" ) return await conv.send_message("Code >>") for attempt in [1, 2, 3]: code = await conv.get_response() code = code.text if "بازگشت" in code: conv.cancel() return try: #print(f"{code=}") logged = await cclient.sign_in(phone, code) except SessionPasswordNeededError: await conv.send_message( "اکانت دارای تایید دو مرحله ای میباشد لطفا رمز آن را ارسال کنید" ) two = True break except errors.PhoneNumberUnoccupiedError: logged = await cclient.sign_up(code, random.choice(NAMES), random.choice(LAST_NAMES)) except (errors.PhoneCodeEmptyError, errors.PhoneCodeExpiredError, errors.PhoneCodeHashEmptyError, errors.PhoneCodeInvalidError): if attempt == 3: await conv.send_message( "تعداد دفعات ورود رمز غلط به 3 رسیده است لطفا بعدا تلاش کنید" ) else: await conv.send_message( f"کد وارد شده اشتباه است لطفا مجددا ارسال کنید ({attempt}/3)" ) except Exception as ex: print(ex) print("check2") if two: print("is2") for attempt in [1, 2, 3]: f2a = await conv.get_response() f2a = f2a.text if "بازگشت به منوی اصلی" in f2a: conv.cancel() return try: #print(f"{f2a=}") logged = await cclient.sign_in(phone=phone, password=f2a) break except (errors.PasswordHashInvalidError): if attempt == 3: await conv.send_message( "تعداد دفعات بیش از حد مجاز شد لطفا بعدا تلاش کنید" ) else: await conv.send_message( f"کد وارد شده اشتباه است یا باطل شده است ! ({attempt}/3)" ) except Exception as ex: print(ex) print("endfor") print(logged) if logged: lw = LoginWeb(phone) sleep(2) msg = await cclient.get_messages(777000, limit=1) msg = msg[0].message pas = re.findall(r".*\s*login\s*code:\s*(.*)\s*Do", msg)[0] psa = pas.strip() lw.login(pas) lw.create_app() ays = lw.get_apis() session = StringSession.save(cclient.session) newphone = Phone.create( number=phone, donater=uid, session=session, aid=ays['api_id'], ahsh=ays['api_hash'], ) u = User.select().where(User.uid == uid).get() udonated = u.donated u.donated = int(udonated) + 1 u.save() newphone.save() await conv.send_message( f"با موفقیت به نام {logged.first_name} {logged.last_name or ''} وارد شد\nامتیاز شما : {int(u.donated)}" ) if cclient: await cclient.disconnect()
def __init__(self, phone): self.phone = phone self.client = TelegramClient(MemorySession(), API_ID, API_HASH)
from telethon.tl.types import DocumentAttributeAudio from telethon.sessions import MemorySession from telethon import TelegramClient from youtube_dl import YoutubeDL from urllib.parse import urlparse akira = "0.1" akira_dir = os.getcwd() + "/akira" def log(text): print(f"[Akira] {text}") client = TelegramClient( MemorySession(), os.environ.get("API_ID"), os.environ.get("API_HASH")).start(bot_token=os.environ.get("BOT_TOKEN")) bot = Bot(token=os.environ.get("BOT_TOKEN")) dp = Dispatcher(bot, storage=MemoryStorage()) # Ass builtins.yt2a_cache = {} builtins.sc2a_cache = {} def get_vid(url): purl = urlparse(url) if purl.query: vid = purl.query.split("&")[0][2:] else: vid = purl.path[1:] if len(vid) == 11: return vid
def __init__(self): api_id = int(os.getenv("TELEGRAM_API_ID")) api_hash = os.getenv("TELEGRAM_API_HASH") bot_token = os.getenv("TELEGRAM_BOT_TOKEN") self.client = TelegramClient(MemorySession(), api_id, api_hash).start(bot_token=bot_token)
SESSION = os.environ.get('TG_SESSION', 'quart') API_ID = int(os.environ.get('API_ID', '0')) API_HASH = os.environ.get('API_HASH', '') # must be channel id (int, positive) MASTER = int(os.environ.get('MASTER', '0')) # must be chat id (int, negative) SLAVE = int(os.environ.get('SLAVE', '0')) PORT = int(os.environ.get('PORT', '8000')) config = hypercorn.Config() config._bind = [f'0.0.0.0:{PORT}'] # config._log = logging.Logger # Telethon client client = TelegramClient(MemorySession(), API_ID, API_HASH) # client = TelegramClient(SESSION, API_ID, API_HASH) # client.parse_mode = 'html' # <- Render things nicely # phone = None phone = os.environ.get('PHONE', '') # Quart app app = Quart(__name__) app.secret_key = 'CHANGE THIS TO SOMETHING SECRET' # Connect the client before we start serving with Quart @app.before_serving async def startup(): await client.connect() await config.log.info(phone)