使用 Microsoft Bot Framework,当我在 Messenger 频道上写作时,如何将结果推送到特定的 Slack 频道? (带节点)
Using Microsoft Bot Framework, when I write on Messenger channel, how to push the result to a specific Slack channel ? (with Node)
目前我的用户正在使用 Messenger 频道。
事情是这样的:我的用户在我的机器人中输入 "Talk to a coach",然后他们可以写任何他们想写的东西,然后应该使用 Slack 将结果推送到特定的私人频道。
我怎样才能做到这一点?
编辑:私人频道是一个用户请求列表,可以在教练可用时处理,因此用户不必在等候名单中
您正在寻找将客户从机器人“移交给”人工代理的能力。
此主题在 official documentation and there are several samples available, for Node.js you may be interested in this one: https://github.com/palindromed/Bot-HandOff
中描述
我直接使用 Slack API 实现了它。我尝试了 .sourceEvent
,它几乎成功了:机器人只在 Slack 上私下与我交谈,他不接受任何频道 ID/名称(私人或非私人)。
这是我如何让它工作的(有点因式分解):
它从用户想要更改其计划中的任何内容(必须由教练手动完成)开始,因此它从对话 "/select_modify_planning"
开始
app.js:
...
bot.dialog("/select_modify_planning", require("./dialogs/modifyPlanning").select);
bot.dialog("/ask_for_request", require("./dialogs/modifyPlanning).askForRequest);
...
modifyPlanning.js:
const builder = require('botbuilder');
const rp = require('request-promise');
...
exports.select = [
(session, args) => {
session.sendTyping();
builder.Prompts.choice(session, "What do you want to declare ? :)", "Internal hours|Update rendezvous");
},
(session, results) => {
if (results.response) {
switch (results.response.entity) {
case "Internal hours":
session.beginDialog("/ask_for_request");
break;
case "Update rendezvous":
...
}
}
]
...
exports.askForRequest = [
(session, args) => {
session.sendTyping();
builder.Prompts.text(session, "Type your demand please:");
},
async (session, results) => {
try {
if (results.response)
await sendRequestToSlack(results.response);
session.endDialog("Your demand has been sent successfully, thank you ;)");
}
.catch(err) {
console.error(err);
session.endDialog("There was a problem while sending your demand to a coach, sorry :(");
}
}
]
...
const sendRequestToSlack = (textToSend) => {
var options = {
uri: "https://slack.com/api/chat.postMessage",
form: {
"token": "xoxb-XXXXXX-XXXXXX", // Your bot slack token
"channel": "XXXXXXXXX", // Your channel id (or name)
"text": textToSend
},
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}
return rp.post(options);
}
...
就在这里。
如果您想知道您的频道 ID(使用名称是一种不好的做法,因为它们可以更改),您可以使用此方法:https://api.slack.com/methods/channels.list for public channels, or this one: https://api.slack.com/methods/groups.list 对于私人频道。
如果是私人频道,你必须在你的机器人范围设置中添加良好的权限(groups.xxx
),然后重新安装它(你有一个绿色按钮专用于机器人设置)
希望一切都清楚:)
目前我的用户正在使用 Messenger 频道。
事情是这样的:我的用户在我的机器人中输入 "Talk to a coach",然后他们可以写任何他们想写的东西,然后应该使用 Slack 将结果推送到特定的私人频道。
我怎样才能做到这一点?
编辑:私人频道是一个用户请求列表,可以在教练可用时处理,因此用户不必在等候名单中
您正在寻找将客户从机器人“移交给”人工代理的能力。
此主题在 official documentation and there are several samples available, for Node.js you may be interested in this one: https://github.com/palindromed/Bot-HandOff
中描述我直接使用 Slack API 实现了它。我尝试了 .sourceEvent
,它几乎成功了:机器人只在 Slack 上私下与我交谈,他不接受任何频道 ID/名称(私人或非私人)。
这是我如何让它工作的(有点因式分解):
它从用户想要更改其计划中的任何内容(必须由教练手动完成)开始,因此它从对话 "/select_modify_planning"
app.js:
...
bot.dialog("/select_modify_planning", require("./dialogs/modifyPlanning").select);
bot.dialog("/ask_for_request", require("./dialogs/modifyPlanning).askForRequest);
...
modifyPlanning.js:
const builder = require('botbuilder');
const rp = require('request-promise');
...
exports.select = [
(session, args) => {
session.sendTyping();
builder.Prompts.choice(session, "What do you want to declare ? :)", "Internal hours|Update rendezvous");
},
(session, results) => {
if (results.response) {
switch (results.response.entity) {
case "Internal hours":
session.beginDialog("/ask_for_request");
break;
case "Update rendezvous":
...
}
}
]
...
exports.askForRequest = [
(session, args) => {
session.sendTyping();
builder.Prompts.text(session, "Type your demand please:");
},
async (session, results) => {
try {
if (results.response)
await sendRequestToSlack(results.response);
session.endDialog("Your demand has been sent successfully, thank you ;)");
}
.catch(err) {
console.error(err);
session.endDialog("There was a problem while sending your demand to a coach, sorry :(");
}
}
]
...
const sendRequestToSlack = (textToSend) => {
var options = {
uri: "https://slack.com/api/chat.postMessage",
form: {
"token": "xoxb-XXXXXX-XXXXXX", // Your bot slack token
"channel": "XXXXXXXXX", // Your channel id (or name)
"text": textToSend
},
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}
return rp.post(options);
}
...
就在这里。
如果您想知道您的频道 ID(使用名称是一种不好的做法,因为它们可以更改),您可以使用此方法:https://api.slack.com/methods/channels.list for public channels, or this one: https://api.slack.com/methods/groups.list 对于私人频道。
如果是私人频道,你必须在你的机器人范围设置中添加良好的权限(groups.xxx
),然后重新安装它(你有一个绿色按钮专用于机器人设置)
希望一切都清楚:)