Last active
January 17, 2018 02:45
-
-
Save nathandaly/302a65143d5499b427c8c14c4c3ea564 to your computer and use it in GitHub Desktop.
Slack direct message delete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once './vendor/autoload.php'; | |
use GuzzleHttp\Pool; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Psr7\Request; | |
$apiUrl = 'https://slack.com/api/'; // API URL. | |
$apiToken = 'xoxp-xxxx-xxxx-xxxx-xxxx'; // Slack Legacy API Token. | |
$channel = 'D8783SG54'; // Channel ID. | |
$conversationLimit = 1000; // Amount of conversation in history to delete. | |
$users = [ | |
'U8YGMLGJ4' // Only delete messages belonging to user ID's contained in this array. | |
]; | |
// Get conversation history for timestamps. | |
$httpClient = new Client(); | |
$response = $httpClient->request('GET', $apiUrl . 'conversations.history', [ | |
'query' => [ | |
'token' => $apiToken, | |
'channel' => $channel, | |
'limit' => $conversationLimit | |
] | |
]); | |
$conversationHistory = json_decode($response->getBody(), true); | |
$requests = function ($messages) use ($apiUrl, $apiToken, $channel, $users) { | |
$uri = $apiUrl . 'chat.delete'; | |
foreach ($messages as $message) { | |
if (in_array($message['user'], $users)) { | |
yield new Request( | |
'POST', | |
$uri, [ | |
'Content-Type' => 'application/x-www-form-urlencoded' | |
], | |
http_build_query([ | |
'token' => $apiToken, | |
'channel' => $channel, | |
'ts' => $message['ts'] | |
], null, '&') | |
); | |
sleep(1); | |
} | |
} | |
}; | |
if ($conversationHistory['ok']) { // $response->getStatusCode(); | |
$pool = new Pool($httpClient, $requests($conversationHistory['messages']), [ | |
'concurrency' => 5, | |
'fulfilled' => function ($response, $index) { | |
}, | |
'rejected' => function ($reason, $index) { | |
var_dump($reason); | |
}, | |
]); | |
// Initiate the transfers and create a promise. | |
$promise = $pool->promise(); | |
// Force the pool of requests to complete. | |
$promise->wait(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment