Microsoft Bot-Node SDK:如何发布到公共频道*不回复上一个版本.活动* [英] Microsoft Bot - Node SDK: How to post to a public channel *without replying to a prev. activity*

查看:79
本文介绍了Microsoft Bot-Node SDK:如何发布到公共频道*不回复上一个版本.活动*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据

According to the docs and examples, you must save the reference to a conversation in order to restore it on demand (for instance when the server receives an HTTP request) and send a reactive message to a public channel.

所以我在做:

  1. 任何用户在频道上提及该漫游器(例如,在 #CorpChannel 处)
  2. 自动存储(特别是我正在使用Azure Cosmos db)对话的引用( storage.write(storeItems))
  3. [稍后] Bot收到一个HTTP请求,该请求表示:发送"hi there"到#CorpChannel"
  4. Bot还原会话引用,并将其用于创建 TurnContext 以便调用 sendActivity()
  1. Any user mentions the bot on a channel (at #CorpChannel for example)
  2. Bot stores (in particular I'm using Azure Cosmos db) the reference of the conversation(storage.write(storeItems))
  3. [later] Bot receives an HTTP request that means: "send 'hi there' to #CorpChannel"
  4. Bot restores the conversation reference and uses it for creating a TurnContext in order to call sendActivity()

问题

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

Problem

The activity 'hi there' is replying to the original mention to my bot instead of starting a new thread/conversation over that channel. I want to start a new fresh conversation at #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 |
    --------------------

我尝试过的

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

What I've tried

This is the code where I'm sending the activity on demand

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 } });
});

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

this is the code where I'm going to 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;
  }
};

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

====编辑====

how can I post a new message instead of replying to the original thread?

==== EDIT ====

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

I've tried also to use createConversation() method from the SDK, but as it says in the documentation:

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

The Bot Connector service supports the creating of group conversations; however, this method and most channels only support initiating a direct message (non-group) conversation.

它与与发布第一条消息私密

It starts a new conversation with the original user that posted the first message in private

推荐答案

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

A conversation ID in a Teams channel thread looks like this: 19:1234abcd@thread.tacv2;messageid=12345

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

All you need to do to start a new thread is to send a message to that conversation ID with the "messageid" portion removed.

您可以这样删除messageid部分:

You can remove the messageid portion like this:

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

如果要通过转弯上下文发送消息以保留其中间件管道,则可以直接修改传入活动的对话ID.由于这会影响转弯其余时间的转弯上下文,因此,稍后再更改会话ID可能是一个好主意.

If you want to send a message through the turn context to retain its middleware pipeline, you can modify the incoming activity's conversation ID directly. Since this would affect the turn context for the rest of the turn, it might be a good idea to change the conversation ID back afterwards.

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

如果您不想担心转弯上下文,也可以直接通过连接器客户端发送消息.

You can also send a message through a connector client directly if you don't want to worry about the turn context.

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)"));

这篇关于Microsoft Bot-Node SDK:如何发布到公共频道*不回复上一个版本.活动*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆