You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Google Chat App(Python/FastAPI)因路由至插件管道导致所有响应格式均报‘Failed to parse JSON’错误的解决问询

Google Chat App(Python/FastAPI)因路由至插件管道导致所有响应格式均报‘Failed to parse JSON’错误的解决问询

问题背景

我最近在尝试用Python(FastAPI/Cloud Functions Framework)结合ngrok隧道部署一个简单的Google Chat应用,目标很简单:做一个纯文本的聊天机器人,能返回像{"text": "Hello"}这种最基础的JSON响应就行。

核心问题

按官方Google Chat文档配置完后,我的服务器响应一直被Google Cloud端点拒绝,错误日志显示请求被错误路由到了Workspace Add-ons的旧处理管道(gsuiteaddons.googleapis.com,而不是标准的Chat API管道(chat.googleapis.com)。不管我用文档里推荐的哪种现代响应格式,都会触发旧管道的格式校验错误,完全无法适配。

我尝试过的所有方案及对应错误

尝试1:标准Chat App纯文本响应(预期的理想格式)

我先试了官方文档里说的纯文本响应:

{"text": "Test"}

结果收到的错误日志:

Failed to parse JSON as RenderActions, DataActions or Card. Failed with error: Cannot find field: text in message google.apps.card.v1.RenderActions

尝试2:极简现代卡片响应(cardsV2格式)

接着我试了文档推荐的现代cardsV2卡片结构:

{
    "cardsV2": [{
        "cardId": "test-card",
        "card": {
            "sections": [{
                "widgets": [{
                    "textParagraph": {
                        "text": "Test card content"
                    }
                }]
            }]
        }
    }]
}

错误日志依然报错:

Failed to parse JSON as RenderActions, DataActions or Card. Failed with error: Cannot find field: cardsV2 in message google.apps.card.v1.RenderActions

尝试3:用Add-on的actionResponse包装器

我又试着套上旧Add-on的actionResponse包装(结合cardsV2):

{
    "actionResponse": { "type": "NEW_MESSAGE" },
    "cardsV2": [{
        "cardId": "test-card",
        "card": {
            "sections": [{
                "widgets": [{
                    "textParagraph": {
                        "text": "Test card content"
                    }
                }]
            }]
        }
    }]
}

错误日志:

Failed to parse JSON as RenderActions, DataActions or Card. Failed with error: Cannot find field: actionResponse in message google.apps.card.v1.Card

尝试4:Stack Overflow上的解决方案

我还试了Stack Overflow上看到的一个解决方案,用了特定的包装结构:

{
    "hostAppDataAction": {
        "chatDataAction": {
            "createMessageAction": {
                "message": {
                    "text": "test"
                }
            }
        }
    }
}

结果错误日志更复杂:

Failed to parse JSON as RenderActions, DataActions or Card. Failed to parse JSON as RenderActions. Failed with error: Expect message object but got: ["{"hostAppDataAction": {"chatDataAction": {"createMessageAction": {"message": {"text": "test"}}}}}",200,{"Content-Type":"application/json"}]
Failed to parse JSON as DataActions. Failed with error: Expect message object but got: ["{"hostAppDataAction": {"chatDataAction": {"createMessageAction": {"message": {"text": "test"}}}}}",200,{"Content-Type":"application/json"}]
Failed to parse JSON as Card. Failed with error: Expect message object but got: ["{"hostAppDataAction": {"chatDataAction": {"createMessageAction": {"message": {"text": "test"}}}}}",200,{"Content-Type":"application/json"}]

我的当前配置

  • Google Cloud项目配置:只显式启用了Google Chat APIGoogle Workspace Add-ons API完全没启用
  • Chat API具体配置
    • 交互功能已启用(才能设置端点URL)
    • 连接设置里填的是ngrok提供的隧道URL,能正常接收用户的消息请求
    • Slash命令和对话框都处于禁用/空状态
  • 服务器代码返回逻辑:用FastAPI的Response对象返回JSON,核心代码如下:
from fastapi import Response
import json

# 处理Chat请求的路由函数内
payload = {"text": "Hello"}
return Response(
    content=json.dumps(payload),
    media_type="application/json",
    status_code=200
)

核心疑问

  1. 怎么才能强制我的Google Cloud项目把交互事件路由到标准的Google Chat API管道(chat.googleapis.com,而不是旧的Workspace Add-ons管道?
  2. 有没有什么隐藏的设置(不管是在Google Cloud控制台还是Workspace管理员控制台),会把我的应用标记成Add-on而不是标准Chat App?明明我只启用了Chat API,完全没碰Add-ons相关的配置啊...

火山引擎 最新活动