-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_troubleshooting.py
More file actions
executable file
·173 lines (149 loc) · 6.43 KB
/
telegram_troubleshooting.py
File metadata and controls
executable file
·173 lines (149 loc) · 6.43 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
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
#!/usr/bin/env python3
"""
Telegram Troubleshooting Script
Run this script when your Telegram bot is not responding
"""
import subprocess
import json
import sys
import os
def run_command(command, description):
"""Run a shell command and return the output"""
print(f"\n🔍 {description}")
print(f" Command: {command}")
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode == 0:
print(" ✅ Success")
return result.stdout.strip()
else:
print(f" ❌ Failed with exit code {result.returncode}")
if result.stderr:
print(f" Error: {result.stderr}")
return None
except Exception as e:
print(f" ❌ Exception: {e}")
return None
def check_docker_status():
"""Check if Docker containers are running"""
print("\n🐳 Checking Docker container status...")
output = run_command("docker-compose ps", "List running containers")
if output:
print(output)
return output
def check_backend_health():
"""Check if backend services are healthy"""
print("\n🏥 Checking backend health...")
output = run_command("curl -s http://localhost:8000/health", "Check API health endpoint")
if output:
try:
health_data = json.loads(output)
print(f" Status: {health_data.get('status', 'Unknown')}")
for service, status in health_data.get('services', {}).items():
print(f" {service}: {status}")
return health_data
except json.JSONDecodeError:
print(f" Could not parse health data: {output}")
return None
def check_telegram_status():
"""Check Telegram webhook status"""
print("\n📱 Checking Telegram status...")
output = run_command("curl -s http://localhost:8000/admin/telegram-status", "Check Telegram webhook status")
if output:
try:
telegram_data = json.loads(output)
print(f" Webhook Status: {telegram_data.get('status', 'Unknown')}")
print(f" Webhook URL: {telegram_data.get('webhook_url', 'Not set')}")
print(f" Pending Updates: {telegram_data.get('pending_update_count', 'Unknown')}")
return telegram_data
except json.JSONDecodeError:
print(f" Could not parse Telegram status: {output}")
return None
def check_ngrok_status():
"""Check Ngrok tunnel status"""
print("\n🔗 Checking Ngrok status...")
output = run_command("curl -s http://localhost:4040/api/tunnels", "Check Ngrok tunnels")
if output:
try:
ngrok_data = json.loads(output)
tunnels = ngrok_data.get('tunnels', [])
if tunnels:
for tunnel in tunnels:
print(f" Tunnel: {tunnel.get('name', 'Unknown')}")
print(f" Public URL: {tunnel.get('public_url', 'Unknown')}")
print(f" Proto: {tunnel.get('proto', 'Unknown')}")
else:
print(" No active tunnels found")
return ngrok_data
except json.JSONDecodeError:
print(f" Could not parse Ngrok status: {output}")
return None
def setup_telegram_webhook(ngrok_url=None):
"""Set up Telegram webhook"""
if not ngrok_url:
# Try to get ngrok URL from running tunnels
ngrok_output = run_command("curl -s http://localhost:4040/api/tunnels", "Get Ngrok URL")
if ngrok_output:
try:
ngrok_data = json.loads(ngrok_output)
tunnels = ngrok_data.get('tunnels', [])
if tunnels:
ngrok_url = tunnels[0].get('public_url')
except json.JSONDecodeError:
pass
if ngrok_url:
webhook_url = f"{ngrok_url}/webhook/telegram"
print(f"\n🔧 Setting up Telegram webhook to: {webhook_url}")
command = f'curl -s -X POST http://localhost:8000/admin/setup-webhook -H "Content-Type: application/json" -d \'{{"webhook_url": "{webhook_url}"}}\''
output = run_command(command, "Set Telegram webhook")
if output:
print(f" Response: {output}")
return output
else:
print("\n❌ Could not determine Ngrok URL. Please start ngrok or provide URL manually.")
return None
def main():
"""Main troubleshooting function"""
print("🤖 NLP Order Chatbot - Telegram Troubleshooting")
print("=" * 50)
# Check Docker status
docker_status = check_docker_status()
# Check backend health
health_status = check_backend_health()
# Check Telegram status
telegram_status = check_telegram_status()
# Check Ngrok status
ngrok_status = check_ngrok_status()
# Summary and recommendations
print("\n📋 SUMMARY AND RECOMMENDATIONS")
print("=" * 30)
issues_found = []
if not docker_status or "Exit" in docker_status:
issues_found.append("Docker containers are not running properly")
if health_status and health_status.get('status') != 'healthy':
issues_found.append("Backend services are not healthy")
if telegram_status and telegram_status.get('status') != 'configured':
issues_found.append("Telegram webhook is not configured")
if not ngrok_status or not ngrok_status.get('tunnels'):
issues_found.append("Ngrok tunnel is not running")
if issues_found:
print("\n⚠️ ISSUES FOUND:")
for i, issue in enumerate(issues_found, 1):
print(f" {i}. {issue}")
print("\n🔧 AUTOMATIC FIXES:")
if "Telegram webhook is not configured" in issues_found:
print(" Setting up Telegram webhook...")
setup_telegram_webhook()
else:
print("\n✅ All systems are running correctly!")
print(" If Telegram is still not responding, try:")
print(" 1. Restarting ngrok: ngrok http 8000")
print(" 2. Updating the webhook URL with the new ngrok URL")
print(" 3. Sending /start to your bot on Telegram")
print("\n💡 TIPS FOR FUTURE TROUBLESHOOTING:")
print(" • Check Docker logs: docker-compose logs backend")
print(" • Check Telegram status: curl http://localhost:8000/admin/telegram-status")
print(" • Check health: curl http://localhost:8000/health")
print(" • Restart services: docker-compose restart")
if __name__ == "__main__":
main()