PHP对接飞书机器人
Ps:因为公司用的飞书,我就研究了下飞书开放平台。
飞书自带的有定时机器人,但是想要接入自己第三方的数据就要自己来对接了。
效果图
接入飞书api需要得到公司管理员的同意(发布版本需要管理员审核)
进入后创建自定义应用,有个配置填入自己的xxx.xx.com/token.php文件
这个文件就是官方回调到你的入口,使用PHP获取input数据
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class FeishuAPI
{
private $app_id;
private $app_secret;
public function __construct($app_id, $app_secret)
{
$this->app_id = $app_id;
$this->app_secret = $app_secret;
}
public function getTenantAccessToken()
{
$client = new Client();
$data = [
"app_id" => $this->app_id,
"app_secret" => $this->app_secret
];
$response = $client->request('POST', 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal', ['json' => $data]);
$jsonToArray = json_decode($response->getBody()->getContents(), true);
return $jsonToArray['tenant_access_token'];
}
public function replyUser()
{
$rawData = file_get_contents('php://input');
$Server_data = json_decode($rawData, true);
$tent_token = $this->getTenantAccessToken();
$headers = [
'Content-Type' => 'application/json;charset=utf-8',
'Authorization' => 'Bearer ' . $tent_token
];
// 可以根据发送人的信息回复特定功能...
$data = [
'content' => json_encode([
'text' => "你好,我是基于Guzzle请求的Daixs Bot..."
]),
'msg_type' => 'text'
];
$client = new Client();
$response = $client->request('POST', "https://open.feishu.cn/open-apis/im/v1/messages/" . $Server_data['event']['message']['message_id'] . "/reply", [
'json' => $data,
'headers' => $headers
]);
}
}
$app_id = 'cli_xxxx';
$app_secret = 'xasxasdadasda';
$feishuAPI = new FeishuAPI($app_id, $app_secret);
$feishuAPI->replyUser();
已上传至Github