mickeywu520 commited on
Commit
81a7334
·
1 Parent(s): 9e4ca7c

add test command

Browse files
Files changed (1) hide show
  1. backend/services/line_bot_service.py +113 -1
backend/services/line_bot_service.py CHANGED
@@ -33,6 +33,10 @@ class LineBotService:
33
  # 記錄用戶訊息
34
  logger.info(f"收到來自用戶 {user_id} 的訊息: {message_text}")
35
 
 
 
 
 
36
  # 檢查是否為特殊指令
37
  if message_text.lower() in ['help', '幫助', '說明', '指令']:
38
  return self._handle_help_command(user_id)
@@ -243,4 +247,112 @@ class LineBotService:
243
  return {
244
  "success": False,
245
  "error": str(e)
246
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # 記錄用戶訊息
34
  logger.info(f"收到來自用戶 {user_id} 的訊息: {message_text}")
35
 
36
+ # 🔧 後門測試:直接與 DeepSeek V3 聊天
37
+ if message_text.startswith('##test##'):
38
+ return self._handle_deepseek_test(user_id, message_text[8:].strip())
39
+
40
  # 檢查是否為特殊指令
41
  if message_text.lower() in ['help', '幫助', '說明', '指令']:
42
  return self._handle_help_command(user_id)
 
247
  return {
248
  "success": False,
249
  "error": str(e)
250
+ }
251
+
252
+ def _handle_deepseek_test(self, user_id: str, test_message: str) -> Dict[str, Any]:
253
+ """
254
+ 🔧 後門測試:直接與 DeepSeek V3 聊天
255
+ 使用方式:在 LINE 中發送 "##test##你好"
256
+ """
257
+ try:
258
+ import asyncio
259
+ import httpx
260
+ from backend.config import settings
261
+
262
+ if not test_message:
263
+ return {
264
+ "type": "text",
265
+ "text": "🔧 DeepSeek V3 測試模式\n\n使用方式:##test##[您的訊息]\n\n範例:##test##你好,請介紹一下自己",
266
+ "user_id": user_id,
267
+ "success": True,
268
+ "intent": "deepseek_test"
269
+ }
270
+
271
+ # 同步調用異步函數的方法
272
+ def sync_call_deepseek():
273
+ return asyncio.run(self._call_deepseek_api(test_message))
274
+
275
+ try:
276
+ response_text = sync_call_deepseek()
277
+
278
+ return {
279
+ "type": "text",
280
+ "text": f"🤖 DeepSeek V3 回應:\n\n{response_text}",
281
+ "user_id": user_id,
282
+ "success": True,
283
+ "intent": "deepseek_test"
284
+ }
285
+
286
+ except Exception as api_error:
287
+ return {
288
+ "type": "text",
289
+ "text": f"❌ DeepSeek V3 API 錯誤:\n{str(api_error)}\n\n請檢查:\n1. API Key 設定\n2. 網路連線\n3. OpenRouter 服務狀態",
290
+ "user_id": user_id,
291
+ "success": False,
292
+ "intent": "deepseek_test",
293
+ "error": str(api_error)
294
+ }
295
+
296
+ except Exception as e:
297
+ logger.error(f"DeepSeek 測試錯誤: {str(e)}")
298
+ return {
299
+ "type": "text",
300
+ "text": f"❌ 測試功能錯誤:{str(e)}",
301
+ "user_id": user_id,
302
+ "success": False,
303
+ "intent": "deepseek_test",
304
+ "error": str(e)
305
+ }
306
+
307
+ async def _call_deepseek_api(self, message: str) -> str:
308
+ """
309
+ 直接調用 DeepSeek V3 API
310
+ """
311
+ from backend.config import settings
312
+
313
+ api_key = settings.OPENROUTER_API_KEY
314
+ model = settings.OPENROUTER_MODEL
315
+
316
+ if not api_key:
317
+ return "❌ OPENROUTER_API_KEY 未設定"
318
+
319
+ headers = {
320
+ "Authorization": f"Bearer {api_key}",
321
+ "Content-Type": "application/json",
322
+ "HTTP-Referer": "https://huggingface.co/spaces",
323
+ "X-Title": "LINE Bot DeepSeek Test"
324
+ }
325
+
326
+ try:
327
+ async with httpx.AsyncClient(timeout=30.0) as client:
328
+ response = await client.post(
329
+ "https://openrouter.ai/api/v1/chat/completions",
330
+ headers=headers,
331
+ json={
332
+ "model": model,
333
+ "messages": [
334
+ {
335
+ "role": "system",
336
+ "content": "你是 DeepSeek V3,一個強大的中文 AI 助手。請用繁體中文回應,並在回應開頭說明你是 DeepSeek V3。"
337
+ },
338
+ {
339
+ "role": "user",
340
+ "content": message
341
+ }
342
+ ],
343
+ "temperature": 0.7,
344
+ "max_tokens": 500
345
+ }
346
+ )
347
+
348
+ if response.status_code == 200:
349
+ result = response.json()
350
+ content = result["choices"][0]["message"]["content"]
351
+ return content
352
+ else:
353
+ return f"❌ API 錯誤 {response.status_code}: {response.text}"
354
+
355
+ except httpx.TimeoutException:
356
+ return "❌ API 請求超時,請稍後再試"
357
+ except Exception as e:
358
+ return f"❌ 網路錯誤: {str(e)}"