PHP code example of carver / carver-msg-tools

1. Go to this page and download the library: Download carver/carver-msg-tools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carver / carver-msg-tools example snippets



// 引入composer 包
k
use Carver\CarverMsgTools\CarverRobotSdk;

try {
    // 实例化 test sdk
    $robotSdk = new CarverRobotSdk();
    //使用方法如下:

    //----------------------------单聊----------------------------
    $singleConfig = [
        'appKey'    => '机器人key',
        'appSecret' => '机器人秘钥',
        'userIds'   => ['1441442424'] //钉钉分配给每个用户的唯一id
    ];
    //单聊发送【文本】形式
    $sendContentData = ['content'=>'hello'];
    $result = $robotSdk->setSingleChatTextData($sendContentData)->sendSingleDingDingMsg($singleConfig);
    var_dump(json_decode($result, true));   
     
    //单聊发送【markdown】形式
    $sendContentData = ['title'=>'状态标题,不显示','text'=>'hello'];
    $result = $robotSdk->setSingleChatMarkdownData($sendContentData)->sendSingleDingDingMsg($singleConfig);
    var_dump(json_decode($result, true));

    //----------------------------群聊----------------------------
    //方式1:【直接使用创建机器人的webHook】
    $webHook = ['webHook' => '机器人的webhook'];
    $sendContentData =[
        'msgType'=> 1,                  // 1. text文本格式  2. markdown格式
        'msgReceiver'=>['17515487857'], // 被@人的手机号列表
        'msgContent'=>'发送的内容'        // 发送的内容 如果参数 msgType = 2 可以添加自定义的markdown样式
    ];
    $data    = $robotSdk->setWebHook($webHook)->sendGroupDingDingMsg($sendContentData);
    var_dump(json_decode($result, true));

    //方式2:【通过验签方式自动生成webhook】
    $webHook = ['secret' => '机器人秘钥','accessToken'=>'机器人验签的token'];
    $data    = $robotSdk->setWebHookBySign($webHook)->sendGroupDingDingMsg($sendContentData);
    var_dump(json_decode($result, true));

} catch (\Exception $e) {
    var_dump($e->getMessage());
}