使用PHP对接飞书机器人

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值