使用 bot 框架向 slack 发送主动消息
sending pro active messages to slack using bot framework
我有一个用例,我想使用 bot 框架偶尔向一个闲散的用户发送一条消息,我知道其中的通知 ID。
现在我有以下内容:
server.get("/api/notify", async (req, res) => {
await adapter.createConversation(conversationReference, async turnContext => {
await turnContext.sendActivity("proactive hello");
});
res.setHeader("Content-Type", "text/html");
res.writeHead(200);
res.write(
"<html><body><h1>Proactive messages have been sent.</h1></body></html>"
);
res.end();
});
对话参考如下:
const conversationReference = {
user: { id: "ID3:ID2", name: "user1" },
bot: { id: "ID1:ID2", name: "bot1" },
conversation: {
isGroup: false,
id: "ID1:ID2:ID3",
conversationType: "slack",
tenantId: "",
name: ""
},
channelId: "slack",
serviceUrl: "https://slack.botframework.com/"
};
但只有当用户在机器人启动后与机器人交谈时它才有效。但是在重新启动后,这将不再起作用,直到用户发起对话。
当我在 bot 重启后尝试发送主动消息时,用户还没有开始对话,我收到以下异常:
UnhandledPromiseRejectionWarning: Error
at new RestError (/usr/app/node_modules/@azure/ms-rest-js/dist/msRest.node.js:1397:28)
at /usr/app/node_modules/@azure/ms-rest-js/dist/msRest.node.js:1849:37
at process._tickCallback (internal/process/next_tick.js:68:7)
我的问题是:我怎样才能保持这种状态,这样我在重启后仍然可以发送主动消息?
啊哈!你这部分问题是关键:
But it only works if the user has talked to the bot since the bot has booted. But after a restart this won't work anymore until the user initiates a conversation.
这几乎肯定是 TrustServiceUrl Issue. Please see 的附加上下文。
基本上,在重新启动时,机器人忘记了可以与该用户交谈。您需要 "Trust" user/activity 的 ServiceUrl 以确保机器人知道可以向他们发送消息。
我有一个用例,我想使用 bot 框架偶尔向一个闲散的用户发送一条消息,我知道其中的通知 ID。
现在我有以下内容:
server.get("/api/notify", async (req, res) => {
await adapter.createConversation(conversationReference, async turnContext => {
await turnContext.sendActivity("proactive hello");
});
res.setHeader("Content-Type", "text/html");
res.writeHead(200);
res.write(
"<html><body><h1>Proactive messages have been sent.</h1></body></html>"
);
res.end();
});
对话参考如下:
const conversationReference = {
user: { id: "ID3:ID2", name: "user1" },
bot: { id: "ID1:ID2", name: "bot1" },
conversation: {
isGroup: false,
id: "ID1:ID2:ID3",
conversationType: "slack",
tenantId: "",
name: ""
},
channelId: "slack",
serviceUrl: "https://slack.botframework.com/"
};
但只有当用户在机器人启动后与机器人交谈时它才有效。但是在重新启动后,这将不再起作用,直到用户发起对话。
当我在 bot 重启后尝试发送主动消息时,用户还没有开始对话,我收到以下异常:
UnhandledPromiseRejectionWarning: Error
at new RestError (/usr/app/node_modules/@azure/ms-rest-js/dist/msRest.node.js:1397:28)
at /usr/app/node_modules/@azure/ms-rest-js/dist/msRest.node.js:1849:37
at process._tickCallback (internal/process/next_tick.js:68:7)
我的问题是:我怎样才能保持这种状态,这样我在重启后仍然可以发送主动消息?
啊哈!你这部分问题是关键:
But it only works if the user has talked to the bot since the bot has booted. But after a restart this won't work anymore until the user initiates a conversation.
这几乎肯定是 TrustServiceUrl Issue. Please see
基本上,在重新启动时,机器人忘记了可以与该用户交谈。您需要 "Trust" user/activity 的 ServiceUrl 以确保机器人知道可以向他们发送消息。