如何將 OpenCode 接入飛書機器人
前言
本文將詳細介紹如何將 OpenCode 接入飛書,讓你可以透過飛書群組與 AI 助手互動。這個方法使用 Webhook 接收訊息,並透過飛書 API 發送回覆。
所需材料
飛書開發者帳號
安裝了 OpenCode 的電腦或伺服器
Cloudflare Tunnel(用於建立公開 URL)
Python 環境
步驟一:創建飛書應用
前往 飛書開放平台
點擊「創建企業自建應用」
填寫應用名稱(如「OpenCode AI」)
取得 App ID 和 App Secret
步驟二:啟用機器人功能
進入應用頁面
點擊「添加應用能力」
選擇「機器人」
啟用機器人功能
步驟三:設定權限
進入「權限管理」
開啟以下權限:
im:message:send_as_bot - 發送消息
im:message - 讀取消息
im:resource - 上傳資源
步驟四:設定事件訂閱
進入「事件與回調」
選擇「訂閱方式」為「Webhook URL」
填入你的 Webhook URL(稍後設定)
步驟五:開啟 Cloudflare Tunnel
安裝 cloudflared:
# Windows PowerShell
irm https://pkg.cloudflare.com/cloudflared-stable.windows-amd64.msi -o cloudflared.msi
msiexec /i cloudflared.msi
啟動 Tunnel:
cloudflared tunnel --url http://localhost:3000
複製輸出的 URL,格式如:
https://xxxx.trycloudflare.com
步驟六:啟動 Python Webhook 服務
建立 feishu_webhook.py 檔案:
from flask import Flask, request, Response
import json
import requests
app = Flask(__name__)
# 飛書配置
FEISHU_APP_ID = "你的 App ID"
FEISHU_APP_SECRET = "你的 App Secret"
CHAT_ID = "你的群組 ID"
def get_feishu_token():
r = requests.post(
'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal',
json={'app_id': FEISHU_APP_ID, 'app_secret': FEISHU_APP_SECRET}
)
return r.json()['tenant_access_token']
def send_to_feishu(message, token):
r = requests.post(
'https://open.feishu.cn/open-apis/im/v1/messages',
headers={'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'},
params={'receive_id_type': 'chat_id'},
json={
'receive_id': CHAT_ID,
'msg_type': 'text',
'content': json.dumps({'text': message})
}
)
return r.json()
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
if request.method == 'GET':
challenge = request.args.get('challenge')
if challenge:
return Response(json.dumps({"challenge": challenge}), mimetype='application/json')
return Response(json.dumps({"status": "ok"}), mimetype='application/json')
data = request.json
print(f"Received: {data}")
if data.get('type') == 'url_verification':
challenge = data.get('challenge', '')
return Response(json.dumps({"challenge": challenge}), mimetype='application/json')
# 處理訊息並回覆
event = data.get('event', {})
message = event.get('message', {})
if message.get('msg_type') == 'text':
text = message.get('content', '')
try:
content = json.loads(text)
user_text = content.get('text', '')
except:
user_text = text
if user_text:
token = get_feishu_token()
response = f"收到: {user_text}"
send_to_feishu(response, token)
return Response(json.dumps({"code": 0, "msg": "success"}), mimetype='application/json')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3000)
執行:
pip install flask requests
python feishu_webhook.py
步驟七:設定飛書 Webhook
回到飛書開放平台
在「事件與回調」中選擇「Webhook URL」模式
URL 填入 Cloudflare Tunnel 的 URL,加上 /webhook
點擊「保存」
步驟八:發布應用
填寫版本號和更新說明
提交審核
作為管理員通過審核
步驟九:將機器人加入群組
在飛書群組中
設定 → 成員 → 新增應用
選擇你的應用
使用方式
確保 Cloudflare Tunnel 和 Python 腳本都在運行
在群組中 @機器人 發送訊息
AI 將自動回覆
注意事項
Cloudflare Tunnel 需要保持運行
飛書應用需要發布才能生效
確保事件訂閱中的「接收消息」已開啟
結語
透過這個方法,你可以將 OpenCode 接入飛書,實現 AI 助手的即時互動。這對於團隊協作、自動化客服等場景都非常實用。
作者:未來之書 日期:2026年3月20日

沒有留言:
張貼留言