I'm adding blocks with actions to a bot reply. How do I get the response most efficiently?
All I can think of is listening to all messages, check if the actions parameter is set and has length, then checking if any of the actions has MY_BLOCK_123 as block_id...
await bot.replyPrivate(message, {
blocks: [
{
type: 'section',
text: {
type: 'plain_text',
text: 'What did you want to do?',
emoji: true,
},
},
{
type: 'actions',
block_id: 'MY_BLOCK_123',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: '猬嗭笍 Raise blinds',
emoji: true,
},
value: 'up',
},
{
type: 'button',
text: {
type: 'plain_text',
text: '猬囷笍 Lower blinds',
emoji: true,
},
value: 'down',
},
}
I have this issue as well and it is causing me a lot of pain right now. Makes me not want to use Botkit considering the poor support for Slack Blocks! :'(
Using the latest version of the Slack adapter, all the block actions will now have their primary value set to message.text, so they can be treated much like normal messages.
You can set up a specific handler for block actions like this:
controller.on('block_actions', async(bot, message) => { ... });
You can "hear" the value of a block action this way:
controller.hears(value, 'block_actions', async(bot, message) => { ... });
Or, you want to respond to different different actions based on their type, trigger your own events like so:
controller.on('block_actions', async(bot, message) => {
await controller.trigger(message.channelData.actions[0].type, bot, message)
});
Which would allow you to do things like:
controller.on('static_select', async(bot, message) => { ... });
EDIT: Upgrading the slack adapter solved the problem. Ignore the message below.
@benbrown I've tried your suggestions, but unfortunately it seems like this block_actions event never fires. I have a very simple handler:
controller.on('button', 'block_actions', async (bot, message) => console.log('Pressed button!'));
And this log never occurs.
I can do the following though and get the expected log statement...
controller.on('message,direct_message', async (bot, message) => {
if (message.action != null) {
for (const action of message.actions) {
if (action.value === 'button') {
console.log('Pressed button!');
}
}
}
});
Am I doing something wrong? Is there a bug in the event firing?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
Using the latest version of the Slack adapter, all the block actions will now have their primary value set to
message.text, so they can be treated much like normal messages.You can set up a specific handler for block actions like this:
You can "hear" the value of a block action this way:
Or, you want to respond to different different actions based on their type, trigger your own events like so:
Which would allow you to do things like: