You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Twilio Studio Flow输出未发送至网站回调的技术咨询

Twilio Conversations中Studio Flow消息触发网站回调的配置方案

问题描述

我们正在网站上实现聊天机器人,目前代码已成功触发Twilio Studio Flow,Conversations日志显示代码发送了“Hi”消息,Flow也正常触发并返回了预期问候语。但核心问题是:Flow发送的消息无法触发网站的onMessageAdded回调,导致无法将Flow的消息展示给网站用户。我们找不到配置Flow输出回调到网站的途径,推测问题可能出在Conversations、Messages、Flow的配置或代码本身。

现有代码

TwilioClient.Init(_twilioAccountSid, _twilioAuthToken);

// Create Conversation
var conversation = ConversationResource.Create(
    friendlyName: "Test conversation",
    messagingServiceSid: _twilioMessagingServiceSid,
    attributes: null,
    xTwilioWebhookEnabled: ConversationResource.WebhookEnabledTypeEnum.True
);
_log.Info("Conversation.Create: " + conversation.Sid);

// Attach Flow to Conversation
var webhook = WebhookResource.Create(
    configurationMethod: WebhookResource.MethodEnum.Post,
    configurationFlowSid: _twilioStudioFlowSid,
    target: WebhookResource.TargetEnum.Studio,
    configurationFilters: new List<string> {
        "onMessageAdded", 
        "onMessageUpdated", 
        "onMessageRemoved", 
        "onConversationUpdated", 
        "onConversationRemoved", 
        "onParticipantAdded", 
        "onParticipantUpdated", 
        "onParticipantRemoved"
    },
    pathConversationSid: conversation.Sid
);
_log.Info("WebhookResource.Create: " + webhook.Sid);

// Create a Participant
var participant = ParticipantResource.Create(
    identity: _identity,
    pathConversationSid: conversation.Sid
);
_log.Info("Participant.Create: " + participant.Sid);

// Send Message
var message = MessageResource.Create(
    author: _identity,
    body: "Hi!",
    xTwilioWebhookEnabled: MessageResource.WebhookEnabledTypeEnum.True,
    pathConversationSid: conversation.Sid
);
_log.Info("Message.Create: " + message.Sid);

解决方案

1. 新增指向网站的Conversation Webhook

你当前仅创建了指向Studio Flow的Webhook,需要额外创建一个监听onMessageAdded事件的Webhook,将消息推送至你的网站回调地址。添加以下代码到现有逻辑中:

// 创建网站回调的Webhook
var websiteWebhook = WebhookResource.Create(
    configurationMethod: WebhookResource.MethodEnum.Post,
    configurationUrl: "你的网站回调API地址", // 替换为实际的网站回调接口
    target: WebhookResource.TargetEnum.Webhook,
    configurationFilters: new List<string> { "onMessageAdded" },
    pathConversationSid: conversation.Sid
);
_log.Info("Website Webhook Created: " + websiteWebhook.Sid);

这个Webhook与指向Studio的Webhook独立存在,负责将所有新消息(包括Flow发送的)推送至你的网站。

2. 确认Flow消息的事件触发逻辑

Studio Flow发送的消息会自动被添加到关联的Conversation中,触发onMessageAdded事件,无需额外配置Flow组件。只需确保你的网站Webhook正确监听该事件即可。

3. 前端SDK监听消息事件(可选)

如果你的网站使用Twilio Conversations前端SDK,直接通过SDK监听messageAdded事件更高效,无需依赖后端回调:

// 前端示例代码
conversation.on('messageAdded', (message) => {
    // 将消息渲染到聊天界面
    renderMessage(message.author, message.body);
});

确保前端已正确初始化Conversations客户端并加入目标Conversation。

4. 验证Webhook配置

  • 确认网站回调地址可以正常接收POST请求,且返回200状态码
  • 检查Twilio控制台中的Conversations Webhook日志,查看是否有推送记录
  • 确保Webhook的过滤规则仅保留onMessageAdded,避免无关事件干扰

内容的提问来源于stack exchange,提问作者rwkiii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 17:15:59