群機器人
使用說明
使用前必須先在群組里面添加機器人,然后將 Webhook 地址
中的 key
取出來,作為示例中 $groupKey
的值。
發送文本類型消息
快速發送文本消息
<?php
// 獲取 Messenger 實例
$messenger = $work->group_robot_messenger;
// 群組 key
$groupKey = 'ab4f609a-3feb-427c-ae9d-b319ca712d36';
$messenger->message('大家好,我是本群的"喝水提醒小助手"')->toGroup($groupKey)->send();
// 或者寫成
$messenger->toGroup($groupKey)->send('大家好,我是本群的"喝水提醒小助手"');
使用
Text
發送文本消息
<?php
// 準備消息
$text = new \EasySwoole\WeChat\Work\GroupRobot\Messages\Text('hello');
// 發送
$messenger->message($text)->toGroup($groupKey)->send();
@某人:
<?php
// 通過構造函數傳參
$text = new \EasySwoole\WeChat\Work\GroupRobot\Messages\Text('hello', 'her-cat', '18700000000');
//$text = new Text('hello', ['her-cat', 'easyswoole'], ['18700000000', '18700000001']);
// 通過 userId
$text->mention('her-cat');
//$text->mention(['her-cat', 'easyswoole']);
// 通過手機號
$text->mentionByMobile('18700000000');
//$text->mentionByMobile(['18700000000', '18700000001']);
// @所有人
$text->mention('@all');
//$text->mentionByMobile('@all');
$messenger->message($text)->toGroup($groupKey)->send();
發送 Markdown
類型消息
<?php
$content = '
# 標題一
## 標題二
<font color="info">綠色</font>
<font color="comment">灰色</font>
<font color="warning">橙紅色</font>
> 引用文字
';
$markdown = new \EasySwoole\WeChat\Work\GroupRobot\Messages\Markdown($content);
$messenger->message($markdown)->toGroup($groupKey)->send();
發送圖片類型消息
<?php
$img = file_get_contents('http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png');
$image = new \EasySwoole\WeChat\Work\GroupRobot\Messages\Image(base64_encode($img), md5($img));
$result = $messenger->message($image)->toGroup($groupKey)->send();
發送圖文類型消息
<?php
$items = [
new \EasySwoole\WeChat\Work\GroupRobot\Messages\NewsItem([
'title' => '中秋節禮品領取',
'description' => '今年中秋節公司有豪禮相送',
'url' => 'http://www.jrrswxmm.cn',
'image' => 'http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png',
]),
//...
];
$news = new \EasySwoole\WeChat\Work\GroupRobot\Messages\News($items);
$messenger->message($news)->toGroup($groupKey)->send();
其他方式
使用 group_robot
發送消息。
$work->groupRobot->message('大家好,我是本群的"喝水提醒小助手"')->toGroup($groupKey)->send();