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. $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. }
  54. $str=substr($str,0,4000);
  55. try {
  56. \Longman\TelegramBot\Request::sendToActiveChats(
  57. 'sendMessage', // Callback function to execute (see Request.php methods)
  58. ['text' => $str], // Param to evaluate the request
  59. [
  60. 'groups' => true,
  61. 'supergroups' => true,
  62. 'channels' => false,
  63. 'users' => false,
  64. ]
  65. );
  66. }catch (\Exception $e){
  67. Util::WriteLog('telegram',$e->getMessage());
  68. }
  69. }
  70. public function sendMsgWithEnv($str){
  71. if(is_array($str))$str=json_encode($str);
  72. $url=$_SERVER['REQUEST_SCHEME'].$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'?'.http_build_query(Request::all());
  73. $env = env('APP_ENV');//](".$url.")
  74. $str=$str."\n".'#### url:' . Request::getRequestUri() . "\n" .
  75. '#### params:' . json_encode(Request::all()) . "\n".(new \Exception())->getTraceAsString() ;
  76. $this->sendMsg($env .":". $str);
  77. }
  78. public function sendProgramNotify($compName="",$str="",$contentArr=[]){
  79. $env = env('APP_ENV');
  80. $this->sendMsg($env . " $compName 异常 ###\n [$str]\n".json_encode($contentArr,JSON_PRETTY_PRINT));
  81. }
  82. /**
  83. * @var TelegramBot
  84. */
  85. static private $_default=null;
  86. /**
  87. * @return TelegramBot
  88. */
  89. static public function getDefault(){
  90. if(!isset(self::$_default)||self::$_default==null)self::$_default=new TelegramBot();
  91. return self::$_default;
  92. }
  93. }