Making WordPress.org

Changeset 10828


Ignore:
Timestamp:
03/18/2021 03:11:43 AM (4 years ago)
Author:
dd32
Message:

Slack: Update the /subgroup command, take three.

Shift the response to the /subgroup command to async ephemeral responses. Unfortunately the replace_original flag seems not to work.
Slack requires that the response to the slash command be a 200 response within 3000ms, unfortunately due to the migration to their newer API's this API now often takes longer than that.

See [10823] and [10824].

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/dotorg/slack/subgroup.php

    r10824 r10828  
    2121}
    2222
     23// WARNING: Can only be called FIVE times per request.
     24function update_text( $text ) {
     25    if ( empty( $_POST['response_url'] ) ) {
     26        die( "response_url missing." );
     27    }
     28
     29    $payload = [
     30        'response_type'    => 'ephemeral',
     31        'replace_original' => 'true', // Doesn't work.
     32        'text'             => $text,
     33    ];
     34
     35    $context = stream_context_create( [ 'http' => [
     36        'method'  => 'POST',
     37        'header'  => 'Content-Type: application/json',
     38        'content' => json_encode( $payload ),
     39    ] ] );
     40
     41    file_get_contents( $_POST['response_url'], false, $context );
     42}
     43
     44function die_text( $text ) {
     45    update_text( $text );
     46    die();
     47}
     48
    2349// Confirm it came from Slack.
    2450if ( $_POST['token'] !== WEBHOOK_TOKEN ) {
    2551    die( "Invalid Token" );
    2652}
     53
     54// This API needs to return a successful 200 ASAP, within 3 seconds, and most of the API calls below will take longer than.
     55header( 'HTTP/1.0 200 OK' );
     56flush();
     57if ( function_exists( 'fastcgi_finish_request' ) ) {
     58    fastcgi_finish_request();
     59}
     60
     61update_text( 'Please wait.. This could take a moment..' );
    2762
    2863$current_group_id = $_POST['channel_id'];
     
    5287// Sorry - @wordpressdotorg isn't in this group.
    5388if ( empty( $found ) ) {
    54     die( "@wordpressdotorg isn't here." );
     89    die_text( "@wordpressdotorg isn't here." );
    5590}
    5691
    5792// Can't call this from a subgroup.
    5893if ( $pos = strpos( $group['name'], '-' ) ) {
    59     die( sprintf( 'Call this from the main `%s` group.', substr( $group['name'], 0, $pos ) ) );
     94    die_text( sprintf( 'Call this from the main `%s` group.', substr( $group['name'], 0, $pos ) ) );
    6095}
    6196
     
    72107// Ensure the calling user is in the main group.
    73108if ( ! in_array( $_POST['user_id'], $members, true ) ) {
    74     die;
     109    die_text( 'Unauthorized' );
    75110}
    76111
     
    81116    case 'create':
    82117        if ( 0 !== strpos( $subcommand, $group['name'] . '-' ) ) {
    83             die( "Must create a group that starts with `{$group['name']}-`." );
     118            die_text( "Must create a group that starts with `{$group['name']}-`." );
    84119        }
    85120
     
    93128        );
    94129        if ( empty( $new_group['ok'] ) ) {
    95             die( "Group creation failed. Does it already exist?" );
     130            die_text( "Group creation failed. Does it already exist?" );
    96131        }
    97132
     
    122157        );
    123158
    124         die( sprintf( "Group %s created.", $new_group['group']['name'] ) );
     159        die_text( sprintf( "Group %s created.", $new_group['group']['name'] ) );
    125160
    126161    case 'invite':
     
    137172                );
    138173
    139                 die( "Invited to {$group['name']}." );
     174                die_text( "Invited to {$group['name']}." );
    140175            }
    141176        }
    142177
    143         die( "$subcommand group not found." );
     178        die_text( "$subcommand group not found." );
    144179
    145180    case 'list':
     
    167202
    168203        if ( $groups_to_add ) {
    169             echo "You may join any of these groups:\n -- `" . implode( "`\n -- `", $groups_to_add ) . "`\n\n\n";
     204            $text = "You may join any of these groups:\n -- `" . implode( "`\n -- `", $groups_to_add ) . "`\n\n\n";
    170205        } else {
    171             echo "You are in all {$parent_group['name']} subgroups that the @wordpressdotorg user knows about.\n\n";
    172         }
    173 
    174         die( "*Help:*\n`/subgroup list` - display this message\n`/subgroup join {name}` - join a subgroup\n`/subgroup create {name}` - create a subgroup" );
    175 }
    176 
     206            $text = "You are in all {$parent_group['name']} subgroups that the @wordpressdotorg user knows about.\n\n";
     207        }
     208
     209        die_text( $text . "*Help:*\n`/subgroup list` - display this message\n`/subgroup join {name}` - join a subgroup\n`/subgroup create {name}` - create a subgroup" );
     210}
     211
Note: See TracChangeset for help on using the changeset viewer.