From 8b8a5ee201227bf1fbb2c531f5c4bf30f6fb40b1 Mon Sep 17 00:00:00 2001 From: Shihaam Abdul Rahman Date: Fri, 21 Feb 2025 16:45:58 +0500 Subject: [PATCH] working --- .env.example | 5 +++ .gitignore | 2 + listen.py | 113 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 0 4 files changed, 120 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 listen.py create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ce3875a --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +API_ID= +API_HASH= +CHAT_ID= +WEB_USERNAME=changeme +WEB_PASSWORD=changeme diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21f5555 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +listen.session diff --git a/listen.py b/listen.py new file mode 100644 index 0000000..327697c --- /dev/null +++ b/listen.py @@ -0,0 +1,113 @@ +import os +from dotenv import load_dotenv +from pyrogram import Client, filters +from flask import Flask, render_template_string, request, Response + +# Load environment variables from .env file +load_dotenv() + +# Get the API ID, API Hash, and Chat ID from the .env file +api_id = os.getenv("API_ID") +api_hash = os.getenv("API_HASH") +chat_id = int(os.getenv("CHAT_ID")) +web_username = os.getenv("WEB_USERNAME") +web_password = os.getenv("WEB_PASSWORD") + +# Create a Pyrogram Client +app = Client("listen", api_id=api_id, api_hash=api_hash) + +# Create a Flask app +flask_app = Flask(__name__) + +# Store messages in a list +messages = [] + +# Define a message handler for the specific chat +@app.on_message(filters.chat(chat_id)) +def handle_message(client, message): + # Check if the message contains the specific string + if "[SIM2(Dhiraagu) Receive SMS]" in message.text: + # Replace newlines with
tags for HTML rendering + formatted_message = message.text.replace("\n", "
") + # Add the formatted message to the list + messages.append(formatted_message) + # Keep only the last 50 messages (optional) + if len(messages) > 50: + messages.pop(0) + +# Basic authentication decorator +def check_auth(username, password): + return username == web_username and password == web_password + +def authenticate(): + return Response( + "Unauthorized. Please log in.", 401, + {"WWW-Authenticate": 'Basic realm="Login Required"'} + ) + +# Flask route to display messages +@flask_app.route("/") +def index(): + auth = request.authorization + if not auth or not check_auth(auth.username, auth.password): + return authenticate() + return render_template_string(""" + + + + + + Telegram Messages + + + +
+ {% for message in messages %} +
{{ message | safe }}
+ {% endfor %} +
+ + + """, messages=messages) + +# Run the Flask app and Pyrogram client +if __name__ == "__main__": + import threading + # Start the Flask app in a separate thread + flask_thread = threading.Thread(target=lambda: flask_app.run(host="0.0.0.0", port=5000)) + flask_thread.daemon = True + flask_thread.start() + + # Start the Pyrogram client + print(f"Listening to chat ID {chat_id} and filtering for '[SIM2(Dhiraagu) Receive SMS]'...") + app.run() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29