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

Teams AI Library JS调用app.sendProactiveActivity发送主动消息报错求助

解决Bot Framework主动消息发送的配置错误

问题场景

我想基于存储的conversationReference,通过app.sendProactiveActivity给用户发送1:1主动消息,后端新增一个/notify路由作为外部系统的webhook触发发送,但调用时遇到两类错误:

错误场景1:直接调用app.sendProactiveActivity

代码:

// Listen for notification requests
server.post("/notify", async (req, res) => {
  // Get the conversationReference for the user
  var conversationReference = await getConversationReference("userId");

  // Send the proactive message
  await app.sendProactiveActivity(conversationReference, "This is the notification");
})

报错信息:

{
    "code": "Internal",
    "message": "Error: You must configure the Application with an 'adapter' before calling Application.continueConversationAsync()"
}

错误场景2:用adapter.process包裹调用

代码:

// Listen for notification requests
server.post("/notify", async (req, res) => {
  // Get the conversationReference for the user
  var conversationReference = await getConversationReference("userId");

  // Send the proactive message
  await adapter.process(req, res as any, async (context) => {
    await app.sendProactiveActivity(conversationReference, "This is the notification");
  });
})

报错信息:

{
    "code": "Internal",
    "message": "Error: validateAndFixActivity(): missing activity type."
}

问题原因

  • 第一个错误:Application实例未绑定adapter,无法发起主动会话流程。
  • 第二个错误:误用adapter.process——该方法用于处理来自Bot Framework服务的入站请求(如/api/messages的消息),主动消息属于出站操作,不需要走这个流程,因此会因缺少请求内的Activity类型报错。

正确实现方案

推荐写法:直接用adapter.continueConversation恢复会话

// Listen for notification requests
server.post("/notify", async (req, res) => {
  // 获取用户的conversationReference
  const conversationReference = await getConversationReference("userId");

  try {
    // 通过adapter恢复会话上下文并发送消息
    await adapter.continueConversation(conversationReference, async (context) => {
      await context.sendActivity("This is the notification");
    });

    res.status(200).send("Notification sent successfully");
  } catch (error) {
    console.error("Error sending proactive message:", error);
    res.status(500).send("Failed to send notification");
  }
})

备选写法:给Application绑定adapter后用sendProactiveActivity

如果坚持使用app.sendProactiveActivity,需要在初始化Application时传入adapter参数:

// 初始化Application时绑定adapter
const app = new Application({
  adapter: adapter,
  // 其他必填配置:botId、appId、appPassword等
});

// 路由代码
server.post("/notify", async (req, res) => {
  const conversationReference = await getConversationReference("userId");

  try {
    await app.sendProactiveActivity(conversationReference, "This is the notification");
    res.status(200).send("Notification sent successfully");
  } catch (error) {
    console.error("Error sending proactive message:", error);
    res.status(500).send("Failed to send notification");
  }
})

关键注意点

  • 主动消息是出站操作,无需通过adapter.process处理,直接用adapter.continueConversation恢复会话上下文是更标准的做法。
  • 确保存储的conversationReference结构完整且有效,包含user、conversation、channelId等必要字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:31:04