iT邦幫忙

2025 iThome 鐵人賽

DAY 19
0

昨日在測試時發現使用天氣及地震時輸入要求會出現錯誤,結論是要parameters,所以又小改了一下
.yaml

openapi: 3.0.3
info:
  title: Weather & Earthquake MCP Tools
  version: 1.0.0
  description: 天氣和地震查詢工具
servers:
  - url: http://localhost:8001
    description: REST API 伺服器
paths:
  /weather:
    post:
      operationId: getWeather
      summary: 查詢天氣
      description: 根據縣市名稱查詢天氣資訊
      parameters:
        - name: LocationName
          in: query
          required: false
          description: 臺灣各縣市名稱
          schema:
            type: string
        - name: ElementName
          in: query
          required: false
          description: 天氣預報因子
          schema:
            type: string
      responses:
        '200':
          description: 天氣結果
          content:
            application/json:
              schema:
                type: object
                properties:
                  city:
                    type: string
                    description: 城市名稱
                  temperature:
                    type: string
                    description: 溫度資訊
                  description:
                    type: string
                    description: 天氣描述
  /earthquake:
    post:
      operationId: getEarthquakes
      summary: 取得地震資料
      description: 可指定縣市或取得全部最新地震
      parameters:
        - name: target_areas
          in: query
          required: false
          description: 目標縣市名稱
          schema:
            type: string
        - name: limit
          in: query
          required: false
          description: 限制回傳地震筆數
          schema:
            type: string
      responses:
        '200':
          description: 地震查詢結果
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    description: 查詢是否成功
                  message:
                    type: string
                    description: 回應訊息
                  data:
                    type: array
                    description: 地震資料列表
                    items:
                      type: object
                      properties:
                        time:
                          type: string
                          description: 地震時間
                        location:
                          type: string
                          description: 地震位置
                        magnitude:
                          type: string
                          description: 地震規模
                        depth:
                          type: string
                          description: 地震深度

rest_api

@app.post("/earthquake")
async def post_earthquakes(
    # Query 參數 (來自 MCP)
    target_areas: Optional[str] = Query(None, description="目標縣市名稱"),
    limit: Optional[str] = Query("5", description="限制回傳地震筆數"),
    # Request Body (來自傳統 HTTP 客戶端)
    request: Optional[EarthquakeRequest] = Body(None)
):
    # 優先使用 query 參數,如果沒有則使用 request body
    final_target_areas = target_areas or (request.target_areas if request else None)
    final_limit = limit if limit != "5" else (request.limit if request else "5")

接著是今天的目標,要接收discord的訊息然後回傳我要的地震或天氣功能
首先到設定,選擇community nodes 右上角install輸入@jordanburke/n8n-nodes-discord
https://ithelp.ithome.com.tw/upload/images/20251003/20177920yUvOw0NTGA.png
安裝好後就可以在自己的n8n中找到discord trigger了
https://ithelp.ithome.com.tw/upload/images/20251003/201779206LF3B2Vbak.png
接著需要Credential
但要使用的話,由於之前的權限不同,要再創一個機器人
https://discord.com/developers/applications
到OAuth2,最上面的client ID
https://ithelp.ithome.com.tw/upload/images/20251003/201779204zNDeyMwEx.png
接著下面勾選bot、applications.command、Adminstrator
https://ithelp.ithome.com.tw/upload/images/20251003/201779206TBSB6JNSz.png
接著到bot勾選這些
https://ithelp.ithome.com.tw/upload/images/20251003/2017792011Rz4Zgq0Y.png
然後加到你的discord群組
bot token之前說過這邊不介紹
回到n8n,到Setting,選擇n8n api,新增api,如可以更改過期時間,我選擇無限制
https://ithelp.ithome.com.tw/upload/images/20251003/20177920IhpAF1gGlQ.png
這樣Credential要的資料就全部都有了
接著重開n8n就可以看到它可以正常讀取discord的頻道了,明天再正式使用他


上一篇
d18
系列文
這是一個一個一個 Python API 與 Gemini 整合、n8n入門指南19
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言