根据the docs和examples,必须保存对会话的引用,以便按需还原(例如,当服务器收到HTTP请求时)并将响应消息发送到会话。公共频道。
所以我在做:
#CorpChannel
上提及该机器人)storage.write(storeItems)
)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连接器服务支持创建群组对话;但是,此方法和大多数渠道仅支持发起直接消息(非组)对话。
它与与发布第一条消息的私人用户进行新的对话
答案 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)"));