目前,我们正在使用 Microsoft BotFramework 构建 Slack 机器人,但开发和积极支持的内容存在很多混乱,以至于它成为一个又一个的障碍。
看到上面的内容,由于我们有 dotnet 开发人员,我们决定继续使用 dotnet SDK
现在,社区botbuilder-community-dotnet支持一个单独的 Slack 适配器。我们必须使用它,因为我们想要一些 Azure Slack 通道不支持的功能(例如 BlockKit 消息)。 这个库有很多问题,没有积极开发并且缺乏示例。
我已经修复了一些问题并为它们创建了 PR
我也提出了问题
所以我的问题是,使用 Microsoft BotFramework 和 Slack 进行开发的最新方法是什么,以便我们可以使用 BlockKit、作为线程回复等,并获得积极的社区支持?
在 OP 的支持下发布这个 JavaScript 解决方案。另外,这是我自己的机器人的精简实现,主要用于演示。按原样,除了通过单个瀑布步骤发送块套件之外,它没有做太多事情。
首先,在generateWelcome.js 中,我向位于Incoming Webhooks 下的Slack 应用程序中生成的Webhook 发出POST 请求。此请求还包括 Slack 应用程序的 Bot 用户 OAuth 令牌,位于 Authorization
标头中的
OAuth & Permissions下。正文包含我的块套件的字符串化 JSON。不幸的是,我不记得哪些范围是必要的,因为我启用了多个范围(用于测试)。所以,你可能需要研究一下。
生成SlackWelcome.js
const fetch = require('node-fetch');
module.exports = async (context) => {
const { membersAdded } = context.activity;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await fetch(
'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX',
{
method: 'POST',
headers: {
Host: 'hooks.slack.com',
Accept: '*/*',
Authorization: 'Bearer xoxb-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXppq3e',
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: 'Now back in stock! :tada:',
attachments: [
{
title: 'The Further Adventures of Slackbot',
author_name: 'Stanford S. Strickland',
author_icon: 'https://api.slack.com/img/api/homepage_custom_integrations-2x.png',
image_url: 'http://i.imgur.com/OJkaVOI.jpg?1',
},
{
fields: [
{
title: 'Volume',
value: '1',
short: true,
},
{
title: 'Issue',
value: '3',
short: true,
},
],
},
{
title: 'Synopsis',
text: 'After Johnny pushed exciting changes to a devious new branch back in Issue 1, Slackbot notifies Marco about an unexpected deploy...',
},
{
fallback: 'Would you recommend it to customers?',
title: 'Would you recommend it to customers?',
callback_id: 'comic_1234_xyz',
color: '#3AA3E3',
attachment_type: 'default',
actions: [
{
name: 'recommend',
text: 'Recommend',
type: 'button',
value: 'recommend',
},
{
name: 'no',
text: 'No',
type: 'button',
value: 'bad',
},
],
},
],
}),
}
).then(res => {
if (res.ok) return;
throw new Error(res.statusText);
});
break;
}
}
};
在 slackDialog.js 中,我将从generateSlackWelcome.js 导出的函数导入为
slackWelcome
。我像往常一样构建对话框,并在瀑布步骤中调用 slackWelcome
,然后通过 Webhook 将块工具包消息发布到 Slack 通道(也已指定)。
slackDialog.js
const { ComponentDialog, WaterfallDialog } = require('botbuilder-dialogs');
const slackWelcome = require('./resources/scripts/slackWelcome');
const SLACK_DIALOG = 'slackDialog';
class SlackDialog extends ComponentDialog {
constructor(id) {
super(id);
this.addDialog(new WaterfallDialog(SLACK_DIALOG, [
this.slackStep.bind(this),
]));
this.initialDialogId = SLACK_DIALOG;
}
async slackStep(stepContext) {
const { context: { activity: { channelId } } = stepContext;
if (channelId === 'slack') {
return await slackWelcome(stepContext);
}
}
}
希望有帮助!