Microsoft Bot-Node SDK:如何发布到公共频道*不回复上一个版本。活动*

时间:2020-05-29 07:40:11

标签: node.js azure botframework microsoft-teams

背景与理论

根据the docsexamples,必须保存对会话的引用,以便按需还原(例如,当服务器收到HTTP请求时)并将响应消息发送到会话。公共频道。

所以我在做:

  1. 任何用户在某个频道(例如#CorpChannel上提及该机器人)
  2. 自动存储(尤其是我正在使用Azure Cosmos db)对话的引用(storage.write(storeItems)
  3. [之后] Bot收到一个HTTP请求,该请求表示:“发送“ hi there”到#CorpChannel”
  4. Bot恢复对话参考并将其用于创建TurnContext以便调用sendActivity()

问题

活动“'嗨,那里'”正在回复我的漫游器原始提要,而不是通过该渠道开始新的话题/对话。我想在#CorpChannel

开始一个新的对话

视觉上:

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

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

HTTP请求:“发送“ hi there”到#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 } });
});

这是我要进入数据库的代码

// (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;
  }
};

我该如何发布新消息而不是回复原始主题?

====编辑====

我也尝试过使用SDK中的createConversation()方法,但正如文档中所述:

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

它与与发布第一条消息的私人用户进行新的对话

1 个答案:

答案 0 :(得分:1)

“团队”频道线程中的对话ID如下:19:1234abcd@thread.tacv2;messageid=12345

启动新线程所需要做的就是将删除了“ messageid”部分的消息发送到该会话ID。

您可以这样删除messageid部分:

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

如果要通过转弯上下文发送消息以保留其中间件管道,则可以直接修改传入活动的对话ID。由于这会影响转弯其余时间的转弯上下文,因此,稍后再更改会话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)"));