Add code execution and uploading to telegram features: import os
import asyncio
import logging
import io
from telethon import TelegramClient
from telethon.sessions import StringSession
from flask import Flask, request

# Set up logging to write to the StringIO object
log_stream = io.StringIO()
logging.basicConfig(stream=log_stream, level=logging.INFO)

app = Flask(__name__)

# Define Telegram API credentials
api_id = os.getenv('TELEGRAM_API_ID')
api_hash = os.getenv('TELEGRAM_API_HASH')
string_session = os.getenv('STRING_SESSION')

# Initialize the Telegram client using the string session
if string_session:
    client = TelegramClient(StringSession(string_session), api_id, api_hash)
else:
    client = TelegramClient(StringSession(), api_id, api_hash)

# Web form to input phone number
@app.route('/', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        phone_number = request.form['phone']
        logging.info(f"Received phone number: {phone_number}")

        # Start the client and send the code
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        try:
            loop.run_until_complete(client.connect())
            if not loop.run_until_complete(client.is_user_authorized()):
                loop.run_until_complete(client.send_code_request(phone_number))
                logging.info("Code sent to phone number")
            else:
                # User is already authorized
                me = loop.run_until_complete(client.get_me())
                logging.info(f"Already logged in as {me.first_name} (ID: {me.id})")
                return f"Already logged in as {me.first_name} (ID: {me.id})"
        except Exception as e:
            logging.error(f"Error sending code: {str(e)}")
            return f"Error sending code: {str(e)}"

        # Render form to enter the code
        return '''
            <form action="/verify" method="post">
                Code: <input type="text" name="code"><br>
                <input type="submit" value="Verify">
            </form>
        '''

    # Render form to input phone number
    return '''
        <form method="post">
            Phone number: <input type="text" name="phone"><br>
            <input type="submit" value="Submit">
        </form>
    '''

# Route to verify the code
@app.route('/verify', methods=['POST'])
def verify_code():
    code = request.form['code']
    logging.info(f"Received verification code: {code}")
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        loop.run_until_complete(client.sign_in(code=code))
        me = loop.run_until_complete(client.get_me())
        logging.info(f"Logged in as {me.first_name} (ID: {me.id})")
        # Save the string session
        session_str = client.session.save()
        logging.info(f"String Session: {session_str}")
        return f"Logged in as {me.first_name} (ID: {me.id})"
    except Exception as e:
        logging.error(f"Login failed: {str(e)}")
        return f"Login failed: {str(e)}"

# Route to display logs
@app.route('/logs')
def get_logs():
    log_contents = log_stream.getvalue()
    return f"<pre>{log_contents}</pre>"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)