Created
February 18, 2020 11:10
-
-
Save blessani/3d10ea096fd1d748e401aa7ec44a4fd1 to your computer and use it in GitHub Desktop.
PHP script to delete all messages in Slack channel
This file contains hidden or 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 | |
class Slack | |
{ | |
private $token = ''; | |
private $channel = ''; | |
public function __construct() | |
{ | |
} | |
public function deleteAllChannelMessages() | |
{ | |
$data = file_get_contents(sprintf('https://slack.com/api/conversations.history?token=%s&channel=%s', $this->token, $this->channel)); | |
$jsonData = json_decode($data, JSON_PRETTY_PRINT); | |
foreach ($jsonData['messages'] as $message) { | |
$this->deleteMessage($this->token, $this->channel, $message['ts']); | |
printf("Deleting message %s\n", $message['ts']); | |
} | |
} | |
private function deleteMessage($token, $channel, $ts) | |
{ | |
$url = 'https://slack.com/api/chat.delete'; | |
$fields = [ | |
'token' => urlencode($token), | |
'channel' => urlencode($channel), | |
'ts' => urlencode($ts), | |
]; | |
$fields_string = ''; | |
foreach ($fields as $key=>$value) { | |
$fields_string .= $key.'='.$value.'&'; | |
} | |
rtrim($fields_string, '&'); | |
$ch = curl_init(); | |
curl_setopt($ch,CURLOPT_URL, $url); | |
curl_setopt($ch,CURLOPT_POST, count($fields)); | |
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
} | |
} | |
$slack = new Slack(); | |
$slack->deleteAllChannelMessages(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment