根据 文献 和 例子你必须保存对话的引用,以便在需要时(例如当服务器收到一个HTTP请求时)恢复它,并向一个公共通道发送一个反应性消息。
所以我在做。
#CorpChannel
(例如)storage.write(storeItems)
)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服务支持创建群组对话;但是,这个方法和大多数渠道只支持发起直接消息(非群组)对话。
它与发布第一条消息的原始用户开始一个新的对话。私下
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)"));