【发布时间】:2019-06-20 18:39:53
【问题描述】:
我正在尝试使用 Slack 的交互式消息为部分和按钮发布 slack 块套件 JSON。但是我收到一条错误消息,提示 Invalid ChannelData
我尝试通过 slack api 测试器发布块 JSON,它可以工作,但不能通过我编写的代码。我写了一些自定义代码来覆盖bot框架中专门针对slack频道的提示功能。
覆盖提示
public async prompt(stepContext: WaterfallStepContext, dialogId: string, options: PromptOptions): Promise<DialogTurnResult> {
let promptText: string = '';
if (isString(options.prompt)) {
promptText = options.prompt;
} else if (isActivity(options.prompt)) {
promptText = options.prompt.text;
}
this.logger.log(`prompt: ${promptText}`);
const choices = options.choices.map((choice) => {
let text = '';
this.logger.log(`choice: ${choice}`);
if (isString(choice)) {
text = choice;
} else if (isChoice(choice)) {
text = choice.value;
}
// return { name: text, type: 'button', text: text, value: text };
return {
type: 'button',
text: {
type: "plain_text",
text: text
},
value: text
};
});
const channelData = {
text: '',
blocks: [
{
type: "section",
text:
{
type: "mrkdwn",
text: "Are you using a Mac or PC?"
}
},
{
type: "actions",
elements: choices
}]
};
this.logger.log(`channelData: ${JSON.stringify(channelData)}`);
return await stepContext.prompt('ChoicePrompt', { type: ActivityTypes.Message, channelData: channelData });
}
辅助函数:
function isString(str: any): str is string {
return typeof str === 'string';
}
function isActivity(obj: any): obj is Activity {
return obj && obj.text !== undefined;
}
function isChoice(obj: any): obj is Choice {
return obj && obj.value !== undefined;
}
调用:
const channel = getChannel(step.context);
return await channel.prompt(step, 'ChoicePrompt', {
choices: buttons,
prompt: this.generateAssetInfoMessage(deviceType),
retryPrompt: `Sorry, I didn't understand. Please choose one of the following:`,
});
我希望看到带有两个按钮的消息,但我收到一条错误消息 [onTurnError]:错误:无效的 ChannelData 这是字符串化的通道数据
{"text":"","blocks":[{"type":"section","text":{"type":"mrkdwn","text":"Are you using a Mac or PC?"}},{"type":"actions","elements":[{"type":"button","text":{"type":"plain_text","text":"I'm using a Mac"},"value":"I'm using a Mac"},{"type":"button","text":{"type":"plain_text","text":"I'm using Windows"},"value":"I'm using Windows"}]}]}
【问题讨论】:
标签: typescript botframework slack-api