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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing along state to Interactive Messages handlers #59

Closed
SamirBoulil opened this issue Nov 23, 2016 · 2 comments
Closed

Passing along state to Interactive Messages handlers #59

SamirBoulil opened this issue Nov 23, 2016 · 2 comments

Comments

@SamirBoulil
Copy link

Hi,

I used the starter-slapp-app as a bootstrap project for my bot. It really works fine and wanted to give you a big round of applause for the good work 馃憤 .

I was simply wondering if there was a way to pass along a state to interactive messages handlers.

Here is some code:
The interactive message I want to send with the state:

// This route is invoked previously where state is initialized.
slapp.route('handle_match_confirmation', (msg, state, duration) => {
  msg
  .say({
    channel: state.loserId,
    text: '',
    attachments: [{
      fallback: 'Match log confirmation',
      title: `Do you confirm that you lost ${state.winnerScore}-${state.loserScore} against <@${state.winnerId}> ?`,
      callback_id: 'match_confirmation_callback',
      color: '#3AA3E3',
      attachment_type: 'default',
      actions: [{
        name: 'match_confirmation',
        text: 'Yep, good game.',
        style: 'primary',
        type: 'button',
        value: 'yes'
      },
      {
        name: 'match_confirmation',
        text: 'NO WAY ! That\' a lie!',
        type: 'button',
        value: 'no'
      }]
    }]
  });
});

My handlers supposed to know the state:

slapp.action('match_confirmation_callback', 'match_confirmation', 'yes', (msg, value) => {
  msg.respond('Too bad, maybe next time :wink:!')
});
slapp.action('match_confirmation_callback', 'match_confirmation', 'no', (msg, value) => {
  msg.respond('What what what ? Someone is trying to cheat then. You need to see this IRL!')
});

The reason I want to pass the state along is because it contains slack_ids I use to send messages + other information regarding the business logic.

Thank you ;)

@mbrevoort
Copy link
Contributor

mbrevoort commented Nov 29, 2016

Hi, thanks :)

Yes, actually the way we've done this is to serialize JSON into the value of the button action.

So we have utility functions like this:

let common = {

    // args takes a JSON stringify'd string and returns an object,
    // fails silently returning an empty object
    unmarshall: (str) => {
      try {
        return JSON.parse(str)
      } catch (ex) {
        console.error(`Error unmarshalling ${str}`, ex)
        return {}
      }
    },

    marshall: JSON.stringify
}

So then your button actions might look something like this:

      actions: [{
        name: 'match_confirmation_yes',
        text: 'Yep, good game.',
        style: 'primary',
        type: 'button',
        value: common.marshall({ id: 'some-id', value: 'yes' })
      },
      {
        name: 'match_confirmation_no',
        text: 'NO WAY ! That\' a lie!',
        type: 'button',
        value: common.marshall({ id: 'some-id', value: 'no' })
      }]

And your action handlers would unmarshall the value and then have an object to deal with:

slapp.action('match_confirmation_callback', 'match_confirmation_yes', (msg, args) => {
  args = common.unmarshall(args)
  // args.id is 'some-id'
  msg.respond('Too bad, maybe next time :wink:!')
});

slapp.action('match_confirmation_callback', 'match_confirmation_no', (msg, args) => {
  args = common.unmarshall(args)
  // args.id is 'some-id'
  msg.respond('What what what ? Someone is trying to cheat then. You need to see this IRL!')
});

@SamirBoulil
Copy link
Author

Ok that's a neat trick ! It could be awesome to have the state being handled the same way routes does.

Thank you ;)

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

No branches or pull requests

2 participants