微软机器人 - Node SDK。如何在公共频道发帖*而不回复前一个活动*?

问题描述 投票:0回答:1

背景和理论

根据 文献例子你必须保存对话的引用,以便在需要时(例如当服务器收到一个HTTP请求时)恢复它,并向一个公共通道发送一个反应性消息。

所以我在做。

  1. 任何用户在一个频道上提到机器人(在... #CorpChannel (例如)
  2. Bot存储(特别是我使用Azure Cosmos db)对话的引用(storage.write(storeItems))
  3. [稍后]Bot收到一个HTTP请求,意思是,"向#CorpChannel发送'你好' "发送'你好'到#CorpChannel"
  4. 机器人恢复对话参考,并使用它来创建一个 TurnContext 为了调用 sendActivity()

问题

活动 "你好 是回复最初提到我的机器人,而不是在那个频道上开始一个新的线程对话。我想 开始 在#CorpChannel#上进行新的对话。

视觉上。

Jane Doe: --------------
| @MyBot        09:00AM |
------------------------

Jhon Doe: --------------
| what ever     10:00AM |
------------------------

HTTP请求:"向#CorpChannel发送'你好' "发送'你好'到#CorpChannel"

Jhon Doe: --------------
| whatever      10:00AM |
------------------------

Jane Doe: --------------
| @MyBot        09:00AM |
------------------------
   |> MyBot: -----------
   |  Hi there  11:00AM |
    --------------------

我试过的

这是我按需发送活动的代码。

server.post("/api/notify", async (req, res) => {
  const channel = req.body.channel;
  const message = req.body.message;
  const conversation = await bot.loadChannelConversation(channel);

  if (!conversation) { /* ... */ }

  await adapter.continueConversation(conversation, async (context) => {
      await context.sendActivity(message);
  });

  return res.send({ notified: { channel, message } });
});

这是我要去db的代码。

// (storage) is in the scope
const loadChannelConversation = async (key) => {
  try {
    const storeItems = await storage.read(['channels']);
    const channels = storeItems['channels'] || {};
    return channels[key] || null;
  } catch (err) {
    console.error(err);
    return undefined;
  }
};

我怎么能发一条新消息而不是回复原主题?

==== 编辑 ====

我也试过用 createConversation() 方法,但正如它在文档中所说。

Bot Connector服务支持创建群组对话;但是,这个方法和大多数渠道只支持发起直接消息(非群组)对话。

它与发布第一条消息的原始用户开始一个新的对话。私下

node.js azure botframework microsoft-teams
1个回答
1
投票

Teams频道线程中的对话ID是这样的。19:[email protected];messageid=12345

所有你需要做的就是发送一条消息到那个对话ID,并去掉 "messageid "部分。

你可以像这样删除 messageid 部分。

function getRootConversationId(turnContext) {
    const threadId = turnContext.activity.conversation.id;
    const messageIdIndex = threadId.indexOf(";messageid=");
    return messageIdIndex > 0 ? threadId.slice(0, messageIdIndex) : threadId;
}

如果你想通过turn上下文发送消息来保留它的中间件管道, 你可以直接修改传入活动的对话ID。因为这样会影响到turn上下文的其余部分,所以事后再把对话id改回来可能是个好主意。

const conversationId = getRootConversationId(turnContext);

// Send through the turn context

const conversation = turnContext.activity.conversation;
const threadId = conversation.id;
conversation.id = conversationId;

await turnContext.sendActivity("New thread (through the turn context)");

conversation.id = threadId;  // restore conversation ID

如果你不想担心回合上下文,也可以直接通过连接器客户端发送消息。

const conversationId = getRootConversationId(turnContext);

// Send through a connector client

const client = turnContext.turnState.get(turnContext.adapter.ConnectorClientKey);

await client.conversations.sendToConversation(
    conversationId,
    MessageFactory.text("New thread (through a connector client)"));
© www.soinside.com 2019 - 2024. All rights reserved.