Created
August 30, 2021 17:28
-
-
Save filmaj/74fe823ba5b21f6ca4dbc16068deaebb to your computer and use it in GitHub Desktop.
Simple bolt-js Slack app with a view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { App } = require('@slack/bolt'); | |
// Initializes your app with your bot token and signing secret | |
const app = new App({ | |
token: process.env.SLACK_BOT_TOKEN, | |
signingSecret: process.env.SLACK_SIGNING_SECRET, | |
socketMode: true, | |
appToken: process.env.SLACK_APP_TOKEN | |
}); | |
// Listens to incoming messages that contain "hello" | |
app.message('hello', async ({ message, say }) => { | |
// say() sends a message to the channel where the event was triggered | |
console.log('hello: say()'); | |
await say({ | |
blocks: [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": `Hey there <@${message.user}>!` | |
}, | |
"accessory": { | |
"type": "button", | |
"text": { | |
"type": "plain_text", | |
"text": "Open View" | |
}, | |
"action_id": "button_click" | |
} | |
} | |
], | |
text: `Hey there <@${message.user}>!` | |
}); | |
}); | |
app.action('button_click', async ({ body, ack, say, client }) => { | |
console.log('button_click: ack && view.open'); | |
// Acknowledge the action | |
await ack(); | |
const result = await client.views.open({ | |
// Pass a valid trigger_id within 3 seconds of receiving it | |
trigger_id: body.trigger_id, | |
// View payload | |
view: { | |
type: 'modal', | |
// View identifier | |
callback_id: 'quiz_view', | |
title: { | |
type: 'plain_text', | |
text: 'Modal title' | |
}, | |
blocks: [ | |
{ | |
type: 'section', | |
text: { | |
type: 'mrkdwn', | |
text: 'Welcome to a modal with _blocks_' | |
}, | |
accessory: { | |
type: 'button', | |
text: { | |
type: 'plain_text', | |
text: 'Click me!' | |
}, | |
action_id: 'button_abc' | |
} | |
}, | |
{ | |
type: 'input', | |
block_id: 'input_c', | |
label: { | |
type: 'plain_text', | |
text: 'What are your hopes and dreams?' | |
}, | |
element: { | |
type: 'plain_text_input', | |
action_id: 'dreamy_input', | |
multiline: true | |
} | |
} | |
], | |
submit: { | |
type: 'plain_text', | |
text: 'Submit' | |
} | |
} | |
}); | |
app.view("quiz_view", async ({ ack, body, view, context, client }) => { | |
await ack(); | |
const user = body['user']['id']; | |
console.log('inside quiz view, user', user, JSON.stringify(view.state, null, 2)); | |
const userInput = view.state.values.input_c.dreamy_input.value; | |
await client.chat.postMessage({ | |
channel: user, | |
text: `You told me "${userInput}"!` | |
}); | |
}); | |
}); | |
(async () => { | |
// Start your app | |
await app.start(process.env.PORT || 3000); | |
console.log('⚡️ Bolt app is running!'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment