-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
167 lines (140 loc) · 5.42 KB
/
main.py
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
"""
This script defines a Telegram bot that monitors the uptime status of specified bots and
updates their status in a designated channel.
The bot periodically sends a /start command to each monitored bot and
checks if the bot is responsive. The results are then posted to a channel, and
notifications can be sent if a bot is down.
Modules:
- logging: Configures logging for the script.
- time: Used for sleeping between checks.
- datetime: Used for getting the current time.
- pytz: Used for timezone conversions.
- pyromod: Provides the Client class for interacting with Telegram.
- pyrogram: Used for handling filters and message handlers.
- config: Contains configuration constants like
API_HASH, API_ID, BOTS, MESSAGE_IDS,
CHANNEL_ID, SESSION_STRING, SLEEP_TIME, and GET_NOFIFIED.
Classes:
- Bot: A subclass of pyromod. Client that implements the bot functionality.
Functions:
- status: Responds to the status command to confirm the bot is up and running.
"""
import time
import logging
import datetime
import pytz
from pyrogram import filters
from pyrogram.handlers import MessageHandler
from pyromod import Client
from pyromod.exceptions import ListenerTimeout
from config import (
API_HASH,
API_ID,
BOTS,
MESSAGE_IDS,
CHANNEL_ID,
SESSION_STRING,
SLEEP_TIME,
GET_NOFIFIED,
)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logging.getLogger("pyrogram").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
class Bot(Client): # pylint: disable=too-many-ancestors
"""
A Telegram bot that checks the uptime status of specified bots and
updates the status messages in a channel.
Attributes:
None
Methods:
__init__():
Initializes the Bot client with the given
session string, API ID, and API hash.
run():
Starts the bot and runs the status check loop.
check():
Periodically checks the status of specified bots and
updates the status messages in the channel.
"""
def __init__(self):
"""
Initializes the Bot client with the given session string, API ID, and API hash.
"""
super().__init__(
name="StatusCheckBot",
session_string=SESSION_STRING,
api_id=API_ID,
api_hash=API_HASH,
)
async def start(self):
"""
Starts the bot by invoking the parent class's start method.
This method is an asynchronous override that ensures the bot client
starts correctly, utilizing the parent class's implementation.
"""
await super().start()
def run(self):
"""
Starts the bot and runs the status check loop.
"""
super().run(self.start())
super().run(self.check())
super().run(self.stop())
async def check(self):
"""
Periodically checks the status of specified bots and
updates the status messages in the channel.
"""
self.add_handler(
MessageHandler(
status, filters=(filters.command("status", ".") & filters.me)
)
)
try:
while True:
logger.info("starting to check uptime..")
edit_text = "**🤖 NS BOTS Status** (Updated every 1 hour)\n\n"
for bot in BOTS:
logger.info("checking @%s", bot)
try:
await self.ask(chat_id=bot, text="/start", timeout=10)
except ListenerTimeout:
logger.warning("@%s is down", bot)
edit_text += (
f"__Bot Name:__ @{bot}\n__Bot Status:__ Down ❌\n\n"
)
if GET_NOFIFIED:
await self.send_message("me", f"@{bot} was down")
else:
logger.info("all good with @%s", bot)
edit_text += f"__Bot Name:__ @{bot}\n__Bot Status:__ Up ✅\n\n"
time_now = datetime.datetime.now(pytz.timezone("Asia/Kolkata"))
formatted_time = time_now.strftime("%d %B %Y %I:%M %p")
edit_text += f"__Last checked on {formatted_time} (**IST**)__"
for message_id in MESSAGE_IDS:
await self.edit_message_text(CHANNEL_ID, message_id, edit_text)
time.sleep(5)
logger.info("everything done! sleeping for %s mins...", SLEEP_TIME)
time.sleep(SLEEP_TIME * 60)
except KeyboardInterrupt:
logger.info("Stopping the session....")
async def stop(self, *args, **kwargs):
"""
Stops the bot by invoking the parent class's stop method.
This method is an asynchronous override that ensures the bot client
stops correctly, utilizing the parent class's implementation. It can
also accept additional arguments and keyword arguments if needed.
"""
await super().stop(*args, **kwargs)
async def status(_, message):
"""
Responds to the status command to confirm the bot is up and running.
Args:
_ (Client): The client instance.
message (Message): The incoming message object.
"""
await message.edit(text="Bot is up and running....")
if __name__ == "__main__":
Bot().run()