o7c1 commited on
Commit
20a6eaf
·
verified ·
1 Parent(s): adefabd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -29
app.py CHANGED
@@ -107,11 +107,12 @@ def process_single_message(chat_id, text):
107
 
108
 
109
  def send_bale_message(chat_id, text):
110
- """Sends a message with a persistent 'New Chat' button."""
111
- # Define the keyboard markup
112
- keyboard = {
113
- "keyboard": [[{"text": "🗑️ New Chat"}]],
114
- "resize_keyboard": True
 
115
  }
116
 
117
  try:
@@ -120,14 +121,14 @@ def send_bale_message(chat_id, text):
120
  json={
121
  "chat_id": chat_id,
122
  "text": text,
123
- "reply_markup": keyboard # <--- This adds the button
124
  },
125
  timeout=10
126
  )
127
- if send_resp.status_code != 200:
128
- logger.error(f"sendMessage failed: {send_resp.text}")
129
  except Exception as e:
130
- logger.error(f"Network error: {e}")
 
131
 
132
  # ---------- Manual polling loop ----------
133
  def poll_updates():
@@ -156,27 +157,37 @@ def poll_updates():
156
  logger.info(f"Received {len(updates)} update(s)")
157
 
158
  for upd in updates:
159
- offset = upd["update_id"] + 1 # Always acknowledge update first
160
- msg = upd.get("message")
161
- if not msg:
162
- continue
 
 
 
163
 
164
- text = msg.get("text")
165
- chat_id = msg.get("chat", {}).get("id")
166
- if not text or not chat_id:
167
- continue
168
-
169
- # Offload AI generation to the executor so the polling loop remains non-blocking
170
- executor.submit(process_single_message, chat_id, text)
171
- if text == "🗑️ New Chat":
172
- # Clear the history for this user
173
- chat_histories[chat_id] = []
174
- logger.info(f"Chat history cleared for {chat_id}")
175
- send_bale_message(chat_id, "Memory cleared! Starting a fresh conversation. How can I help you?")
176
- continue # Skip AI processing for this specific command
177
-
178
- # Otherwise, process as a normal AI request
179
- executor.submit(process_single_message, chat_id, text)
 
 
 
 
 
 
 
180
 
181
  except Exception as e:
182
  logger.error(f"Polling loop error: {traceback.format_exc()}")
 
107
 
108
 
109
  def send_bale_message(chat_id, text):
110
+ """Sends a message with an Inline Button inside the chat bubble area."""
111
+ # Define the Inline Keyboard (inside the chat area)
112
+ inline_keyboard = {
113
+ "inline_keyboard": [[
114
+ {"text": "🗑️ New Chat", "callback_data": "reset_chat"}
115
+ ]]
116
  }
117
 
118
  try:
 
121
  json={
122
  "chat_id": chat_id,
123
  "text": text,
124
+ "reply_markup": inline_keyboard
125
  },
126
  timeout=10
127
  )
128
+ return send_resp
 
129
  except Exception as e:
130
+ logger.error(f"Network error in send_bale_message: {e}")
131
+ return None
132
 
133
  # ---------- Manual polling loop ----------
134
  def poll_updates():
 
157
  logger.info(f"Received {len(updates)} update(s)")
158
 
159
  for upd in updates:
160
+ offset = upd["update_id"] + 1
161
+
162
+ # --- HANDLE INLINE BUTTON CLICKS ---
163
+ if "callback_query" in upd:
164
+ callback = upd["callback_query"]
165
+ chat_id = callback["message"]["chat"]["id"]
166
+ data = callback.get("data")
167
 
168
+ if data == "reset_chat":
169
+ chat_histories[chat_id] = [] # Clear memory
170
+ logger.info(f"Memory reset via inline button for {chat_id}")
171
+
172
+ # Send confirmation
173
+ send_bale_message(chat_id, "✅ Memory cleared! How can I help you now?")
174
+ continue
175
+
176
+ # --- HANDLE NORMAL MESSAGES ---
177
+ msg = upd.get("message")
178
+ if not msg: continue
179
+
180
+ text = msg.get("text")
181
+ chat_id = msg.get("chat", {}).get("id")
182
+ if not text or not chat_id: continue
183
+
184
+ # Standard text command for "New Chat" just in case they type it
185
+ if text == "/reset" or text == "New Chat":
186
+ chat_histories[chat_id] = []
187
+ send_bale_message(chat_id, "✅ Memory cleared!")
188
+ continue
189
+
190
+ executor.submit(process_single_message, chat_id, text)
191
 
192
  except Exception as e:
193
  logger.error(f"Polling loop error: {traceback.format_exc()}")