群机器人

使用说明

使用前必须先在群组里面添加机器人,然后将 Webhook 地址 中的 key 取出来,作为示例中 $groupKey 的值。

Webhook 地址示例:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=`ab4f609a-3feb-427c-ae9d-b319ca712d36`

微信文档:https://work.weixin.qq.com/api/doc#90000/90136/91770

发送文本类型消息

快速发送文本消息

  1. // 获取 Messenger 实例
  2. $messenger = $app->group_robot_messenger;
  3. // 群组 key
  4. $groupKey = 'ab4f609a-3feb-427c-ae9d-b319ca712d36';
  5. $messenger->message('大家好,我是本群的"喝水提醒小助手"')->toGroup($groupKey)->send();
  6. // 或者写成
  7. $messenger->toGroup($groupKey)->send('大家好,我是本群的"喝水提醒小助手"');

使用 Text 发送文本消息

  1. use EasyWeChat\Work\GroupRobot\Messages\Text;
  2. // 准备消息
  3. $text = new Text('hello');
  4. // 发送
  5. $messenger->message($text)->toGroup($groupKey)->send();

@某人:

  1. use EasyWeChat\Work\GroupRobot\Messages\Text;
  2. // 通过构造函数传参
  3. $text = new Text('hello', 'her-cat', '18700000000');
  4. //$text = new Text('hello', ['her-cat', 'overtrue'], ['18700000000', '18700000001']);
  5. // 通过 userId
  6. $text->mention('her-cat');
  7. //$text->mention(['her-cat', 'overtrue']);
  8. // 通过手机号
  9. $text->mentionByMobile('18700000000');
  10. //$text->mentionByMobile(['18700000000', '18700000001']);
  11. // @所有人
  12. $text->mention('@all');
  13. //$text->mentionByMobile('@all');
  14. $messenger->message($text)->toGroup($groupKey)->send();

发送 Markdown 类型消息

  1. use EasyWeChat\Work\GroupRobot\Messages\Markdown;
  2. $content = '
  3. # 标题一
  4. ## 标题二
  5. <font color="info">绿色</font>
  6. <font color="comment">灰色</font>
  7. <font color="warning">橙红色</font>
  8. > 引用文字
  9. ';
  10. $markdown = new Markdown($content);
  11. $messenger->message($markdown)->toGroup($groupKey)->send();

发送图片类型消息

  1. use EasyWeChat\Work\GroupRobot\Messages\Image;
  2. $img = file_get_contents('http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png');
  3. $image = new Image(base64_encode($img), md5($img));
  4. $result = $messenger->message($image)->toGroup($groupKey)->send();

发送图文类型消息

  1. use EasyWeChat\Work\GroupRobot\Messages\News;
  2. use EasyWeChat\Work\GroupRobot\Messages\NewsItem;
  3. $items = [
  4. new NewsItem([
  5. 'title' => '中秋节礼品领取',
  6. 'description' => '今年中秋节公司有豪礼相送',
  7. 'url' => 'https://www.easywechat.com',
  8. 'image' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png',
  9. ]),
  10. //...
  11. ];
  12. $news = new News($items);
  13. $messenger->message($news)->toGroup($groupKey)->send();

其他方式

使用 group_robot 发送消息。

  1. $app->group_robot->message('大家好,我是本群的"喝水提醒小助手"')->toGroup($groupKey)->send();