Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic suggestions in slack dialogs. #116

Closed
wants to merge 4 commits into from

Conversation

kkaehler
Copy link

@kkaehler kkaehler commented Aug 3, 2018

No description provided.

@coveralls
Copy link

coveralls commented Aug 3, 2018

Pull Request Test Coverage Report for Build 378

  • 27 of 27 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 97.118%

Totals Coverage Status
Change from base Build 374: 0.2%
Covered Lines: 595
Relevant Lines: 595

💛 - Coveralls

@selfcontained
Copy link
Contributor

Hi @kkaehler, so I was catching up on dynamic selects inside of dialogs, and it turns out they're already supported inside of slapp with the existing options support. I can see our documentation is missing a few pieces about how it sets up that /sack/options http route for you though that we should add. Below is a full set of handlers to show what you'd need to do to provide dynamic options inside of a Slack dialog.

// Another module for building slack message json, optional
const smb = require('slack-message-builder')
const Slapp = require('slapp')

let slapp = Slapp({
  // ...whatever options you need for setting up Slapp
})

// Attach it to express, will use the default of '/slack/options' for the options route
slapp.attachToExpress(httpServer)

// Listen for a DM to the bot of "dialog_dynamic_menu" and provide a button for the dialog
slapp.message('dialog_dynamic_menu', ['direct_message'], msg => {
  let message = smb('Dialog w/ dynamic menu')
    .attachment()
      .callbackId('dialog/dynamic')
      .button('dyn_menu_dialog', 'Open Dialog')
        .value('my-dialog')
        .end()
      .end()

  msg.say(message.json())
})

// Handle the button click and open the dialog
slapp.action('dialog/dynamic', 'dyn_menu_dialog', msg => {
  let triggerId = msg.body.trigger_id

  let dialog = {
    callback_id: 'dialog/dynamic/submit',
    title: 'Dynamic Dropdown',
    submit_label: 'Request',
    elements: [
      {
        label: 'Dynamic Select',
        name: 'dynamic_select',
        type: 'select',
        data_source: 'external'
      }
    ]
  }
  msg.respond()

  let params = { trigger_id: triggerId, dialog, token: msg.meta.bot_token }
  slapp.client.dialog.open(params, (err, result) => {
    if (err) {
      console.error(err, `Error opening Slack dialog`)
    }
  })
})

// Provide a handler for dyanmic options
slapp.options('dialog/dynamic/submit', 'dynamic_select', (msg, values) => {
  let val = Array.isArray(values) ? values[0] : values
  // you could use this value to filter your options returned (it's what the user type in)
  console.log(val)

  let options = [
    {
      label: '[UXD-342] The button color should be artichoke green, not jalapeño',
      value: 'UXD-342'
    },
    {
      label: '[FE-459] Remove the marquee tag',
      value: 'FE-459'
    },
    {
      label: '[FE-238] Too many shades of gray in master CSS',
      value: 'FE-238'
    }
  ]

  msg.respond({ options })
})

// Handle submission of the dialog
slapp.dialog('dialog/dynamic/submit', (msg, submission) => {
  let value = submission.dynamic_select

  // Allows dialog to close successfully
  msg.respond()

  // Send a follow-up message with response
  let message = smb('Got your response')
    .attachment()
    .field('Dynamic Select')
    .value(value)
    .end()
    .end()

  msg.say(message.json())
})

@kkaehler
Copy link
Author

@selfcontained works for me! thanks for the example.

@kkaehler kkaehler closed this Aug 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants