哮天犬通知组件

适配Hyperf框架的通知组件,支持短信、电话、邮件、钉钉工作通知、钉钉机器人、知音楼工作通知、知音楼机器人等通知渠道。
该SDK可以用于项目中直接往以上七种通知渠道发送通知,不是告警通知SDK,请注意区分,告警通知SDK请移步:PHP告警SDK。 该SDK只适配过 Hyperf1.1Hyperf2.0 框架,未适配其他框架,其他框架请测试是否可用之后再用于生产环境

代码仓库

https://github.com/tal-tech/alarm-dog-noticer
最新文档请参考代码仓库的 README.md 文件说明

安装

  1. # composer安装依赖
  2. composer require alarm-dog/noticer

配置

使用Hyperf框架工具生成默认配置:

  1. php bin/hyperf.php vendor:publish alarm-dog/noticer

默认配置文件将会被拷贝到框架根目录 config/autoload/noticer.php 注意修改配置文件中的 env 环境变量配置

使用

邮件通知

  1. use Dog\Noticer\Channel\Email;
  2. use Dog\Noticer\Exception\NoticeException;
  3. /**
  4. * @var Email
  5. */
  6. $mail = make(Email::class);
  7. // $mail = $this->container->make(Email::class); // 效果一样
  8. try {
  9. // 发送纯文本
  10. $mail->to('yourname@foo.bar', '你的名字')
  11. ->subject('test')
  12. ->text('<h1>title1</h1>')
  13. ->send();
  14. } catch (NoticeException $e) {
  15. // do something for exception
  16. }
  17. // 发送html
  18. $mail->init()
  19. // ->to('yourname@foo.bar', '你的名字')
  20. ->to('yourname@foo.bar') // 第二个参数可以省略
  21. ->subject('test')
  22. ->html('<h1>title1</h1>')
  23. ->send();
  24. // 一行一行文本发送
  25. $mail->init()
  26. // ->to('yourname@foo.bar', '你的名字')
  27. ->to(['yourname@foo.bar' => '你的名字']) // 可以通过数组格式给多个人发
  28. ->subject('test')
  29. ->line('<h1>title1</h1>')
  30. ->line('<h1>title2</h1>')
  31. ->line('<h1>title3</h1>')
  32. ->send();
  33. // 发送视图
  34. $mail->init()
  35. ->to('yourname@foo.bar', '你的名字')
  36. ->subject('test')
  37. ->view('mail', ['name' => '参数值'])
  38. ->send();
  39. // 视图配置
  40. /*
  41. <!DOCTYPE html>
  42. <html lang="en">
  43. <head>
  44. <meta charset="UTF-8">
  45. <title>Hyperf</title>
  46. </head>
  47. <body>
  48. Hello, {{ $name }}. You are using blade template now.
  49. </body>
  50. </html>
  51. */
  52. // 指定blade模板引擎发送视图
  53. $mail->init()
  54. ->to('yourname@foo.bar', '你的名字')
  55. ->subject('test')
  56. ->blade('mail', ['name' => '参数值'])
  57. ->send();
  58. // 给多个人发送
  59. // 指定blade模板引擎发送视图
  60. $mail->init()
  61. ->to([
  62. 'yourname@foo.bar' => '你的名字1',
  63. 'yourname2@foo.bar' => '你的名字2',
  64. ])
  65. ->subject('test')
  66. ->view('mail', ['name' => '参数值'])
  67. ->send();
  68. // 每次发送之后如果仍使用同一个实例,务必重置,否则可能出现问题
  69. $mail->init()
  70. ->to('yourname@foo.bar', '你的名字')
  71. ->subject('test')
  72. ->html('<h1>重置完成</h1>')
  73. ->send();
  74. // 更多功能请扒源码

短信通知

  1. use Dog\Noticer\Channel\Sms;
  2. /**
  3. * @var Sms
  4. */
  5. $sms = make(Sms::class);
  6. // $sms = $this->container->make(Sms::class); // 效果一样
  7. try {
  8. $tplId = '111111111';
  9. $param = ['name' => '张三'];
  10. $receivers = ['131xxxxxxxx', '132xxxxxxxx'];
  11. $sms->send($tplId, $param, $receivers);
  12. } catch (NoticeException $e) {
  13. // do something for exception
  14. }

电话通知

  1. use Dog\Noticer\Channel\Phone;
  2. /**
  3. * @var Phone
  4. */
  5. $phone = make(Phone::class);
  6. // $phone = $this->container->make(Phone::class); // 效果一样
  7. try {
  8. $content = '您的验证码为:123456';
  9. $receiver = '131xxxxxxxx'; // 只能单个发送
  10. $phone->send($content, $receiver);
  11. } catch (NoticeException $e) {
  12. // do something for exception
  13. }

钉钉工作通知

  1. use Dog\Noticer\Channel\DingWorker;
  2. use Dog\Noticer\Channel\DingWorker\MsgType\Text;
  3. use Dog\Noticer\Channel\DingWorker\MsgType\Markdown;
  4. use Dog\Noticer\Channel\DingWorker\MsgType\ActionCard;
  5. /**
  6. * @var DingWorker
  7. */
  8. $dingworker = make(DingWorker::class);
  9. // $dingworker = $this->container->make(DingWorker::class); // 效果一样
  10. // 发送文本
  11. try {
  12. // 注意:工作通知相同内容只能一天给一个人发一次,每个人最多一天收到50条消息,为了让通知内容不会因为相同被拦截,可以加上当前发送的时间
  13. // 其他所有格式都一样,相同会被拦截,建议都加上时间,包括markdown、actionCard
  14. // $text = new Text('通知内容' . "\n" . date('Y-m-d H:i:s'));
  15. $text = new Text('通知内容');
  16. $options = [
  17. 'emails' => 'yourname@foo.bar', // 多一个以|分隔,例如 'emails' => 'yourname@foo.bar|yourname2@foo.bar',
  18. // 'workcodes' => '01', // 多一个以|分隔,必须带前面0,与邮箱填一个即可
  19. ];
  20. $dingworker->send($text, $options);
  21. } catch (NoticeException $e) {
  22. // do something for exception
  23. }
  24. // 发送markdown,仅支持markdown子集,具体请参考:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/9e91d73c
  25. $markdown = new Markdown('标题', "
  26. # title1
  27. # title2
  28. ---
  29. **加粗** 不加粗
  30. ");
  31. $options = [
  32. 'emails' => 'yourname@foo.bar', // 多一个以|分隔,例如 'emails' => 'yourname@foo.bar|yourname2@foo.bar',
  33. // 'workcodes' => '01', // 多一个以|分隔,必须带前面0,与邮箱填一个即可
  34. ];
  35. $dingworker->send($markdown, $options);
  36. // 发送ActionCard
  37. $actionCard = new ActionCard('标题', "
  38. # markdown文本,主体内容
  39. # ---
  40. ");
  41. // 单个button
  42. $actionCard->single('button标题', 'button跳转url');
  43. // 或者多个button
  44. $actionCard->btn('button标题1', 'button跳转url1')
  45. >btn('button标题2', 'button跳转url2')
  46. >btn('button标题3', 'button跳转url3');
  47. // 或者通过数组设置多个button
  48. $actionCard->btns([
  49. ['title' => 'button标题1', 'action_url' => 'button跳转url1'],
  50. ['title' => 'button标题2', 'action_url' => 'button跳转url2'],
  51. ]);
  52. // 默认button为横向排列,如果需要竖向,可设置:
  53. $actionCard->setVertical(true);
  54. $dingworker->send($actionCard, $options);
  55. // 更多消息类型请扒源码

钉钉机器人(钉钉群)通知

  1. use Dog\Noticer\Channel\DingGroup;
  2. use Dog\Noticer\Channel\DingGroup\MsgType\Text;
  3. use Dog\Noticer\Channel\DingGroup\MsgType\Markdown;
  4. use Dog\Noticer\Channel\DingGroup\MsgType\ActionCard;
  5. /**
  6. * @var DingGroup
  7. */
  8. $dinggroup = make(DingGroup::class);
  9. // $dinggroup = $this->container->make(DingGroup::class); // 效果一样
  10. $robots = [
  11. ['webhook' => '钉钉机器人webhook中的access_token的值', 'secret' => '此参数非必填'],
  12. ['webhook' => '另外一个机器人'],
  13. ];
  14. $options = [
  15. ]
  16. // 发送文本
  17. try {
  18. $text = new Text('通知内容');
  19. $dinggroup->send($text, $robots, $options);
  20. } catch (NoticeException $e) {
  21. // do something for exception
  22. }
  23. // 发送markdown,仅支持markdown子集,具体请参考:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/9e91d73c
  24. $markdown = new Markdown('标题', "
  25. # title1
  26. # title2
  27. ---
  28. **加粗** 不加粗
  29. ");
  30. $dinggroup->send($markdown, $robots, $options);
  31. // 发送ActionCard
  32. $actionCard = new ActionCard('标题', "
  33. # markdown文本,主体内容
  34. # ---
  35. ");
  36. // 单个button
  37. $actionCard->single('button标题', 'button跳转url');
  38. // 或者多个button
  39. $actionCard->btn('button标题1', 'button跳转url1')
  40. >btn('button标题2', 'button跳转url2')
  41. >btn('button标题3', 'button跳转url3');
  42. // 或者通过数组设置多个button
  43. $actionCard->btns([
  44. ['title' => 'button标题1', 'action_url' => 'button跳转url1'],
  45. ['title' => 'button标题2', 'action_url' => 'button跳转url2'],
  46. ]);
  47. // 默认button为横向排列,如果需要竖向,可设置:
  48. $actionCard->setVertical(true);
  49. $dinggroup->send($actionCard, $robots, $options);
  50. // 更多消息类型请扒源码

知音楼机器人(知音楼群)通知

  1. use Dog\Noticer\Channel\YachGroup;
  2. use Dog\Noticer\Channel\YachGroup\MsgType\Text;
  3. use Dog\Noticer\Channel\YachGroup\MsgType\Markdown;
  4. use Dog\Noticer\Channel\YachGroup\MsgType\ActionCard;
  5. /**
  6. * @var YachGroup
  7. */
  8. $yachgroup = make(YachGroup::class);
  9. // $yachgroup = $this->container->make(YachGroup::class); // 效果一样
  10. $robots = [
  11. ['webhook' => 'yach机器人webhook中的access_token的值', 'secret' => '此参数必填'],
  12. ];
  13. // 发送文本
  14. try {
  15. $text = new Text('通知内容');
  16. $yachgroup->send($text, $robots);
  17. } catch (NoticeException $e) {
  18. // do something for exception
  19. }
  20. // 发送markdown,仅支持markdown子集,具体请参考(Yach和钉钉差不多):https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/9e91d73c
  21. $markdown = new Markdown('标题', "
  22. # title1
  23. # title2
  24. ---
  25. **加粗** 不加粗
  26. ");
  27. $yachgroup->send($markdown, $robots);
  28. // 发送markdown,并带图片地址
  29. $markdown = new Markdown('标题', "
  30. # title1
  31. # title2
  32. ---
  33. **加粗** 不加粗
  34. ", 'http://bpit-public.oss-cn-beijing.aliyuncs.com/note_1_1572593268.jpg');
  35. $yachgroup->send($markdown, $robots);
  36. // 发送ActionCard
  37. $actionCard = new ActionCard('标题', "
  38. # markdown文本,主体内容
  39. # ---
  40. ", $image = '', $contentTitle = ''); // 此处可以填下image和contentTitle的参数
  41. // 单个button
  42. $actionCard->single('button标题', 'button跳转url');
  43. // 或者多个button
  44. $actionCard->btn('button标题1', 'button跳转url1')
  45. >btn('button标题2', 'button跳转url2')
  46. >btn('button标题3', 'button跳转url3');
  47. // 或者通过数组设置多个button
  48. $actionCard->btns([
  49. ['title' => 'button标题1', 'action_url' => 'button跳转url1'],
  50. ['title' => 'button标题2', 'action_url' => 'button跳转url2'],
  51. ]);
  52. // 默认button为横向排列,如果需要竖向,可设置:
  53. $actionCard->setVertical(true);
  54. $yachgroup->send($actionCard, $robots);
  55. // 更多消息类型请扒源码

知音楼工作通知

  1. use Dog\Noticer\Channel\YachWorker;
  2. use Dog\Noticer\Channel\YachWorker\MsgType\Text;
  3. use Dog\Noticer\Channel\YachWorker\MsgType\Markdown;
  4. use Dog\Noticer\Channel\YachWorker\MsgType\ActionCard;
  5. /**
  6. * @var YachWorker
  7. */
  8. $yachworker = make(YachWorker::class);
  9. // $yachworker = $this->container->make(YachWorker::class); // 效果一样
  10. $options = [
  11. // 通过邮箱发送(推荐)
  12. 'user_type' => 'email',
  13. // 多个以|分隔
  14. 'userid_list' => 'ethananony@aliyun.com|xxxxxxxxxx@foo.bar',
  15. // // 通过工号发送
  16. // 'user_type' => 'workcode',
  17. // // 多个以|分隔
  18. // 'userid_list' => '01|999999',
  19. ];
  20. // 发送文本
  21. try {
  22. $text = new Text('通知内容');
  23. $yachworker->send($text, $options);
  24. } catch (NoticeException $e) {
  25. // do something for exception
  26. }
  27. // 发送markdown,仅支持markdown子集,具体请参考(Yach和钉钉差不多):https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/9e91d73c
  28. $markdown = new Markdown('标题', "
  29. # title1
  30. # title2
  31. ---
  32. **加粗** 不加粗
  33. ");
  34. $yachworker->send($markdown, $options);
  35. // 发送markdown,并带图片地址
  36. $markdown = new Markdown('标题', "
  37. # title1
  38. # title2
  39. ---
  40. **加粗** 不加粗
  41. ", 'http://bpit-public.oss-cn-beijing.aliyuncs.com/note_1_1572593268.jpg');
  42. $yachworker->send($markdown, $options);
  43. // 发送ActionCard
  44. $actionCard = new ActionCard('标题', "
  45. # markdown文本,主体内容
  46. # ---
  47. ", $image = '', $contentTitle = ''); // 此处可以填下image和contentTitle的参数
  48. // 单个button
  49. $actionCard->single('button标题', 'button跳转url');
  50. // 或者多个button
  51. $actionCard->btn('button标题1', 'button跳转url1')
  52. >btn('button标题2', 'button跳转url2')
  53. >btn('button标题3', 'button跳转url3');
  54. // 或者通过数组设置多个button
  55. $actionCard->btns([
  56. ['title' => 'button标题1', 'action_url' => 'button跳转url1'],
  57. ['title' => 'button标题2', 'action_url' => 'button跳转url2'],
  58. ]);
  59. // 默认button为横向排列,如果需要竖向,可设置:
  60. $actionCard->setVertical(true);
  61. $yachworker->send($actionCard, $options);
  62. // 更多消息类型请扒源码