taskman_atf/slack.inc.php

180 lines
4.7 KiB
PHP
Raw Permalink Normal View History

2022-08-04 14:59:17 +03:00
<?php
namespace taskman;
use Amp;
use Exception;
use CURLFile;
function slack_msg($token, $channel, $msg, $fields = [])
{
$ch = curl_init("https://slack.com/api/chat.postMessage");
$fields["token"] = $token;
if(!isset($fields['channel']))
$fields["channel"] = $channel;
$fields["text"] = $msg;
$fields["as_user"] = "bitgames bot";
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
function slack_upload_image($token, array $channels, $filepath)
{
$url = "https://slack.com/api/files.upload";
$cfile = new CURLFile(realpath($filepath), 'image/jpg', 'test.jpg');
$fields = array(
'token' => $token,
'channels' => implode(",", $channels),
'file' => $cfile,
'filename' => "test.jpg",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
curl_close($ch);
$arr = json_decode($result, true);
if(isset($arr['ok']) && $arr['ok'] == 1)
{
if(isset($arr['file']['permalink_public']))
return $arr['file']['permalink_public'];
}
return "";
}
function _atf_slack_chan()
{
return get("ATF_SLACK_CHANNEL");
}
function _atf_slack_chan_qa()
{
return get("ATF_SLACK_CHANNEL");
}
function _atf_slack_token()
{
$token = "xoxb-141599046048-1567053090644-2gDAoWGiZxFTtXGLMOOeLG2u";
return $token;
}
task('slack_test', function()
{
// createSlackThread();
// atf_slack_post('hi', array('channel' => _atf_slack_chan_qa()));
});
function atf_slack_post($message, $fields = array())
{
if(!get("ATF_SLACK"))
return array('ok' => true, 'ts' => 0);
$ch = curl_init("https://slack.com/api/chat.postMessage");
$fields["token"] = _atf_slack_token();
if(!isset($fields['channel']))
$fields["channel"] = _atf_slack_chan();
$fields["text"] = $message;
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
function atf_slack_get_permalink($message_ts, $channel = null)
{
if(!get("ATF_SLACK"))
return array('permalink' => '');
$ch = curl_init("https://slack.com/api/chat.getPermalink");
$fields["token"] = _atf_slack_token();
if($channel === null)
$channel = _atf_slack_chan();
$fields["channel"] = $channel;
$fields["message_ts"] = $message_ts;
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
function atf_slack_update($ts, array $attachments)
{
if(!get("ATF_SLACK"))
return array('ok' => true);
$ch = curl_init("https://slack.com/api/chat.update");
$fields["token"] = _atf_slack_token();
$fields["channel"] = _atf_slack_chan();
$fields["ts"] = $ts;
$fields["attachments"] = json_encode($attachments);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
function atf_slack_post_png($title, $png_file, $fields = array())
{
if(!get("ATF_SLACK"))
return array('ok' => true);
$file = new CurlFile($png_file, 'image/png');
$header = array();
$header[] = 'Content-Type: multipart/form-data';
$fields["token"] = _atf_slack_token();
$fields["channels"] = _atf_slack_chan();
$fields['file'] = $file;
$fields['title'] = $title;
$ch = curl_init("https://slack.com/api/files.upload");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}