You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Microsoft Bot Framework发送Slack附件?

用Microsoft Bot Framework发送带交互附件的Slack消息

Hey there, let's get that Slack message with attachments working in your Bot Framework bot. I'll break down what you need to know, fix up your example code, and cover the key bits for handling user interactions too.

First off, when sending Slack-specific rich messages (like attachments with interactive buttons/selectors) through Bot Framework, you need to use the channelData field in your Activity object. This tells the Slack adapter to pass through the exact Slack API-compliant structure directly to Slack without modifying it.

Here's your adjusted, working code example (Node.js Bot Framework v4):

async sendSlackWinnerPoll(turnContext) {
    // Build the Slack message structure exactly as Slack expects it
    const slackMessage = {
        "text": "I hope the tour went well, Mr. Wonka.",
        "response_type": "in_channel", // Makes the message visible to everyone in the channel
        "attachments": [
            {
                "text": "Who wins the lifetime supply of chocolate?",
                "fallback": "You could be telling the computer exactly what it can do with a lifetime supply of chocolate.",
                "color": "#3AA3E3",
                "attachment_type": "default",
                "callback_id": "select_simple_1234", // Unique ID to identify this interaction later
                "actions": [
                    // Important: Slack actions need complete definitions—your original was missing key fields
                    {
                        "name": "winners_list",
                        "type": "select", // This specifies a dropdown menu
                        "text": "Pick a winner",
                        "options": [
                            { "text": "Charlie Bucket", "value": "charlie" },
                            { "text": "Veruca Salt", "value": "veruca" }
                        ]
                    }
                ]
            }
        ]
    };

    // Create a Bot Framework Activity and embed the Slack message in channelData
    const outgoingActivity = {
        type: 'message',
        channelData: slackMessage
    };

    // Send the message to Slack
    await turnContext.sendActivity(outgoingActivity);
}

Key things to note:

  • channelData is non-negotiable: Bot Framework is cross-platform, so any platform-specific formatting (like Slack attachments) has to live here. The Slack adapter will forward this straight to the Slack API.
  • Complete your action definitions: Your original code only had a name in the actions array—Slack needs more context! I added a select type with options, but if you want buttons instead, use type: "button" and add a value field.
  • Fallback text is mandatory: Always include a fallback for clients that don't support attachments—Slack might reject your message if this is missing.
  • response_type: "in_channel": This ensures the message is posted publicly in the channel instead of just being a DM to the user, which matches your original intent.

Handling user interactions (when someone selects a winner):

When a user interacts with your attachment (like picking a winner from the dropdown), Slack sends an invoke request to your bot. You need to handle this in your bot's turn logic:

async onTurn(turnContext) {
    // Check if this is an invoke request from Slack (interaction callback)
    if (turnContext.activity.type === 'invoke') {
        const slackPayload = turnContext.activity.value;
        
        // Match the callback_id from your attachment to identify this interaction
        if (slackPayload.callback_id === 'select_simple_1234') {
            // Get the user's selected value
            const selectedValue = slackPayload.actions[0].selected_options[0].value;
            const winner = selectedValue === 'charlie' ? 'Charlie Bucket' : 'Veruca Salt';
            
            // Send a confirmation message back to the channel
            await turnContext.sendActivity(`🎉 ${winner} has won the lifetime supply of chocolate!`);
            
            // Send a 200 response to Slack to avoid timeout errors
            await turnContext.sendActivity({
                type: 'invokeResponse',
                value: { status: 200 }
            });
        }
    }

    // Handle other message types or continue your bot logic...
    await this.nextTurn(turnContext);
}

Final checks:

  • Make sure your bot is set up with the Slack Adapter—you'll need a Slack Bot Token and Signing Secret configured in your bot's settings.
  • For Node.js, ensure you've installed the botbuilder-adapter-slack package via npm. For C#, use the Microsoft.Bot.Builder.Adapters.Slack NuGet package.

内容的提问来源于stack exchange,提问作者Ajaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:53:57