-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstop_bot.py
More file actions
37 lines (31 loc) · 1.21 KB
/
stop_bot.py
File metadata and controls
37 lines (31 loc) · 1.21 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
import asyncio
import aiohttp
import os
from dotenv import load_dotenv
load_dotenv()
BOT_TOKEN = os.getenv("BOT_TOKEN")
async def stop_bot():
"""Stop existing bot instances and clear webhook"""
if not BOT_TOKEN:
print("BOT_TOKEN not found in .env file")
return
async with aiohttp.ClientSession() as session:
# Delete webhook
webhook_url = f"https://api.telegram.org/bot{BOT_TOKEN}/deleteWebhook"
async with session.post(webhook_url) as response:
result = await response.json()
if result.get('ok'):
print("Webhook cleared successfully")
else:
print(f"Failed to clear webhook: {result}")
# Get updates to clear pending updates
updates_url = f"https://api.telegram.org/bot{BOT_TOKEN}/getUpdates?offset=-1"
async with session.get(updates_url) as response:
result = await response.json()
if result.get('ok'):
print("Pending updates cleared")
else:
print(f"Failed to clear updates: {result}")
if __name__ == "__main__":
asyncio.run(stop_bot())
print("Bot cleanup completed. You can now start the bot again.")