PHP code example of ymlluo / group-robot

1. Go to this page and download the library: Download ymlluo/group-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/ */

    

ymlluo / group-robot example snippets

 
$robot->raw(['msgtype'=>'text','text'=>['content'=>'hello world!']])->send();
 
//@全部
$robot->text('hello world')->atAll()->send();
 
//@用户
$robot->text('hello world')->atUsers(["wangqing"])->send();
 
//@手机号
$robot->text('hello world')->atMobiles(["13800001111"])->send();

 $robot->markdown("实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。")->send();

 $robot->markdown("### 用户反馈,请相关同事注意。")->atUsers(["wangqing"])->send();

$robot->image('http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png')->send();
 
$robot->image('/tmp/images/test_pic_msg1.png')->send();
 
$robot->file('http://www.gov.cn/zhengce/pdfFile/2021_PDF.pdf','政府信息公开目录.pdf')->send();
 
$robot->file('/tmp/pdfFile/2021_PDF.pdf','政府信息公开目录.pdf')->send();
 
//单条图文
$robot->news([
  'title' => '中秋节礼品领取',
  'description' => '今年中秋节公司有豪礼相送',
  'url' => 'www.qq.com',
  'picurl' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png'
  ])->send();
  
 
//多条图文  
$robot->news([
  [
    'title' => '胡彦斌,不再为爱情燃烧',
    'description' => '胡彦斌最被大众好奇的问题是:他现在会如何看待感情?',
    'url' => 'https://new.qq.com/omn/20211028/20211028A07EDT00.html',
    'picurl' => 'https://inews.gtimg.com/newsapp_bt/0/14116849898/1000'
  ], 
  [
    'title' => '中秋节礼品领取',
    'description' => '今年中秋节公司有豪礼相送',
    'url' => 'www.qq.com',
    'picurl' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png'
  ]
])->send();

$robot->card(
  "Apple Store 的前身",//一级标题,建议不超过26个字
  "乔布斯 20 年前想打造的苹果咖啡厅 \nApple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",//标题辅助信息,建议不超过30个字
  "https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png",//封面图片
  "https://www.qq.com/",//链接跳转的url
  [['title' => '钉钉', 'url' => 'https://www.dingtalk.com/'], ['title' => '百度', 'url' => 'https://www.baidu.com/']]
)->send();
 
//文本通知模版卡片
$robot->template_card([
            'card_type' => 'text_notice',
            'source' => [
                'icon_url' => 'https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0',
                'desc' => '企业微信',
            ],
            'main_title' => [
              [
                'title' => '欢迎使用企业微信',
                'desc' => '您的好友正在邀请您加入企业微信',
              ]
            ],
            'emphasis_content' => [
                'title' => '100',
                'desc' => '数据含义',
            ],
            'sub_title_text' => '下载企业微信还能抢红包!',
            'horizontal_content_list' => [
                0 => [
                    'keyname' => '邀请人',
                    'value' => '张三',
                ],
                1 => [
                    'keyname' => '企微官网',
                    'value' => '点击访问',
                    'type' => 1,
                    'url' => 'https://work.weixin.qq.com/?from=openApi',
                ]
            ],
            'jump_list' => [
                0 => [
                    'type' => 1,
                    'url' => 'https://work.weixin.qq.com/?from=openApi',
                    'title' => '企业微信官网',
                ]
            ],
            'card_action' => [
                'type' => 1,
                'url' => 'https://work.weixin.qq.com/?from=openApi',
                'appid' => 'APPID',
                'pagepath' => 'PAGEPATH',
            ],
        ])->send();
 
//图文展示模版卡片
$robot->template_card([
            'card_type' => 'news_notice',
            'source' => [
                'icon_url' => 'https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0',
                'desc' => '企业微信',
            ],
            'main_title' => [
                'title' => '欢迎使用企业微信',
                'desc' => '您的好友正在邀请您加入企业微信',
            ],
            'card_image' => [
                'url' => 'https://wework.qpic.cn/wwpic/354393_4zpkKXd7SrGMvfg_1629280616/0',
                'aspect_ratio' => 2.25,
            ],
            'vertical_content_list' =>[
              'title'=>'惊喜红包等你来拿',
              'desc'=>'下载企业微信还能抢红包!'
            ],
            'horizontal_content_list' => [
                0 => [
                    'keyname' => '邀请人',
                    'value' => '张三',
                ],
                1 => [
                    'keyname' => '企微官网',
                    'value' => '点击访问',
                    'type' => 1,
                    'url' => 'https://work.weixin.qq.com/?from=openApi',
                ]
            ],
            'jump_list' => [
                0 => [
                    'type' => 1,
                    'url' => 'https://work.weixin.qq.com/?from=openApi',
                    'title' => '企业微信官网',
                ]
            ],
            'card_action' => [
                'type' => 1,
                'url' => 'https://work.weixin.qq.com/?from=openApi',
                'appid' => 'APPID',
                'pagepath' => 'PAGEPATH',
            ],
        ])->send();



//初始化并设置webhook
$webhook = 'https://oapi.dingtalk.com/robot/send?access_token=XXXXXX';
$token = 'xxx';

/**
 * @param string $platform 发送平台,wechat:企业微信,dingtalk:钉钉,feishu:飞书
 * @param string $webhook webhook 地址
 * @param string $secret 秘钥,加密方式加签时必填,企业微信不支持这个参数
 * @param string $alias 别名,多渠道发送时方便区分结果
 */
$robot = new GroupRobot('dingtalk',$webhook,$token,'d1');
 
$robot->raw(['msgtype'=>'text','text'=>['content'=>'hello world!']])->send();
 
//@全部
$robot->text('hello world')->atAll()->send();
 
//@用户 
$robot->text('hello world')->atUsers(["user123"])->send();
 
//@手机号
$robot->text('hello world')->atMobiles(["13800001111"])->send();

$robot->markdown("#### 杭州天气 \n > 9度,西北风1级,空气良89,相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n", '杭州天气')->send();

$robot->markdown("at 全部用户 \n", 'at')->atAll()->send();

$robot->markdown("at 用户 \n", 'at')->atUsers(["user123"])->send();

$robot->markdown("at 手机 \n", 'at')->atMobiles(["13800001111"])->send();
 
//单条图文
$robot->news([
  'title' => '中秋节礼品领取',
  'description' => '今年中秋节公司有豪礼相送',
  'url' => 'https://developers.dingtalk.com/document/robots/robot-overview',
  'picurl' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png'
])->send();
 


//等同于
$robot->link([
  'title' => '中秋节礼品领取',
  'text' => '今年中秋节公司有豪礼相送',
  'messageUrl' => 'https://developers.dingtalk.com/document/robots/robot-overview',
  'picUrl' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png'
])->send();
 
$robot->news([
  [
  'title' => '中秋节礼品领取',
  'url' => 'https://developers.dingtalk.com/document/robots/robot-overview',
  'picurl' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png'
  ],
  [
  'title' => '杭州天气',
  'url' => 'https://developers.dingtalk.com/document/robots/robot-overview',
  'picurl' => 'https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png'
  ],
])->send();
 
$robot->feedCard([
  [
  'title' => '中秋节礼品领取',
  'description' => '今年中秋节公司有豪礼相送',
  'messageURL' => 'https://developers.dingtalk.com/document/robots/robot-overview',
  'picURL' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png'
  ],
  [
  'title' => '杭州天气',
  'messageURL' => 'https://developers.dingtalk.com/document/robots/robot-overview',//注意是messageURL 不是messageUrl
  'picURL' => 'https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png' //注意是picURL 不是picUrl
  ],
])->send();
 
//Card 单个按钮
  $robot->card(
   "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
   "### 乔布斯 20 年前想打造的苹果咖啡厅 \nApple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
   "https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png",
   "https://www.dingtalk.com/",
   ['title'=>'阅读全文','url'=>'https://www.dingtalk.com/']
 )->send();
 
//Card 多个按钮
  $robot->card(
    "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",//标题
    "### 乔布斯 20 年前想打造的苹果咖啡厅",//描述
    "https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png",//封面图
    "https://www.dingtalk.com/",//跳转地址
    [['title' => '内容不错', 'url' => 'https://www.dingtalk.com/'], ['title' => '不感兴趣', 'url' => 'https://www.dingtalk.com/']],//按钮
    ['btnOrientation' => "1"] //非必填,按钮排列
)->send();
 
//单个按钮
  $robot->actionCard([
   'title'=>'乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身',
   'text'=>"![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) \n### 乔布斯 20 年前想打造的苹果咖啡厅 \nApple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
   'btnOrientation'=>'0',
   'singleTitle'=>'阅读全文',
   'singleURL'=>'https://www.dingtalk.com/'
 ])->send();
 
//多个按钮
  $robot->actionCard([
    'title'=>'乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身',
    'text'=>"![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) \n### 乔布斯 20 年前想打造的苹果咖啡厅 \nApple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
     'btnOrientation'=>'0',
     'btns'=>[
       [
         'title'=>'内容不错',
         'actionURL'=>'https://www.dingtalk.com/'
       ],
       [
         'title'=>'不感兴趣',
         'actionURL'=>'https://www.dingtalk.com/'
       ]
     ]
 ])->send();




//初始化并设置webhook
$webhook = 'https://open.feishu.cn/open-apis/bot/v2/hook/XXXXXX';
$token = 'xxx';
/**
 * @param string $platform 发送平台,wechat:企业微信,dingtalk:钉钉,feishu:飞书
 * @param string $webhook webhook 地址
 * @param string $secret 秘钥,加密方式加签时必填
 * @param string $alias 别名,多渠道发送时方便区分结果
 */
$robot = new GroupRobot('feishu',$webhook,$token,'feishu-1');
 
$robot->raw(['msg_type'=>'text','content'=>['text'=>'hello world!']])->send();

$robot->markdown("#### 杭州天气 \n > 9度,西北风1级,空气良89,相对温度73%\n", '杭州天气')->atAll()->send();

 
//Card 单个按钮
  $robot->card(
   "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",//title
   "### 乔布斯 20 年前想打造的苹果咖啡厅",//描述
   "",//封面图片,不支持
   "https://www.dingtalk.com/",//跳转地址
   ['title'=>'阅读全文','url'=>'https://www.dingtalk.com/'],//选填,按钮
   ['template'=>'red'] //模板样式,
 )->send();
 
 
$robot->interactive([
            'header' => [
                'title' => [
                    'content' => '今日旅游推荐',
                    'tag' => 'plain_text'
                ]
            ],
            'elements' => [
                [
                    'tag' => 'div',
                    'text' => [
                        'content' => '**西湖**,位于浙江省杭州市西湖区龙井路1号',
                        'tag' => 'lark_md'
                    ]
                ],
                [
                    'tag' => 'markdown',
                    'content' => "*西湖美景二月天*"
                ],
                [
                    'tag' => 'action',
                    'actions' => [
                        [
                            'tag' => 'button',
                            'text' => [
                                'content' => '更多景点介绍 :玫瑰:',
                                'tag' => 'lark_md'
                            ],
                            'url' => 'https://feishu.cn',
                            'type' => 'default'
                        ]
                    ]
                ]
            ]
        ])->send();

 
$robot->rich([
  'title' => '项目更新',
    'content' => [
    [
      [
        'tag' => 'text',
        'text' => '项目有更新:'
      ], 
      [
        'tag' => 'a',
        'text' => '请查看',
        'href' => 'https://feishu.cn'
      ],
      [
        'tag' => 'at',
        'user_id' => 'all'
      ]
    ]
  ]
])->send();

 
  Ymlluo\\GroupRobot\\GroupRobotServiceProvider::class
 
$webhook = 'https://oapi.dingtalk.com/robot/send?access_token=xxxx';
$secret = 'xxx';
$results = app('grouprobot')->queue()->text("Hello ")->text('Laravel')->cc('dingtalk',$webhook, $secret, 'ding_1')->send();
dd($result);
 
$robot->queue()
  ->text("文本消息1")
  ->text("文本消息2")
  ->markdown("**Markdown**")
  ->file("http://h10032.www1.hp.com/ctg/Manual/c05440029.pdf", "HP 2600打印机说明书.pdf")
  ->send();

$robot = new GroupRobot();
$robot->text("开始@".date('Y-m-d H:i:s',time()))
      ->text("结束@".date('Y-m-d H:i:s',time()))
      ->queue()
      ->cc('dingtalk','https://oapi.dingtalk.com/robot/send?access_token==xxxx','xxx','ding1')
      ->cc('wechat','https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx','','wx_1')
      ->cc('feishu','https://open.feishu.cn/open-apis/bot/v2/hook/xxxx','xxx','feishu1')
      ->send();
 


namespace App;

use Ymlluo\GroupRobot\Contracts\Platform;
use Ymlluo\GroupRobot\Notify\BaseNotify;

class CustomerPlatform extends BaseNotify implements Platform
{
    // todo
}

 
$webhook = 'https://xxx.com/webhook/xxx';
$robot = new GroupRobot();
$custom = new CustomerPlatform(); 
$robot->extendPlatform($custom,$webhook,$secret='',$alias='c1')->text("hello !")->send();
bash
 
sudo apt-get remove composer;
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');";
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');";
composer self-update;