TelegramBot.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Notification;
  3. use App\Util;
  4. use GuzzleHttp\Client;
  5. use Illuminate\Support\Facades\Request;
  6. use Longman\TelegramBot\Entities\Update;
  7. use Longman\TelegramBot\Exception\TelegramException;
  8. use Longman\TelegramBot\Telegram;
  9. class TelegramBot
  10. {
  11. const api_key='8489593650:AAEioF92HCRDd-mnDpTo-6imc_bdi1My1II';
  12. const username='@uswinBot';
  13. public $telegram;
  14. public function __construct(){
  15. try {
  16. // $telegram = new Telegram(self::api_key, self::username);
  17. //
  18. // $mysql=['host' => env('DB_MYSQL_HOST', '127.0.0.1'),
  19. // 'port' => env('DB_MYSQL_PORT', '3306'),
  20. // 'database' => 'telegram',
  21. // 'user' => env('DB_MYSQL_USERNAME'),
  22. // 'password' => env('DB_MYSQL_PASSWORD'), ];
  23. // $telegram->enableMySql($mysql);
  24. //
  25. // $this->telegram=$telegram;
  26. //// $result = $this->telegram->setWebhook("https://".$_SERVER["HTTP_HOST"]."/api/telehook");
  27. // $allowed_updates = [
  28. // Update::TYPE_MESSAGE,
  29. // Update::TYPE_CHANNEL_POST,
  30. // // etc.
  31. // ];
  32. // $this->telegram->handleGetUpdates(['allowed_updates' => $allowed_updates]);
  33. // if ($result->isOk()) {
  34. // echo $result->getDescription();
  35. // Util::WriteLog('telegram', $result->getDescription());
  36. // }
  37. }catch (TelegramException $e){
  38. // Util::WriteLog('telegram','error:'.$e->getMessage());
  39. }
  40. }
  41. public function handle(){
  42. try {
  43. return $this->telegram->handle();
  44. }catch (TelegramException $e){
  45. Util::WriteLog('telegram',$e);
  46. }
  47. }
  48. public function sendMsg($str){
  49. if(env('TELE_SENDOUT')){
  50. $url=env('TELE_SENDOUT');
  51. $client = new Client();
  52. $client->post( $url , [
  53. 'form_params' => ['str'=>$str], // 传递 POST 数据
  54. ]);
  55. }
  56. $str=substr($str,0,4000);
  57. try {
  58. \Longman\TelegramBot\Request::sendToActiveChats(
  59. 'sendMessage', // Callback function to execute (see Request.php methods)
  60. ['text' => $str], // Param to evaluate the request
  61. [
  62. 'groups' => true,
  63. 'supergroups' => true,
  64. 'channels' => false,
  65. 'users' => false,
  66. ]
  67. );
  68. }catch (\Exception $e){
  69. Util::WriteLog('telegram',$e->getMessage());
  70. }
  71. }
  72. public function sendMsgWithEnv($str){
  73. if(is_array($str))$str=json_encode($str);
  74. $url=$_SERVER['REQUEST_SCHEME'].$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'?'.http_build_query(Request::all());
  75. $env = env('APP_ENV');//](".$url.")
  76. $str=$str."\n".'#### url:' . Request::getRequestUri() . "\n" .
  77. '#### params:' . json_encode(Request::all()) . "\n".(new \Exception())->getTraceAsString() ;
  78. $this->sendMsg($env .":". $str);
  79. }
  80. public function sendProgramNotify($compName="",$str="",$contentArr=[]){
  81. $env = env('APP_ENV');
  82. $this->sendMsg($env . " $compName 异常 ###\n [$str]\n".json_encode($contentArr,JSON_PRETTY_PRINT));
  83. }
  84. /**
  85. * @var TelegramBot
  86. */
  87. static private $_default=null;
  88. /**
  89. * @return TelegramBot
  90. */
  91. static public function getDefault(){
  92. if(!isset(self::$_default)||self::$_default==null)self::$_default=new TelegramBot();
  93. return self::$_default;
  94. }
  95. }