Spaces:
Sleeping
Sleeping
Commit ·
6ece2b7
1
Parent(s): 8acb26f
auto update webhook
Browse files- main.py +29 -0
- requirements.txt +2 -1
main.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import threading
|
|
|
|
|
|
|
| 3 |
from flask import Flask, request, abort
|
| 4 |
from linebot.v3 import WebhookParser
|
| 5 |
from linebot.v3.messaging import (
|
|
@@ -26,6 +28,33 @@ line_bot_api = MessagingApi(api_client)
|
|
| 26 |
parser = WebhookParser(CHANNEL_SECRET)
|
| 27 |
ai_client = genai.Client(api_key=GEMINI_API_KEY)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def handle_events_background(events):
|
| 30 |
for event in events:
|
| 31 |
if isinstance(event, MessageEvent) and isinstance(event.message, TextMessageContent):
|
|
|
|
| 1 |
import os
|
| 2 |
import threading
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
from flask import Flask, request, abort
|
| 6 |
from linebot.v3 import WebhookParser
|
| 7 |
from linebot.v3.messaging import (
|
|
|
|
| 28 |
parser = WebhookParser(CHANNEL_SECRET)
|
| 29 |
ai_client = genai.Client(api_key=GEMINI_API_KEY)
|
| 30 |
|
| 31 |
+
# 設定 LINE Webhook URL
|
| 32 |
+
webhook_url = os.getenv("LINE_WEBHOOK_URL")
|
| 33 |
+
|
| 34 |
+
# 自動更新 LINE Webhook URL
|
| 35 |
+
def update_line_webhook(webhook_url):
|
| 36 |
+
"""使用 LINE Messaging API 更新 Webhook URL"""
|
| 37 |
+
url = "https://api.line.me/v2/bot/channel/webhook/endpoint"
|
| 38 |
+
headers = {
|
| 39 |
+
"Authorization": f"Bearer {line_channel_access_token}",
|
| 40 |
+
"Content-Type": "application/json"
|
| 41 |
+
}
|
| 42 |
+
data = {
|
| 43 |
+
"endpoint": webhook_url
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
response = requests.put(url, headers=headers, json=data)
|
| 47 |
+
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
print(f"✅ LINE Webhook URL 已自動更新為:{webhook_url}")
|
| 50 |
+
return True
|
| 51 |
+
else:
|
| 52 |
+
print(f"❌ 更新失敗:{response.status_code} - {response.text}")
|
| 53 |
+
return False
|
| 54 |
+
|
| 55 |
+
# 執行更新
|
| 56 |
+
update_line_webhook(webhook_url)
|
| 57 |
+
|
| 58 |
def handle_events_background(events):
|
| 59 |
for event in events:
|
| 60 |
if isinstance(event, MessageEvent) and isinstance(event.message, TextMessageContent):
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
flask
|
| 2 |
line-bot-sdk
|
| 3 |
-
google-genai
|
|
|
|
|
|
| 1 |
flask
|
| 2 |
line-bot-sdk
|
| 3 |
+
google-genai
|
| 4 |
+
requests
|