Testing Slack Interactive Messages Locally

2019-05-30 04:49发布

问题:

I'm currently using SlackConnector Repo https://github.com/noobot/SlackConnector. I've created a bot and it sends interactive messages to my chat. I would like to add functionality to my interactive buttons but upon clicking them i get this response. Darn – that didn’t work. Only Slack Apps can add interactive elements to messages. Manage your apps here: https://api.slack.com/apps/ So it looks like I need a request URL to get my past my current roadblock. Is there a way to Test the Interactive Message button locally?

List<SlackAttachment> attachments = new List<SlackAttachment>();
List<SlackAttachmentAction> actions = new List<SlackAttachmentAction>();
actions.Add(new SlackAttachmentAction
{
    Name  = "game",
    Text  = "chess",
    Type  = "button",
    Value = "Chess"
});
actions.Add(new SlackAttachmentAction
{
    Name  = "game",
    Text  = "Falken's Maze",
    Type  = "button",
    Value = "Maze"
});
actions.Add( new SlackAttachmentAction
{
    Name  = "game",
    Text  = "Thermonuclear War",
    Type  = "danger",
    Value = "war"
});

attachments.Add(new SlackAttachment
{
    Text       = "Choose a game to play",
    Fallback   = "You are unable to choose a game",
    CallbackId = "wopr_game",
    ColorHex   =  "#3AA3E3",
    Actions    = actions

});
connection.Say(new BotMessage
{
    ChatHub = chatHub,
    Text = "Usage: !talk <user>",
    Attachments = attachments
});

return Task.CompletedTask;

One thing I tried was I set the request URL to use a url generated from https://webhook.site/#/ and I still get the same response upon clicking

回答1:

It looks to me like you have two problems.

You don't have a Slack app

Interactive Messages only work if you have a registered Slack app. That is why you got that error message. But you can easily create one. Just go here and click on "Create a new app". One reason you need one is that you need to tell Slack to which URL to send the request, after a user clicks a button.

Slack can't reach your local app

Slack's interactive messages will only work with apps that can be reached from the public Internet. So if you want to develop your app locally you need to open your web server to the Internet. There are many ways to do it, one secure way is to use a VPN tunnel service. One provider for this kind of service is ngrok, which is also recommended in the official Slack tutorials. I use it myself and it works great.