无法使用互动消息在松弛状态下呈现bot框架提示

时间:2019-06-20 18:39:53

标签: typescript botframework slack-api

我正在尝试使用Slack的交互式消息为部分和按钮发布Slack块工具包JSON。但是我收到一条错误消息,指出无效的ChannelData

我尝试通过slack api测试程序发布块JSON,它可以工作,但不能通过我编写的代码来工作。我写了一些自定义代码来覆盖bot框架中专门针对松弛通道的提示功能。

覆盖的提示

   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"}]}]}

1 个答案:

答案 0 :(得分:1)

不幸的是,您没有做错任何事情。我们只是不支持。

我已经提交了Design Change Request,以便我们可以将channelData的有效负载映射到适当的Slack API。 您也无法在本地执行任何操作,因为这依赖于API的私有/安全部分。

但是请注意,一旦实施,您可能会像这样将其添加到邮件的channelData中:

"channelData": {
   "text": "Now back in stock! :tada:",
   "blocks": [
        [...]
   ]

编辑:您可以可以在本地执行此操作,但是这很困难,因为您无法直接使用Bot Framework进行此操作。您必须在机器人内部使用Slack's Conversation API。有可能,除了需要新的身份验证令牌以外,您将拥有所有需要的数据。