TelegramBot.php 3.5 KB

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