Changeset 11878
- Timestamp:
- 05/25/2022 03:05:50 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/common/includes/slack/announce/lib.php
r11875 r11878 5 5 6 6 require_once __DIR__ . '/config.php'; 7 8 function api_call( $method, $content = array() ) { 9 $content['token'] = SLACK_TOKEN; 10 $content = http_build_query( $content ); 11 $context = stream_context_create( array( 12 'http' => array( 13 'method' => 'POST', 14 'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL, 15 'content' => $content, 16 ), 17 ) ); 18 19 $response = file_get_contents( 'https://slack.com/api/' . $method, false, $context ); 20 return json_decode( $response, true ); 21 } 22 23 function get_channel_info( $channel_id ) { 24 $channel_info = api_call( 25 'conversations.info', 26 array( 27 'channel' => $channel_id, 28 ) 29 ); 30 31 return $channel_info['channel'] ?? false; 32 } 7 33 8 34 function get_whitelist_for_channel( $channel ) { … … 129 155 global $wpdb; 130 156 131 $channel = $data['channel_name']; 132 $user = false; 157 /* Respond with a 200 ASAP. 158 * The inline API calls might delay this more than 3s, which will cause Slack to error (or retry). 159 * We don't need to respond with the body until later, but the 200 header must make it back within 3s. 160 */ 161 http_response_code( 200 ); 162 ignore_user_abort( true ); 163 flush(); 164 165 $channel = $data['channel_name']; 166 $channel_id = $data['channel_id']; 167 $user = false; 133 168 $slack_profiledata = false; 169 $channel_info = false; 170 171 // Slack sends the channel_name as 'privategroup' for old private channels, but the actual private channel name for newer private channels. 172 if ( 'privategroup' !== $channel ) { 173 $channel_info = get_channel_info( $channel_id ); 174 175 if ( 176 $channel_info && 177 ( channel_info['is_private'] || $channel_info['is_group'] || $channel_info['is_mpim'] ) 178 ) { 179 $channel = 'privategroup'; 180 } 181 } 134 182 135 183 // Find the user_login for the Slack user_id … … 203 251 204 252 // By sending the channel ID, we can post to private groups. 205 $send->send( $data['channel_id'] ); 206 207 // If it was broadcast in a private channel, don't try to broadcast to the public parent channel. 208 if ( 'privategroup' === $channel ) { 209 return; 210 } 253 $send->send( $channel_id ); 211 254 212 255 // Broadcast this message as a non-@here to the "parent" channel too. … … 227 270 $send->set_text( 'In #' . $channel . ': ' . $text ); 228 271 $send->send( '#' . $parent_channel ); 229 230 } 231 272 } 273
Note: See TracChangeset
for help on using the changeset viewer.