PHP code example of xin6841414 / work-wechat-robot

1. Go to this page and download the library: Download xin6841414/work-wechat-robot 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/ */

    

xin6841414 / work-wechat-robot example snippets


    WORK_WECHAT_GROUP_ROBOT_DEFAULT_NUM=1
    WORK_WECHAT_GROUP_ROBOT_DEFAULT_KEY_1=151de1af-f525****f5ea2186d4a0 //你的机器人key

use Xin6841414\WorkWechatRobot\Robot;

$robot = app('workwechatrobot');
//或者 
$robot = app(Robot::class);

    $robot->with('other');

$response = $robot->text('测试消息');
//response
[
    'errcode' => 0,
    'errmsg' => 'ok',
]

    $user = [
        'ZhangSan',
        'LiSi'
    ];   
    $atAll = false ; //使用@all通知@所有人
    $robot->text('今天天气真好')->mentionedAtUserId($user, $atAll)->send();

    $mobile = [
    '13800138000',
    '13900139000'
    ]; 
    $atAll = false;
    $robot->text('今天天气真好')->mentionedAtMobile($mobile, $atAll)->send();  

    $user = [
        'ZhangSan',
        'LiSi'
    ]; 
    //或
    $user = 'ZhangSan';
    $robot->text('今天天气真好')->contentAt($user)->send();
    //或自行拼接
    $robot->text('今天天气真好<@ZhangSan>')->send();

    $markdown = $robot->setMessage('markdown')->getMessage();

    $robot->markdown('我是内容')->send();

    $title = $markdown->setTitleContent('这是标题', 2)
    //参数2表示标题级别,支持1-6,默认1
    $bold = $markdown->setBoldContent('此处加粗');
    $wrap = $markdown->addWrap(); //换行符 \n
    $url = $markdown->setUrlContent('百度', 'https://baidu.com'); //超链接
    $code = $markdown->setOneLineCodeContent('我是一行代码'); //仅支持单行代码,不支持跨行代码段
    $quote = $markdown->setQuoteContent('这是引用文字'); //转引用
    $color = $mardown->setTextColor('我是绿色的字', 1); //这是字体颜色,仅支持仅支持绿(1),灰(2),橙红(3)

    //内容可随意拼接,不同格式间需要用换行符隔离,防止格式粘连
    $result = $robot->markdown($title.$wrap.'我只是一行内容'.$wrap.$bold.$wrap.$url)->send();

 $user = [
        'ZhangSan',
        'LiSi'
    ]; 
 //或
    $user = 'ZhangSan';
    $robot->markdown($title.$warp.$ulr)->contentAt($user)->send();

     
    $base64_data = base64_encode(file_get_contents($file)); //base64头可带可不带;

    $md5 = md5_file($file); //md5值必须是转base64之前的图片计算得来

    $result = $robot->image($base64_data, $md5)->send();


    $title = "中秋节礼品领取";
    $description = '今年中秋节公司有豪礼相送';
    $url = 'www.qq.com';
    $picUrl = 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png';
     $news = $robot->addNew($title,  $url, $description = '', $picUrl = '');
      


    $news->send();
    //支持多条图文
    $robot->addNew($title1, $url1)->addNew($title2, $url2)->send();
    //最大支持添加8条图文, 大于一条的图文展示效果类似公众号消息
    

    $media_id = '1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0';
    $robot->file($media_id)->send();

    php artisan vendor:publish --provider="Xin6841414\WorkWechatRobot\ServiceProvider"
    //或者
    php aritsan vendor:publish 选择相应数字序号