matrixcoder commited on
Commit
2c84504
Β·
verified Β·
1 Parent(s): f89d2f7

Update bot_with_api.py

Browse files
Files changed (1) hide show
  1. bot_with_api.py +31 -4
bot_with_api.py CHANGED
@@ -5,6 +5,7 @@
5
 
6
  import asyncio
7
  import threading
 
8
  from api import app, command_queue
9
  import sys
10
 
@@ -115,6 +116,28 @@ async def handle_emote(command):
115
  print(f"[EMOTE] βœ— Error: {e}")
116
 
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  # ============================================
119
  # FLASK SERVER THREAD
120
  # ============================================
@@ -129,16 +152,19 @@ def run_flask():
129
  # ============================================
130
 
131
  async def run_bot_with_api():
132
- """Main function - runs bot + API together"""
133
 
134
  # Start Flask API
135
  flask_thread = threading.Thread(target=run_flask, daemon=True)
136
  flask_thread.start()
137
- print("[API] Started on http://0.0.0.0:5000")
138
 
139
  # Start command processor
140
  api_processor = asyncio.create_task(process_api_commands())
141
 
 
 
 
142
  # Start main bot
143
  try:
144
  from main import StarTinG
@@ -146,12 +172,12 @@ async def run_bot_with_api():
146
  print("[BOT] Starting...")
147
  bot_task = asyncio.create_task(StarTinG())
148
 
149
- await asyncio.gather(api_processor, bot_task)
150
 
151
  except ImportError as e:
152
  print(f"[ERROR] Could not import bot: {e}")
153
  print("[API] Running in API-only mode...")
154
- await api_processor
155
 
156
 
157
  # ============================================
@@ -162,6 +188,7 @@ if __name__ == '__main__':
162
  print("""
163
  ╔══════════════════════════════════════════════╗
164
  β•‘ FF Emote Bot - Production Mode β•‘
 
165
  β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
166
 
167
  Starting services...
 
5
 
6
  import asyncio
7
  import threading
8
+ import aiohttp # ADDED for keep-alive
9
  from api import app, command_queue
10
  import sys
11
 
 
116
  print(f"[EMOTE] βœ— Error: {e}")
117
 
118
 
119
+ # ============================================
120
+ # KEEP-ALIVE FOR HUGGING FACE
121
+ # ============================================
122
+
123
+ async def keep_alive():
124
+ """Ping status endpoint every 30 minutes to keep Hugging Face space awake."""
125
+ # Wait a bit for Flask to start
126
+ await asyncio.sleep(30)
127
+ url = "https://matrixcoder-emote-api.hf.space/status"
128
+ while True:
129
+ try:
130
+ async with aiohttp.ClientSession() as session:
131
+ async with session.get(url) as resp:
132
+ if resp.status == 200:
133
+ print("[KEEPALIVE] βœ“ Ping successful")
134
+ else:
135
+ print(f"[KEEPALIVE] βœ— Ping failed (status {resp.status})")
136
+ except Exception as e:
137
+ print(f"[KEEPALIVE] βœ— Error: {e}")
138
+ await asyncio.sleep(1800) # 30 minutes
139
+
140
+
141
  # ============================================
142
  # FLASK SERVER THREAD
143
  # ============================================
 
152
  # ============================================
153
 
154
  async def run_bot_with_api():
155
+ """Main function - runs bot + API together + keep-alive"""
156
 
157
  # Start Flask API
158
  flask_thread = threading.Thread(target=run_flask, daemon=True)
159
  flask_thread.start()
160
+ print("[API] Started on http://0.0.0.0:7860")
161
 
162
  # Start command processor
163
  api_processor = asyncio.create_task(process_api_commands())
164
 
165
+ # Start keep-alive task
166
+ keep_alive_task = asyncio.create_task(keep_alive())
167
+
168
  # Start main bot
169
  try:
170
  from main import StarTinG
 
172
  print("[BOT] Starting...")
173
  bot_task = asyncio.create_task(StarTinG())
174
 
175
+ await asyncio.gather(api_processor, keep_alive_task, bot_task)
176
 
177
  except ImportError as e:
178
  print(f"[ERROR] Could not import bot: {e}")
179
  print("[API] Running in API-only mode...")
180
+ await asyncio.gather(api_processor, keep_alive_task)
181
 
182
 
183
  # ============================================
 
188
  print("""
189
  ╔══════════════════════════════════════════════╗
190
  β•‘ FF Emote Bot - Production Mode β•‘
191
+ β•‘ (Keep-alive active - pings every 30m) β•‘
192
  β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
193
 
194
  Starting services...