Skip to content

Commit 5f8c22e

Browse files
committed
给多个机器人发同一条消息
优化 Robot 类,以便给多个机器人发同一条消息
1 parent 9cb92b5 commit 5f8c22e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,32 @@ public function toDingTalkRobot($notifiable)
246246
}
247247
```
248248

249+
## 使用消息通知(Notification)给多个机器人发同一条消息
250+
251+
```php
252+
use Calchen\LaravelDingtalkRobot\Robot;
253+
254+
$notification = new TestDingtalkNotification();
255+
256+
(new Robot('robot_1'))->notify($notification);
257+
(new Robot('robot_2'))->notify($notification);
258+
259+
// TestDingtalkNotification 文件
260+
use Calchen\LaravelDingtalkRobot\Message\TextMessage;
261+
262+
public function toDingTalkRobot($notifiable)
263+
{
264+
$message = new TextMessage('我就是我, @1825718XXXX 是不一样的烟火');
265+
266+
// 可@某人或某些人
267+
$message->at('1825718XXXX'); // $message->at(['1825718XXXX', '1825718XXXY']);
268+
269+
// 可通过 setConnection 设置向指定的机器人发送消息,如果不指定则为默认机器人
270+
$message->setConnection($notifiable->getName());
271+
272+
return $message;
273+
}
274+
```
249275

250276
## 不使用消息通知(Notification)
251277

src/Robot.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,28 @@
1414
class Robot
1515
{
1616
use Notifiable;
17+
18+
// 机器人名称,用于指定发送机器人
19+
private $name;
20+
21+
public function __construct($name = 'default')
22+
{
23+
$this->name = $name;
24+
}
25+
26+
/**
27+
* @return string
28+
*/
29+
public function getName(): string
30+
{
31+
return $this->name;
32+
}
33+
34+
/**
35+
* @param string $name
36+
*/
37+
public function setName(string $name): void
38+
{
39+
$this->name = $name;
40+
}
1741
}

0 commit comments

Comments
 (0)