Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Upload 2 files
#32
by shmsanhafzqaydbdhalwly - opened
- main (2).py +116 -0
- main (3).py +116 -0
main (2).py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from telethon import TelegramClient, events, Button, utils
|
| 2 |
+
from telethon.sessions import StringSession
|
| 3 |
+
import datetime
|
| 4 |
+
import html
|
| 5 |
+
import asyncio
|
| 6 |
+
import os
|
| 7 |
+
from flask import Flask
|
| 8 |
+
import threading
|
| 9 |
+
|
| 10 |
+
# إعدادات البوت - سيتم قراءتها من متغيرات البيئة
|
| 11 |
+
api_id = int(os.environ.get("API_ID", 0))
|
| 12 |
+
api_hash = os.environ.get("API_HASH", "")
|
| 13 |
+
session_string = os.environ.get("SESSION_STRING", "")
|
| 14 |
+
|
| 15 |
+
KEYWORDS = ["يساعدني","يحل", "يحِل", "ابغى", "يسوي", "أبغى", "يساعد"]
|
| 16 |
+
ALERT_TARGET = os.environ.get("ALERT_TARGET", "Shaher8642") # يمكن تغييرها لتكون متغير بيئة أيضاً
|
| 17 |
+
|
| 18 |
+
# استخدام StringSession إذا كانت متوفرة، وإلا فابدأ جلسة جديدة
|
| 19 |
+
client = TelegramClient(StringSession(session_string) if session_string else None, api_id, api_hash)
|
| 20 |
+
|
| 21 |
+
def contains_keyword(text: str) -> bool:
|
| 22 |
+
if not text:
|
| 23 |
+
return False
|
| 24 |
+
t = text.lower()
|
| 25 |
+
return any(kw.lower() in t for kw in KEYWORDS)
|
| 26 |
+
|
| 27 |
+
def build_chat_link(chat, chat_id, message_id):
|
| 28 |
+
if getattr(chat, "username", None):
|
| 29 |
+
return f"https://t.me/{chat.username}/{message_id}"
|
| 30 |
+
try:
|
| 31 |
+
s = str(chat_id)
|
| 32 |
+
if s.startswith("-100"):
|
| 33 |
+
short = s[4:]
|
| 34 |
+
return f"https://t.me/c/{short}/{message_id}"
|
| 35 |
+
except Exception:
|
| 36 |
+
pass
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
@client.on(events.NewMessage(incoming=True))
|
| 40 |
+
async def handler(event):
|
| 41 |
+
try:
|
| 42 |
+
text = event.message.message or ""
|
| 43 |
+
if not text or not contains_keyword(text):
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
chat = await event.get_chat()
|
| 47 |
+
sender = await event.get_sender()
|
| 48 |
+
|
| 49 |
+
sender_name = utils.get_display_name(sender) if sender else "مجهول"
|
| 50 |
+
if sender and getattr(sender, "username", None):
|
| 51 |
+
sender_user_field = f"@{sender.username}"
|
| 52 |
+
sender_link = f"https://t.me/{sender.username}"
|
| 53 |
+
elif sender and getattr(sender, "id", None):
|
| 54 |
+
sender_user_field = f"{sender_name}"
|
| 55 |
+
sender_link = f"tg://user?id={sender.id}"
|
| 56 |
+
else:
|
| 57 |
+
sender_user_field = "لا يوجد"
|
| 58 |
+
sender_link = None
|
| 59 |
+
|
| 60 |
+
chat_title = getattr(chat, "title", None) or getattr(chat, "first_name", None) or "المجموعة"
|
| 61 |
+
chat_id = event.chat_id
|
| 62 |
+
chat_link = build_chat_link(chat, chat_id, event.message.id)
|
| 63 |
+
|
| 64 |
+
when = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 65 |
+
|
| 66 |
+
header = (
|
| 67 |
+
f"📢 رسالة مهمة:\n\n"
|
| 68 |
+
f"👤 المرسل: {html.escape(sender_name)}\n"
|
| 69 |
+
f"🔗 يوزر/رابط المرسل: {sender_user_field}\n"
|
| 70 |
+
f"🏷️ المجموعة: {html.escape(chat_title)}\n"
|
| 71 |
+
f"🔗 رابط الرسالة: {chat_link if chat_link else 'لا يمكن توليد رابط عام'}\n"
|
| 72 |
+
|
| 73 |
+
f"🕒 الوقت: {when}\n\n"
|
| 74 |
+
f"— الرسالة الأصلية ستأتي بعد هذه الملاحظة —"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
buttons = []
|
| 78 |
+
if chat_link:
|
| 79 |
+
buttons.append([Button.url("🔗 الانتقال إلى الرسالة", chat_link)])
|
| 80 |
+
|
| 81 |
+
await client.send_message(ALERT_TARGET, header, buttons=buttons)
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
await client.forward_messages(ALERT_TARGET, event.message)
|
| 85 |
+
except Exception as fe:
|
| 86 |
+
await client.send_message(ALERT_TARGET, "💬 (لم أستطع إعادة توجيه الرسالة — أدرج النص أدناه):\n\n" + text)
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print("Error handling message:", repr(e))
|
| 90 |
+
|
| 91 |
+
# Flask app for keep-alive
|
| 92 |
+
app = Flask(__name__)
|
| 93 |
+
|
| 94 |
+
@app.route("/")
|
| 95 |
+
def home():
|
| 96 |
+
return "I am alive!"
|
| 97 |
+
|
| 98 |
+
def run_flask_app():
|
| 99 |
+
app.run(host="0.0.0.0", port=7860)
|
| 100 |
+
|
| 101 |
+
async def main():
|
| 102 |
+
# Start Flask app in a separate thread
|
| 103 |
+
flask_thread = threading.Thread(target=run_flask_app)
|
| 104 |
+
flask_thread.daemon = True # Allow main program to exit even if thread is running
|
| 105 |
+
flask_thread.start()
|
| 106 |
+
|
| 107 |
+
print("Connecting to Telegram...")
|
| 108 |
+
await client.start()
|
| 109 |
+
print("Userbot started — listening...")
|
| 110 |
+
await client.run_until_disconnected()
|
| 111 |
+
|
| 112 |
+
if __name__ == '__main__':
|
| 113 |
+
try:
|
| 114 |
+
asyncio.run(main())
|
| 115 |
+
except KeyboardInterrupt:
|
| 116 |
+
pass
|
main (3).py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from telethon import TelegramClient, events, Button, utils
|
| 2 |
+
from telethon.sessions import StringSession
|
| 3 |
+
import datetime
|
| 4 |
+
import html
|
| 5 |
+
import asyncio
|
| 6 |
+
import os
|
| 7 |
+
from flask import Flask
|
| 8 |
+
import threading
|
| 9 |
+
|
| 10 |
+
# إعدادات البوت - سيتم قراءتها من متغيرات البيئة
|
| 11 |
+
api_id = int(os.environ.get("API_ID", 0))
|
| 12 |
+
api_hash = os.environ.get("API_HASH", "")
|
| 13 |
+
session_string = os.environ.get("SESSION_STRING", "")
|
| 14 |
+
|
| 15 |
+
KEYWORDS = ["يساعدني","يحل", "يحِل", "ابغى", "يسوي", "أبغى", "يساعد"]
|
| 16 |
+
ALERT_TARGET = os.environ.get("ALERT_TARGET", "Shaher8642") # يمكن تغييرها لتكون متغير بيئة أيضاً
|
| 17 |
+
|
| 18 |
+
# استخدام StringSession إذا كانت متوفرة، وإلا فابدأ جلسة جديدة
|
| 19 |
+
client = TelegramClient(StringSession(session_string) if session_string else None, api_id, api_hash)
|
| 20 |
+
|
| 21 |
+
def contains_keyword(text: str) -> bool:
|
| 22 |
+
if not text:
|
| 23 |
+
return False
|
| 24 |
+
t = text.lower()
|
| 25 |
+
return any(kw.lower() in t for kw in KEYWORDS)
|
| 26 |
+
|
| 27 |
+
def build_chat_link(chat, chat_id, message_id):
|
| 28 |
+
if getattr(chat, "username", None):
|
| 29 |
+
return f"https://t.me/{chat.username}/{message_id}"
|
| 30 |
+
try:
|
| 31 |
+
s = str(chat_id)
|
| 32 |
+
if s.startswith("-100"):
|
| 33 |
+
short = s[4:]
|
| 34 |
+
return f"https://t.me/c/{short}/{message_id}"
|
| 35 |
+
except Exception:
|
| 36 |
+
pass
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
@client.on(events.NewMessage(incoming=True))
|
| 40 |
+
async def handler(event):
|
| 41 |
+
try:
|
| 42 |
+
text = event.message.message or ""
|
| 43 |
+
if not text or not contains_keyword(text):
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
chat = await event.get_chat()
|
| 47 |
+
sender = await event.get_sender()
|
| 48 |
+
|
| 49 |
+
sender_name = utils.get_display_name(sender) if sender else "مجهول"
|
| 50 |
+
if sender and getattr(sender, "username", None):
|
| 51 |
+
sender_user_field = f"@{sender.username}"
|
| 52 |
+
sender_link = f"https://t.me/{sender.username}"
|
| 53 |
+
elif sender and getattr(sender, "id", None):
|
| 54 |
+
sender_user_field = f"{sender_name}"
|
| 55 |
+
sender_link = f"tg://user?id={sender.id}"
|
| 56 |
+
else:
|
| 57 |
+
sender_user_field = "لا يوجد"
|
| 58 |
+
sender_link = None
|
| 59 |
+
|
| 60 |
+
chat_title = getattr(chat, "title", None) or getattr(chat, "first_name", None) or "المجموعة"
|
| 61 |
+
chat_id = event.chat_id
|
| 62 |
+
chat_link = build_chat_link(chat, chat_id, event.message.id)
|
| 63 |
+
|
| 64 |
+
when = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 65 |
+
|
| 66 |
+
header = (
|
| 67 |
+
f"📢 رسالة مهمة:\n\n"
|
| 68 |
+
f"👤 المرسل: {html.escape(sender_name)}\n"
|
| 69 |
+
f"🔗 يوزر/رابط المرسل: {sender_user_field}\n"
|
| 70 |
+
f"🏷️ المجموعة: {html.escape(chat_title)}\n"
|
| 71 |
+
f"🔗 رابط الرسالة: {chat_link if chat_link else 'لا يمكن توليد رابط عام'}\n"
|
| 72 |
+
|
| 73 |
+
f"🕒 الوقت: {when}\n\n"
|
| 74 |
+
f"— الرسالة الأصلية ستأتي بعد هذه الملاحظة —"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
buttons = []
|
| 78 |
+
if chat_link:
|
| 79 |
+
buttons.append([Button.url("🔗 الانتقال إلى الرسالة", chat_link)])
|
| 80 |
+
|
| 81 |
+
await client.send_message(ALERT_TARGET, header, buttons=buttons)
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
await client.forward_messages(ALERT_TARGET, event.message)
|
| 85 |
+
except Exception as fe:
|
| 86 |
+
await client.send_message(ALERT_TARGET, "💬 (لم أستطع إعادة توجيه الرسالة — أدرج النص أدناه):\n\n" + text)
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print("Error handling message:", repr(e))
|
| 90 |
+
|
| 91 |
+
# Flask app for keep-alive
|
| 92 |
+
app = Flask(__name__)
|
| 93 |
+
|
| 94 |
+
@app.route("/")
|
| 95 |
+
def home():
|
| 96 |
+
return "I am alive!"
|
| 97 |
+
|
| 98 |
+
def run_flask_app():
|
| 99 |
+
app.run(host="0.0.0.0", port=7860)
|
| 100 |
+
|
| 101 |
+
async def main():
|
| 102 |
+
# Start Flask app in a separate thread
|
| 103 |
+
flask_thread = threading.Thread(target=run_flask_app)
|
| 104 |
+
flask_thread.daemon = True # Allow main program to exit even if thread is running
|
| 105 |
+
flask_thread.start()
|
| 106 |
+
|
| 107 |
+
print("Connecting to Telegram...")
|
| 108 |
+
await client.start()
|
| 109 |
+
print("Userbot started — listening...")
|
| 110 |
+
await client.run_until_disconnected()
|
| 111 |
+
|
| 112 |
+
if __name__ == '__main__':
|
| 113 |
+
try:
|
| 114 |
+
asyncio.run(main())
|
| 115 |
+
except KeyboardInterrupt:
|
| 116 |
+
pass
|