Update app.py
Browse files
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
|
| 111 |
-
# Define the
|
| 112 |
-
|
| 113 |
-
"
|
| 114 |
-
|
|
|
|
| 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":
|
| 124 |
},
|
| 125 |
timeout=10
|
| 126 |
)
|
| 127 |
-
|
| 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 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()}")
|