File size: 8,494 Bytes
39d89da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import telebot
import json
import os
import threading
import time
import random
import logging
from flask import Flask

# ุฅุนุฏุงุฏ ุงู„ุณุฌู„ุงุช
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# ============ ุฎุงุฏู… ุงู„ูˆูŠุจ ู„ุถู…ุงู† ุนู…ู„ ุงู„ุจูˆุช 24 ุณุงุนุฉ ============
app = Flask('')

@app.route('/')
def home():
    return "Bot is running and alive!"

def run_web():
    # Hugging Face Spaces ุชุณุชุฎุฏู… ุงู„ู…ู†ูุฐ 7860 ุงูุชุฑุงุถูŠุงู‹
    app.run(host='0.0.0.0', port=7860)

# ุชุดุบูŠู„ ุงู„ุณูŠุฑูุฑ ููŠ ุงู„ุฎู„ููŠุฉ
threading.Thread(target=run_web, daemon=True).start()

# ============ ุงู„ุฅุนุฏุงุฏุงุช ============
TOKEN = os.getenv("TOKEN", "8615712682:AAFYFQM7NYxVhAuKdA-4XcK6o_irjX32UzA")

if not TOKEN or TOKEN == "1234567890:ABCdefGHIjklMNOpqrSTUvwxYZ_example":
    logging.error("โŒ ู„ู… ูŠุชู… ุงู„ุนุซูˆุฑ ุนู„ู‰ ุงู„ุชูˆูƒู†! ุฃุถู TOKEN ููŠ Environment Variables")
    exit(1)

DATA_FILE = "user_data.json"

# ============ ุงู„ู…ุญุชูˆู‰ ============
MESSAGES = [
    {"text": "๏ดฟ ุฅูู†ูŽู‘ ู…ูŽุนูŽ ุงู„ู’ุนูุณู’ุฑู ูŠูุณู’ุฑู‹ุง ๏ดพ\n\n[ุณูˆุฑุฉ ุงู„ุดุฑุญ: 6]", "source": "ู‚ุฑุขู† ูƒุฑูŠู…"},
    {"text": "๏ดฟ ูˆูŽู‚ูู„ ุฑูŽู‘ุจูู‘ ุฒูุฏู’ู†ููŠ ุนูู„ู’ู…ู‹ุง ๏ดพ\n\n[ุณูˆุฑุฉ ุทู‡: 114]", "source": "ู‚ุฑุขู† ูƒุฑูŠู…"},
    {"text": "๏ดฟ ุฃูŽู„ูŽุง ุจูุฐููƒู’ุฑู ุงู„ู„ูŽู‘ู‡ู ุชูŽุทู’ู…ูŽุฆูู†ูู‘ ุงู„ู’ู‚ูู„ููˆุจู ๏ดพ\n\n[ุณูˆุฑุฉ ุงู„ุฑุนุฏ: 28]", "source": "ู‚ุฑุขู† ูƒุฑูŠู…"},
    {"text": "๏ดฟ ูˆูŽู…ูŽู† ูŠูŽุชูŽูˆูŽูƒูŽู‘ู„ู’ ุนูŽู„ูŽู‰ ุงู„ู„ูŽู‘ู‡ู ููŽู‡ููˆูŽ ุญูŽุณู’ุจูู‡ู ๏ดพ\n\n[ุณูˆุฑุฉ ุงู„ุทู„ุงู‚: 3]", "source": "ู‚ุฑุขู† ูƒุฑูŠู…"},
    {"text": "๏ดฟ ููŽุงุฐู’ูƒูุฑููˆู†ููŠ ุฃูŽุฐู’ูƒูุฑู’ูƒูู…ู’ ูˆูŽุงุดู’ูƒูุฑููˆุง ู„ููŠ ูˆูŽู„ูŽุง ุชูŽูƒู’ููุฑููˆู†ู ๏ดพ\n\n[ุณูˆุฑุฉ ุงู„ุจู‚ุฑุฉ: 152]", "source": "ู‚ุฑุขู† ูƒุฑูŠู…"},
    {"text": "ุงู„ู„ูŽู‘ู‡ูู…ูŽู‘ ุฅูู†ูู‘ูŠ ุฃูŽุณู’ุฃูŽู„ููƒูŽ ุงู„ู’ู‡ูุฏูŽู‰ ูˆูŽุงู„ุชูู‘ู‚ูŽู‰ ูˆูŽุงู„ู’ุนูŽููŽุงููŽ ูˆูŽุงู„ู’ุบูู†ูŽู‰", "source": "ุฏุนุงุก"},
    {"text": "ุงู„ู„ูŽู‘ู‡ูู…ูŽู‘ ุฃูŽุนูู†ูู‘ูŠ ุนูŽู„ูŽู‰ ุฐููƒู’ุฑููƒูŽ ูˆูŽุดููƒู’ุฑููƒูŽ ูˆูŽุญูุณู’ู†ู ุนูุจูŽุงุฏูŽุชููƒูŽ", "source": "ุฏุนุงุก"},
    {"text": "ุฑูŽุจูŽู‘ู†ูŽุง ุขุชูู†ูŽุง ูููŠ ุงู„ุฏูู‘ู†ู’ูŠูŽุง ุญูŽุณูŽู†ูŽุฉู‹ ูˆูŽูููŠ ุงู„ุขุฎูุฑูŽุฉู ุญูŽุณูŽู†ูŽุฉู‹ ูˆูŽู‚ูู†ูŽุง ุนูŽุฐูŽุงุจูŽ ุงู„ู†ูŽู‘ุงุฑู", "source": "ุฏุนุงุก"},
    {"text": "ุงู„ู„ูŽู‘ู‡ูู…ูŽู‘ ุตูŽู„ูู‘ ูˆูŽุณูŽู„ูู‘ู…ู’ ุนูŽู„ูŽู‰ ู†ูŽุจููŠูู‘ู†ูŽุง ู…ูุญูŽู…ูŽู‘ุฏู", "source": "ุตู„ุงุฉ ุนู„ู‰ ุงู„ู†ุจูŠ"},
    {"text": "ุณูุจู’ุญูŽุงู†ูŽ ุงู„ู„ูŽู‘ู‡ู ูˆูŽุจูุญูŽู…ู’ุฏูู‡ูุŒ ุณูุจู’ุญูŽุงู†ูŽ ุงู„ู„ูŽู‘ู‡ู ุงู„ู’ุนูŽุธููŠู…ู", "source": "ุฐูƒุฑ"},
    {"text": "ู„ูŽุง ุฅูู„ูŽู‡ูŽ ุฅูู„ูŽู‘ุง ุงู„ู„ูŽู‘ู‡ู ูˆูŽุญู’ุฏูŽู‡ู ู„ูŽุง ุดูŽุฑููŠูƒูŽ ู„ูŽู‡ูุŒ ู„ูŽู‡ู ุงู„ู’ู…ูู„ู’ูƒู ูˆูŽู„ูŽู‡ู ุงู„ู’ุญูŽู…ู’ุฏู ูˆูŽู‡ููˆูŽ ุนูŽู„ูŽู‰ ูƒูู„ูู‘ ุดูŽูŠู’ุกู ู‚ูŽุฏููŠุฑูŒ", "source": "ุฐูƒุฑ"},
    {"text": "ุฃูŽุณู’ุชูŽุบู’ููุฑู ุงู„ู„ูŽู‘ู‡ูŽ ุงู„ู’ุนูŽุธููŠู…ูŽ ูˆูŽุฃูŽุชููˆุจู ุฅูู„ูŽูŠู’ู‡ู", "source": "ุงุณุชุบูุงุฑ"},
]

INTERVAL = 900  # 15 ุฏู‚ูŠู‚ุฉ

bot = telebot.TeleBot(TOKEN)


# ============ ุญูุธ/ุชุญู…ูŠู„ ุงู„ู…ุฌู…ูˆุนุงุช ============
def load_groups() -> list:
    if os.path.exists(DATA_FILE):
        try:
            with open(DATA_FILE, "r", encoding="utf-8") as f:
                return json.load(f)
        except json.JSONDecodeError:
            return []
    return []


def save_groups(groups: list):
    try:
        with open(DATA_FILE, "w", encoding="utf-8") as f:
            json.dump(groups, f, ensure_ascii=False, indent=4)
    except Exception as e:
        logging.error(f"ุฎุทุฃ ููŠ ุญูุธ ุงู„ุจูŠุงู†ุงุช: {e}")


def add_group(chat_id: int, title: str):
    groups = load_groups()
    for g in groups:
        if g["chat_id"] == chat_id:
            return
    groups.append({"chat_id": chat_id, "title": title})
    save_groups(groups)
    logging.info(f"โœ… ุชู…ุช ุฅุถุงูุฉ ุงู„ู…ุฌู…ูˆุนุฉ: {title} ({chat_id})")


def remove_group(chat_id: int):
    groups = load_groups()
    groups = [g for g in groups if g["chat_id"] != chat_id]
    save_groups(groups)
    logging.info(f"๐Ÿ—‘๏ธ ุชู…ุช ุฅุฒุงู„ุฉ ุงู„ู…ุฌู…ูˆุนุฉ: {chat_id}")


# ============ ุงู„ุฅุฑุณุงู„ ุงู„ุชู„ู‚ุงุฆูŠ ูƒู„ 15 ุฏู‚ูŠู‚ุฉ ============
def auto_send_loop():
    time.sleep(10)
    while True:
        logging.info("โฐ ุจุฏุก ุฏูˆุฑุฉ ุงู„ุฅุฑุณุงู„ ุงู„ุชู„ู‚ุงุฆูŠ...")
        groups = load_groups()
        logging.info(f"ุนุฏุฏ ุงู„ู…ุฌู…ูˆุนุงุช: {len(groups)}")

        for g in groups:
            chat_id = g["chat_id"]
            title = g.get("title", "ู…ุฌู…ูˆุนุฉ")
            msg = random.choice(MESSAGES)
            try:
                text = f"{msg['text']}\n\nโ€” {msg['source']} ๐ŸŒ™"
                bot.send_message(chat_id=chat_id, text=text)
                logging.info(f"โœ… ุชู… ุงู„ุฅุฑุณุงู„ ุฅู„ู‰ {title}")
            except Exception as e:
                logging.error(f"โŒ ูุดู„ ุงู„ุฅุฑุณุงู„ ุฅู„ู‰ {title}: {e}")

        logging.info(f"๐Ÿ˜ด ุงู„ุงู†ุชุธุงุฑ {INTERVAL} ุซุงู†ูŠุฉ (15 ุฏู‚ูŠู‚ุฉ)...")
        time.sleep(INTERVAL)


threading.Thread(target=auto_send_loop, daemon=True).start()


# ============ ู…ุนุงู„ุฌุงุช ุชูŠู„ูŠุฌุฑุงู… ============
@bot.message_handler(commands=['start'])
def start(message):
    if message.chat.type == "private":
        bot.reply_to(
            message,
            "๐Ÿค– ุจูˆุช ุงู„ุฃุฏุนูŠุฉ ูˆุงู„ู‚ุฑุขู†\n\n"
            "ุฃุถูู†ูŠ ุฅู„ู‰ ู…ุฌู…ูˆุนุชูƒ ูˆุณุฃุฑุณู„ ุขูŠุฉ ุฃูˆ ุฏุนุงุก ูƒู„ 15 ุฏู‚ูŠู‚ุฉ ุชู„ู‚ุงุฆูŠุงู‹.\n\n"
            "ุดูƒุฑุงู‹ ู„ุงุณุชุฎุฏุงู…ูƒ ุงู„ุจูˆุช."
        )


@bot.message_handler(content_types=['new_chat_members'])
def on_new_chat_members(message):
    """ุนู†ุฏ ุฅุถุงูุฉ ุงู„ุจูˆุช ู„ู…ุฌู…ูˆุนุฉ ุฌุฏูŠุฏุฉุŒ ูŠุณุฌู„ู‡ุง ุชู„ู‚ุงุฆูŠุงู‹."""
    for member in message.new_chat_members:
        if member.id == bot.get_me().id:
            add_group(message.chat.id, message.chat.title or "ู…ุฌู…ูˆุนุฉ")
            try:
                bot.send_message(
                    message.chat.id,
                    "๐Ÿค– ุฃู‡ู„ุงู‹ ุจูƒู…!\n\nุณุฃุฑุณู„ ู„ูƒู… ุขูŠุฉ ุฃูˆ ุฏุนุงุก ูƒู„ 15 ุฏู‚ูŠู‚ุฉ ุชู„ู‚ุงุฆูŠุงู‹ ุจุฅุฐู† ุงู„ู„ู‡.\n\n"
                    "โ€” ุงู„ุจูˆุช ุฃุฌุฑ ู„ูŠ ูˆู„ูƒู… ๐ŸŒ™"
                )
            except Exception as e:
                logging.error(f"ูุดู„ ุฅุฑุณุงู„ ุฑุณุงู„ุฉ ุงู„ุชุฑุญูŠุจ: {e}")


@bot.message_handler(content_types=['left_chat_member'])
def on_left_chat_member(message):
    """ุนู†ุฏ ุฅุฒุงู„ุฉ ุงู„ุจูˆุช ู…ู† ุงู„ู…ุฌู…ูˆุนุฉุŒ ูŠุญุฐูู‡ุง ู…ู† ุงู„ู‚ุงุฆู…ุฉ."""
    if message.left_chat_member.id == bot.get_me().id:
        remove_group(message.chat.id)


@bot.message_handler(commands=['stop'])
def stop_cmd(message):
    """ุฃู…ุฑ /stop ุฏุงุฎู„ ุงู„ู…ุฌู…ูˆุนุฉ ู„ุฅูŠู‚ุงู ุงู„ุฅุฑุณุงู„ ู„ู‡ุฐูŠ ุงู„ู…ุฌู…ูˆุนุฉ."""
    if message.chat.type in ["group", "supergroup"]:
        try:
            member = bot.get_chat_member(message.chat.id, message.from_user.id)
            if member.status not in ["administrator", "creator"]:
                return
            remove_group(message.chat.id)
            bot.reply_to(message, "๐Ÿ›‘ ุชู… ุฅูŠู‚ุงู ุงู„ุฅุฑุณุงู„ ููŠ ู‡ุฐูŠ ุงู„ู…ุฌู…ูˆุนุฉ.")
        except Exception as e:
            logging.error(f"ุฎุทุฃ ููŠ /stop: {e}")


@bot.message_handler(commands=['resume'])
def resume_cmd(message):
    """ุฃู…ุฑ /resume ุฏุงุฎู„ ุงู„ู…ุฌู…ูˆุนุฉ ู„ุงุณุชุฆู†ุงู ุงู„ุฅุฑุณุงู„."""
    if message.chat.type in ["group", "supergroup"]:
        try:
            member = bot.get_chat_member(message.chat.id, message.from_user.id)
            if member.status not in ["administrator", "creator"]:
                return
            add_group(message.chat.id, message.chat.title or "ู…ุฌู…ูˆุนุฉ")
            bot.reply_to(message, "โœ… ุชู… ุชุดุบูŠู„ ุงู„ุฅุฑุณุงู„ ู…ู† ุฌุฏูŠุฏ.")
        except Exception as e:
            logging.error(f"ุฎุทุฃ ููŠ /resume: {e}")


if __name__ == "__main__":
    if not os.path.exists(DATA_FILE):
        save_groups([])
    logging.info("๐Ÿš€ ุจุฏุก ุชุดุบูŠู„ ุงู„ุจูˆุช...")
    while True:
        try:
            bot.polling(none_stop=True, interval=0, timeout=20)
        except Exception as e:
            logging.error(f"โš ๏ธ ุฎุทุฃ: {e}")
            time.sleep(5)
            logging.info("ุฅุนุงุฏุฉ ุชุดุบูŠู„ ุงู„ุจูˆุช...")