WebHook.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Tools;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\Http;
  5. class WebHook{
  6. /**
  7. * Create a new command instance.
  8. *
  9. * @return void
  10. */
  11. public function __construct()
  12. {
  13. }
  14. public function wechat($url, $title=null, $message=null)
  15. {
  16. $param["msgtype"]="markdown";
  17. if(empty($title) && empty($message) ){
  18. return 0;
  19. }
  20. if($title !== null){
  21. $param["markdown"]['title'] = $title;
  22. }
  23. if($message !== null){
  24. $param["markdown"]['content'] = $message;
  25. }
  26. return $this->send($url, $param);
  27. }
  28. public function dingtalk($url, $title=null, $message=null){
  29. $param = array();
  30. $param["msgtype"]="markdown";
  31. if(empty($title) && empty($message) ){
  32. return 0;
  33. }
  34. if($title !== null){
  35. $param["markdown"]['title'] = $title;
  36. }
  37. if($message !== null){
  38. $param["markdown"]['text'] = $message;
  39. }
  40. return $this->send($url, $param);
  41. }
  42. private function send($url, $param){
  43. Log::info('webhook send');
  44. Log::info($param);
  45. try{
  46. $response = Http::post($url, $param);
  47. if($response->successful()){
  48. return 0;
  49. }else{
  50. return 1;
  51. }
  52. }catch(\Exception $e){
  53. Log::error('webhook send fail');
  54. Log::error($e);
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. }