| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace app\common\service\dtalk\robot;
- use app\common\enum\dtalk\RobotSence;
- use app\common\library\dtalk\Robot;
- use app\common\model\DtalkRobot;
- use app\common\service\Basics;
- /**
- * 基础发送钉钉消息机器人
- *
- * @Author Mark
- * @DateTime 2024-08-12
- */
- class BaseRobot extends Basics
- {
- /**
- * 构造函数
- *
- * @param integer $wxappId 小程序id
- * @param integer $sence 发送场景
- * @Author Mark
- * @DateTime 2024-08-12
- */
- public function __construct(int $wxappId)
- {
- $this->wxappId = $wxappId;
- }
- /**
- * 获取机器人列表
- *
- * @return DtalkRobot[]
- * @Author Mark
- * @DateTime 2024-08-09
- */
- private function getRobot(int $sence)
- {
- $where = [
- 'sence' => $sence,
- 'is_delete' => 0,
- 'wxapp_id' => $this->wxappId,
- ];
- $robot = DtalkRobot::where($where)->select();
- return $robot;
- }
- /**
- * 发送消息
- *
- * @param string $content 发送的内容
- * @param integer $sence 发送场景
- * @return bool
- * @Author Mark
- * @DateTime 2024-08-12
- */
- public function sendMessage(string $content, int $sence, array $phones = [])
- {
- $robotList = $this->getRobot($sence);
- if (count($robotList) < 1) {
- return $this->setError('未设置符合条件的机器人');
- }
- foreach ($robotList as $robot) {
- $service = new Robot;
- $service->setWebhookAndSecret($robot->webhook, $robot->secret);
- $atPhones = $robot->at_phone ? explode('#', $robot->at_phone) : $phones;
- $service->setMessage($content)->setAtPhones($atPhones)->sendMessage();
- }
- return true;
- }
- }
|