Spaces:
Runtime error
Runtime error
Delete bot
Browse files- bot/__init__.py +0 -23
- bot/__main__.py +0 -8
- bot/config.py +0 -29
- bot/init.py +0 -39
- bot/main.py +0 -8
- bot/plugins/init.py +0 -1
- bot/plugins/talk.py +0 -69
- bot/server/__init__.py +0 -36
- bot/server/main.py +0 -18
- bot/startup_log.py +0 -35
bot/__init__.py
DELETED
|
@@ -1,23 +0,0 @@
|
|
| 1 |
-
from logging import getLogger
|
| 2 |
-
from hydrogram import Client
|
| 3 |
-
from .config import Telegram
|
| 4 |
-
|
| 5 |
-
logger = getLogger("bot")
|
| 6 |
-
|
| 7 |
-
_kwargs = dict(
|
| 8 |
-
name="bot",
|
| 9 |
-
api_id=Telegram.API_ID,
|
| 10 |
-
api_hash=Telegram.API_HASH,
|
| 11 |
-
plugins={"root": "bot/plugins"},
|
| 12 |
-
sleep_threshold=-1,
|
| 13 |
-
max_concurrent_transmissions=10,
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
if Telegram.SESSION_STRING:
|
| 17 |
-
_kwargs["session_string"] = Telegram.SESSION_STRING
|
| 18 |
-
elif Telegram.BOT_TOKEN:
|
| 19 |
-
_kwargs["bot_token"] = Telegram.BOT_TOKEN
|
| 20 |
-
else:
|
| 21 |
-
raise RuntimeError("Set SESSION_STRING or TELEGRAM_BOT_TOKEN in environment.")
|
| 22 |
-
|
| 23 |
-
TelegramBot = Client(**_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/__main__.py
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
from bot import TelegramBot
|
| 2 |
-
from bot.server import server
|
| 3 |
-
from bot.startup_log import log_startup
|
| 4 |
-
|
| 5 |
-
if __name__ == "__main__":
|
| 6 |
-
TelegramBot.loop.create_task(server.serve())
|
| 7 |
-
TelegramBot.loop.create_task(log_startup())
|
| 8 |
-
TelegramBot.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/config.py
DELETED
|
@@ -1,29 +0,0 @@
|
|
| 1 |
-
from os import environ as env
|
| 2 |
-
|
| 3 |
-
def _to_int_list(s: str):
|
| 4 |
-
if not s:
|
| 5 |
-
return []
|
| 6 |
-
out = []
|
| 7 |
-
for part in s.replace(",", " ").split():
|
| 8 |
-
try:
|
| 9 |
-
out.append(int(part.strip()))
|
| 10 |
-
except:
|
| 11 |
-
pass
|
| 12 |
-
return out
|
| 13 |
-
|
| 14 |
-
class Telegram:
|
| 15 |
-
API_ID = int(env.get("TELEGRAM_API_ID", "0") or "0")
|
| 16 |
-
API_HASH = env.get("TELEGRAM_API_HASH", "")
|
| 17 |
-
SESSION_STRING = env.get("SESSION_STRING", "") # ✅ use this (generated by your gen.py)
|
| 18 |
-
OWNER_ID = int(env.get("OWNER_ID", "0") or "0")
|
| 19 |
-
|
| 20 |
-
# Optional: extra allowlist (space-separated ids)
|
| 21 |
-
ALLOWED_USER_IDS = set(_to_int_list(env.get("ALLOWED_USER_IDS", "")))
|
| 22 |
-
|
| 23 |
-
BOT_USERNAME = env.get("TELEGRAM_BOT_USERNAME", "")
|
| 24 |
-
CHANNEL_ID = int(env.get("TELEGRAM_CHANNEL_ID", "0") or "0")
|
| 25 |
-
|
| 26 |
-
class Server:
|
| 27 |
-
BASE_URL = env.get("BASE_URL", "")
|
| 28 |
-
BIND_ADDRESS = env.get("BIND_ADDRESS", "0.0.0.0")
|
| 29 |
-
PORT = int(env.get("PORT", "7860") or "7860")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/init.py
DELETED
|
@@ -1,39 +0,0 @@
|
|
| 1 |
-
from hydrogram import Client
|
| 2 |
-
from logging import getLogger
|
| 3 |
-
from logging.config import dictConfig
|
| 4 |
-
from .config import Telegram
|
| 5 |
-
|
| 6 |
-
LOGGER_CONFIG_JSON = {
|
| 7 |
-
"version": 1,
|
| 8 |
-
"formatters": {
|
| 9 |
-
"default": {
|
| 10 |
-
"format": "[%(asctime)s][%(name)s][%(levelname)s] -> %(message)s",
|
| 11 |
-
"datefmt": "%d/%m/%Y %H:%M:%S",
|
| 12 |
-
},
|
| 13 |
-
},
|
| 14 |
-
"handlers": {
|
| 15 |
-
"stream_handler": {"class": "logging.StreamHandler", "formatter": "default"},
|
| 16 |
-
},
|
| 17 |
-
"loggers": {
|
| 18 |
-
"uvicorn": {"level": "INFO", "handlers": ["stream_handler"]},
|
| 19 |
-
"uvicorn.error": {"level": "WARNING", "handlers": ["stream_handler"]},
|
| 20 |
-
"bot": {"level": "INFO", "handlers": ["stream_handler"]},
|
| 21 |
-
"hydrogram": {"level": "INFO", "handlers": ["stream_handler"]},
|
| 22 |
-
},
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
dictConfig(LOGGER_CONFIG_JSON)
|
| 26 |
-
logger = getLogger("bot")
|
| 27 |
-
|
| 28 |
-
if not Telegram.SESSION_STRING:
|
| 29 |
-
raise RuntimeError("SESSION_STRING missing. Put it in HF Secrets/Variables.")
|
| 30 |
-
|
| 31 |
-
TelegramBot = Client(
|
| 32 |
-
name="bot",
|
| 33 |
-
api_id=Telegram.API_ID,
|
| 34 |
-
api_hash=Telegram.API_HASH,
|
| 35 |
-
session_string=Telegram.SESSION_STRING,
|
| 36 |
-
plugins={"root": "bot/plugins"},
|
| 37 |
-
sleep_threshold=-1,
|
| 38 |
-
max_concurrent_transmissions=10,
|
| 39 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/main.py
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
from bot import TelegramBot
|
| 2 |
-
from bot.server import server
|
| 3 |
-
from bot.startup_log import log_startup
|
| 4 |
-
|
| 5 |
-
if __name__ == "__main__":
|
| 6 |
-
TelegramBot.loop.create_task(server.serve())
|
| 7 |
-
TelegramBot.loop.create_task(log_startup())
|
| 8 |
-
TelegramBot.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/plugins/init.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
# plugin package
|
|
|
|
|
|
bot/plugins/talk.py
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
from hydrogram import filters
|
| 2 |
-
from hydrogram.types import Message
|
| 3 |
-
from bot import TelegramBot
|
| 4 |
-
from bot.config import Telegram
|
| 5 |
-
|
| 6 |
-
def is_allowed(uid: int) -> bool:
|
| 7 |
-
if Telegram.OWNER_ID and uid == Telegram.OWNER_ID:
|
| 8 |
-
return True
|
| 9 |
-
return uid in Telegram.ALLOWED_USER_IDS
|
| 10 |
-
|
| 11 |
-
@TelegramBot.on_message(filters.command("start"))
|
| 12 |
-
async def start_handler(_, m: Message):
|
| 13 |
-
uid = m.from_user.id if m.from_user else 0
|
| 14 |
-
if not is_allowed(uid):
|
| 15 |
-
return await m.reply_text("❌ Not allowed. Contact owner.")
|
| 16 |
-
await m.reply_text(
|
| 17 |
-
"✅ Bot live.\n"
|
| 18 |
-
"Commands:\n"
|
| 19 |
-
"/ping\n"
|
| 20 |
-
"/id\n"
|
| 21 |
-
"/help\n\n"
|
| 22 |
-
"Ab normal msg bhejo, main reply dunga."
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
@TelegramBot.on_message(filters.command("help"))
|
| 26 |
-
async def help_handler(_, m: Message):
|
| 27 |
-
uid = m.from_user.id if m.from_user else 0
|
| 28 |
-
if not is_allowed(uid):
|
| 29 |
-
return await m.reply_text("❌ Not allowed.")
|
| 30 |
-
await m.reply_text(
|
| 31 |
-
"✅ Help\n"
|
| 32 |
-
"/ping -> pong\n"
|
| 33 |
-
"/id -> your telegram id\n"
|
| 34 |
-
"Send any text -> echo + small talk reply"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
@TelegramBot.on_message(filters.command("ping"))
|
| 38 |
-
async def ping_handler(_, m: Message):
|
| 39 |
-
uid = m.from_user.id if m.from_user else 0
|
| 40 |
-
if not is_allowed(uid):
|
| 41 |
-
return await m.reply_text("❌ Not allowed.")
|
| 42 |
-
await m.reply_text("pong ✅")
|
| 43 |
-
|
| 44 |
-
@TelegramBot.on_message(filters.command("id"))
|
| 45 |
-
async def id_handler(_, m: Message):
|
| 46 |
-
uid = m.from_user.id if m.from_user else 0
|
| 47 |
-
await m.reply_text(f"Your ID: `{uid}`")
|
| 48 |
-
|
| 49 |
-
@TelegramBot.on_message(filters.text & ~filters.command(["start","help","ping","id"]))
|
| 50 |
-
async def talk_handler(_, m: Message):
|
| 51 |
-
uid = m.from_user.id if m.from_user else 0
|
| 52 |
-
if not is_allowed(uid):
|
| 53 |
-
return await m.reply_text("❌ Not allowed.")
|
| 54 |
-
|
| 55 |
-
text = (m.text or "").strip()
|
| 56 |
-
if not text:
|
| 57 |
-
return
|
| 58 |
-
|
| 59 |
-
# ultra-simple “talking” logic (safe, fast)
|
| 60 |
-
low = text.lower()
|
| 61 |
-
if any(x in low for x in ["hi", "hello", "hey"]):
|
| 62 |
-
return await m.reply_text("Haan bol 😄")
|
| 63 |
-
if "kaisa" in low or "kaisi" in low:
|
| 64 |
-
return await m.reply_text("Mast. Tu bata 😄")
|
| 65 |
-
if "bye" in low:
|
| 66 |
-
return await m.reply_text("Bye 👋")
|
| 67 |
-
|
| 68 |
-
# default echo-style reply
|
| 69 |
-
await m.reply_text(f"🗣️ You said: {text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/server/__init__.py
DELETED
|
@@ -1,36 +0,0 @@
|
|
| 1 |
-
from quart import Quart
|
| 2 |
-
from uvicorn import Server as UvicornServer, Config
|
| 3 |
-
from logging import getLogger
|
| 4 |
-
|
| 5 |
-
from bot.config import Server as ServerCfg
|
| 6 |
-
from . import main, error
|
| 7 |
-
|
| 8 |
-
logger = getLogger("uvicorn")
|
| 9 |
-
|
| 10 |
-
instance = Quart(__name__)
|
| 11 |
-
instance.config["RESPONSE_TIMEOUT"] = None
|
| 12 |
-
instance.config["MAX_CONTENT_LENGTH"] = 999999999999999
|
| 13 |
-
|
| 14 |
-
@instance.before_serving
|
| 15 |
-
async def before_serve():
|
| 16 |
-
logger.info("Web server is started!")
|
| 17 |
-
logger.info(f"Server running on {ServerCfg.BIND_ADDRESS}:{ServerCfg.PORT}")
|
| 18 |
-
|
| 19 |
-
# routes
|
| 20 |
-
instance.register_blueprint(main.bp)
|
| 21 |
-
|
| 22 |
-
# errors
|
| 23 |
-
instance.register_error_handler(400, error.invalid_request)
|
| 24 |
-
instance.register_error_handler(404, error.not_found)
|
| 25 |
-
instance.register_error_handler(405, error.invalid_method)
|
| 26 |
-
instance.register_error_handler(error.HTTPError, error.http_error)
|
| 27 |
-
|
| 28 |
-
# uvicorn server (no custom log_config)
|
| 29 |
-
server = UvicornServer(
|
| 30 |
-
Config(
|
| 31 |
-
app=instance,
|
| 32 |
-
host=ServerCfg.BIND_ADDRESS,
|
| 33 |
-
port=ServerCfg.PORT,
|
| 34 |
-
log_level="info",
|
| 35 |
-
)
|
| 36 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/server/main.py
DELETED
|
@@ -1,18 +0,0 @@
|
|
| 1 |
-
from quart import Blueprint, jsonify
|
| 2 |
-
from bot.startup_log import uptime_s
|
| 3 |
-
from bot import TelegramBot
|
| 4 |
-
|
| 5 |
-
bp = Blueprint("main", __name__)
|
| 6 |
-
|
| 7 |
-
@bp.route("/")
|
| 8 |
-
async def home():
|
| 9 |
-
return "OK: talking bot + web server", 200
|
| 10 |
-
|
| 11 |
-
@bp.route("/health")
|
| 12 |
-
async def health():
|
| 13 |
-
# simple health + uptime + connected state
|
| 14 |
-
return jsonify({
|
| 15 |
-
"ok": True,
|
| 16 |
-
"uptime_s": uptime_s(),
|
| 17 |
-
"connected": bool(getattr(TelegramBot, "is_connected", False))
|
| 18 |
-
}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot/startup_log.py
DELETED
|
@@ -1,35 +0,0 @@
|
|
| 1 |
-
import platform
|
| 2 |
-
import time
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
from hydrogram import __version__ as hydrogram_version
|
| 5 |
-
from bot import TelegramBot
|
| 6 |
-
from bot.config import Telegram, Server
|
| 7 |
-
|
| 8 |
-
START_TS = time.time()
|
| 9 |
-
|
| 10 |
-
def uptime_s() -> int:
|
| 11 |
-
return int(time.time() - START_TS)
|
| 12 |
-
|
| 13 |
-
async def log_startup():
|
| 14 |
-
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 15 |
-
print(f"\n===== STARTUP SNAPSHOT @ {now} =====", flush=True)
|
| 16 |
-
print(f"Python: {platform.python_version()} | System: {platform.platform()}", flush=True)
|
| 17 |
-
print(f"Hydrogram: {hydrogram_version}", flush=True)
|
| 18 |
-
|
| 19 |
-
print(f"API_ID: {Telegram.API_ID}", flush=True)
|
| 20 |
-
print(f"BOT_USERNAME env: {Telegram.BOT_USERNAME}", flush=True)
|
| 21 |
-
print(f"OWNER_ID: {Telegram.OWNER_ID}", flush=True)
|
| 22 |
-
print(f"ALLOWED_USER_IDS count: {len(Telegram.ALLOWED_USER_IDS)}", flush=True)
|
| 23 |
-
print(f"BASE_URL: {Server.BASE_URL} | BIND: {Server.BIND_ADDRESS}:{Server.PORT}", flush=True)
|
| 24 |
-
|
| 25 |
-
# Bot identity test
|
| 26 |
-
try:
|
| 27 |
-
me = await TelegramBot.get_me()
|
| 28 |
-
print("\n--- BOT IDENTITY ---", flush=True)
|
| 29 |
-
print(f"id={me.id}", flush=True)
|
| 30 |
-
print(f"username=@{me.username}", flush=True)
|
| 31 |
-
print(f"name={me.first_name}", flush=True)
|
| 32 |
-
except Exception as e:
|
| 33 |
-
print(f"get_me_failed: {type(e).__name__}: {e}", flush=True)
|
| 34 |
-
|
| 35 |
-
print("===== END SNAPSHOT =====\n", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|