phpslack-dialog

Trigger ID not found with Slack slash command?


My goal is to be able to use a slash command to open a dialog and process the feedback into a database. I am trying to get the dialog to open but am getting an error regarding the slash command where it says "trigger_id" not found.

When I run it from my slack, I get the output of

'{"ok":false,"error":"invalid_arguments","response_metadata":{"messages":["[ERROR] missing required field: trigger_id"]}}'

I have tried some debugging and output the trigger_id to the screen and find that the trigger_id is indeed null. What am I missing to pass this?

I admit that I am new to the slack realm. I have followed (I think) the documentation from the slack site on setting up the app correctly.

Am I missing something with my slack app setup or something in my code that is causing this error message?

Thank you in advance for your time!

<?
$command    = $_POST['command'];
$text       = $_POST['text'];
$token      = $_POST['token'];
$cn         = $_POST['channel_id'];
$user_id    = $_POST['user_id'];
$triggerid  = $_POST['trigger_id'];

// define the dialog for the user (from Slack documentation example)
$dialog = [
    'callback_id' => 'validres-3100',
    'title' => 'Test',
    'submit_label' => 'Submit',
    'elements' => [
        [
            'type' => 'text',
            'label' => 'Test Field 1',
            'name' => 'field_1'
        ],
        [
            'type' => 'text',
            'label' => 'Test Field 2',
            'name' => 'field_2'
        ]
    ]
];

// define POST query parameters
$query = [
        'token' => '<my api auth code>',
        'dialog' => json_encode($dialog),
        'trigger_id' => $triggerid
];

// define the curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));

// execute curl request
$response = curl_exec($ch);

// close
curl_close($ch);

var_export($response);
?>


Solution

  • To open dialog box in slack you can use this api "https://slack.com/api/views.open". With api you need to send trigger id which is valid only for 3 seconds. Your url will looks like : "https://slack.com/api/views.open?trigger_id=" + "xxxx.xxxxxxxxx.xxxxxxxxxxxx" + "&view=your data". with this request you need to send token with your post request like :- (URL,"POST", { "token" ,"xoxb-xxxxxx-xxxxx-xxxxxxx"});

    Need to add view.open API in your slack app also for this use following step: Use "Bot User OAuth Access Token" , In "OAuth and permissions Tab" Format is xoxb-xxxxx-xxxxx-xxxx. And then add scope "views:open" and reinstall your app in slack. And then try to get open view dialog.

    Hope this will be helpful.