-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
110 lines (92 loc) · 4.22 KB
/
Copy pathbot.py
File metadata and controls
110 lines (92 loc) · 4.22 KB
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
import os
from telebot import TeleBot
from dotenv import load_dotenv
import database as db
load_dotenv()
TOKEN = os.getenv("TELEGRAM_TOKEN")
bot = TeleBot(TOKEN, parse_mode="HTML")
# --- Start ---
@bot.message_handler(commands=["start"])
def start(message):
bot.reply_to(message, "Bot is counting messages now")
db.init_db()
# --- /stats_day ---
@bot.message_handler(commands=["stats_day"])
def stats_today(message):
stats = db.get_stats_today(message.chat.id)
if not stats:
bot.reply_to(message, "Сегодня пока нет сообщений.\n There were no messages today")
return
text = "\n".join(f"{row['username']}: {row['total']}" for row in stats)
bot.reply_to(message, f"📊 Сообщения за сегодня/Messages today:\n{text}")
# --- /stats_prev_month ---
@bot.message_handler(commands=["stats_prev_month"])
def stats_month(message):
stats1 = db.get_stats_prev_month(message.chat.id)
if not stats1:
bot.reply_to(message, "No messages were sent previous month\nНичего не было отправлено в прошлом месяце")
return
text = "\n".join(f"{row['username']}: {row['total']}" for row in stats1)
bot.reply_to(message, f"📊 Сообщения за прошлый месяц/Messages previous month:\n{text}")
# --- /stats_month ---
@bot.message_handler(commands=["stats_month"])
def stats_month(message):
stats1 = db.get_stats_month(message.chat.id)
if not stats1:
bot.reply_to(message, "No messages were sent this month\nНичего не было отправлено в этом месяце")
return
text = "\n".join(f"{row['username']}: {row['total']}" for row in stats1)
bot.reply_to(message, f"📊 Сообщения за месяц/Messages this month:\n{text}")
# --- /stats_prev_week ---
@bot.message_handler(commands=["stats_prev_week"])
def stats_week(message):
stats1 = db.get_stats_prev_week(message.chat.id)
if not stats1:
bot.reply_to(message, "No messages were sent previous week\nНичего не было отправлено на прошлой неделе")
return
text = "\n".join(f"{row['username']}: {row['total']}" for row in stats1)
bot.reply_to(message, f"📊 Сообщения за прошлую неделю/Messages previous week:\n{text}")
# --- /stats_week ---
@bot.message_handler(commands=["stats_week"])
def stats_week(message):
stats1 = db.get_stats_week(message.chat.id)
if not stats1:
bot.reply_to(message, "No messages were sent this week\nНичего не было отправлено на этой неделе")
return
text = "\n".join(f"{row['username']}: {row['total']}" for row in stats1)
bot.reply_to(message, f"📊 Сообщения за эту неделю/Messages this week:\n{text}")
# --- /info ---
@bot.message_handler(commands=["info"])
def showing_info(message):
bot.reply_to(message, "Author/Автор: " + "[SlpPan4](https://github.com/SlpPan4)\n" +
"Bot was custom-made for my friend\n" + "If you notice some bugs/errors, please contact me on Github", parse_mode ="Markdown")
# --- collect all messages in the group ---
@bot.message_handler(content_types=[
"text","photo","sticker","animation","video","voice","document","audio","video_note"
])
def count_message(message):
try:
# counts only in groups or supergroups
if message.chat.type not in ("group", "supergroup"):
return
# if message is empty - skip
if not message.from_user:
return
# getting the text or the caption of the message
text = message.text or message.caption or ""
# not counting if the message is a command
if text and text.strip().startswith("/"):
return
#
username = message.from_user.username or message.from_user.full_name
db.add_message(
user_id=message.from_user.id,
username=username,
chat_id=message.chat.id
)
print(f"COUNTED: {username} in chat {message.chat.id} type={message.content_type}")
except Exception as e:
# log
print("count_message exception:", e)
print("Bot is running...")
bot.infinity_polling()