mirror of
				https://github.com/v2board/v2board.git
				synced 2025-10-31 09:21:46 +08:00 
			
		
		
		
	update telegram
This commit is contained in:
		
							
								
								
									
										41
									
								
								app/Console/Commands/Test.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								app/Console/Commands/Test.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Console\Commands; | ||||
|  | ||||
| use Illuminate\Console\Command; | ||||
|  | ||||
| class Test extends Command | ||||
| { | ||||
|     /** | ||||
|      * The name and signature of the console command. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $signature = 'test'; | ||||
|  | ||||
|     /** | ||||
|      * The console command description. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $description = ''; | ||||
|  | ||||
|     /** | ||||
|      * Create a new command instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Execute the console command. | ||||
|      * | ||||
|      * @return mixed | ||||
|      */ | ||||
|     public function handle() | ||||
|     { | ||||
|     } | ||||
| } | ||||
| @@ -3,6 +3,7 @@ | ||||
| namespace App\Http\Controllers\Admin; | ||||
|  | ||||
| use App\Http\Requests\Admin\ConfigSave; | ||||
| use App\Services\TelegramService; | ||||
| use Illuminate\Http\Request; | ||||
| use App\Utils\Dict; | ||||
| use App\Http\Controllers\Controller; | ||||
| @@ -20,6 +21,20 @@ class ConfigController extends Controller | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function setTelegramWebhook() | ||||
|     { | ||||
|         $telegramService = new TelegramService(); | ||||
|         if (!$telegramService->getMe()) { | ||||
|             abort(500, '机器人Token有误'); | ||||
|         } | ||||
|         if (!$telegramService->setWebhook(config('v2board.app_url') . '/api/v1/guest/telegram/webhook?access_token=' . md5(config('v2board.telegram_bot_token')))) { | ||||
|             abort(500, 'Webhook设置失败'); | ||||
|         } | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function fetch() | ||||
|     { | ||||
|         // TODO: default should be in Dict | ||||
|   | ||||
							
								
								
									
										21
									
								
								app/Http/Controllers/Guest/TelegramController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								app/Http/Controllers/Guest/TelegramController.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Http\Controllers\Guest; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use App\Http\Controllers\Controller; | ||||
|  | ||||
| class TelegramController extends Controller | ||||
| { | ||||
|     public function __construct(Request $request) | ||||
|     { | ||||
|         if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) { | ||||
|             abort(500, 'authentication failed'); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function webhook(Request $request) | ||||
|     { | ||||
|         info($request->input()); | ||||
|     } | ||||
| } | ||||
| @@ -15,6 +15,7 @@ class AdminRoute | ||||
|             $router->get ('/config/fetch', 'Admin\\ConfigController@fetch'); | ||||
|             $router->post('/config/save', 'Admin\\ConfigController@save'); | ||||
|             $router->get ('/config/getEmailTemplate', 'Admin\\ConfigController@getEmailTemplate'); | ||||
|             $router->get ('/config/setTelegramWebhook', 'Admin\\ConfigController@setTelegramWebhook'); | ||||
|             // Plan | ||||
|             $router->get ('/plan/fetch', 'Admin\\PlanController@fetch'); | ||||
|             $router->post('/plan/save', 'Admin\\PlanController@save'); | ||||
|   | ||||
| @@ -17,6 +17,8 @@ class GuestRoute | ||||
|             $router->post('/order/stripeNotify', 'Guest\\OrderController@stripeNotify'); | ||||
|             $router->post('/order/bitpayXNotify', 'Guest\\OrderController@bitpayXNotify'); | ||||
|             $router->post('/order/payTaroNotify', 'Guest\\OrderController@payTaroNotify'); | ||||
|             // Telegram | ||||
|             $router->post('/telegram/webhook', 'Guest\\TelegramController@webhook'); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										50
									
								
								app/Services/TelegramService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								app/Services/TelegramService.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | ||||
| <?php | ||||
| namespace App\Services; | ||||
|  | ||||
| use \Curl\Curl; | ||||
|  | ||||
| class TelegramService { | ||||
|     protected $api; | ||||
|  | ||||
|     public function __construct() | ||||
|     { | ||||
|         $this->api = 'https://api.telegram.org/bot' . config('v2board.telegram_bot_token') . '/'; | ||||
|     } | ||||
|  | ||||
|     public function sendMessage(int $chatId, string $text, string $parseMode = '') | ||||
|     { | ||||
|         $this->request('sendMessage', [ | ||||
|             'chat_id' => $chatId, | ||||
|             'text' => $text, | ||||
|             'parse_mode' => $parseMode | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getMe() | ||||
|     { | ||||
|         $response = $this->request('getMe'); | ||||
|         if (!$response->ok) { | ||||
|             return false; | ||||
|         } | ||||
|         return $response; | ||||
|     } | ||||
|  | ||||
|     public function setWebhook(string $url) | ||||
|     { | ||||
|         $response = $this->request('setWebhook', [ | ||||
|             'url' => $url | ||||
|         ]); | ||||
|         if (!$response->ok) { | ||||
|             return false; | ||||
|         } | ||||
|         return $response; | ||||
|     } | ||||
|  | ||||
|     private function request(string $method, array $params = []) | ||||
|     { | ||||
|         $curl = new Curl(); | ||||
|         $curl->get($this->api . $method, http_build_query($params)); | ||||
|         $curl->close(); | ||||
|         return $curl->response; | ||||
|     } | ||||
| } | ||||
| @@ -20,10 +20,16 @@ class Telegram { | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getMe() | ||||
|     { | ||||
|         dd($this->request('getMe')); | ||||
|     } | ||||
|  | ||||
|     private function request(string $method, array $params) | ||||
|     { | ||||
|         $curl = new Curl(); | ||||
|         $curl->post($this->api . $method, http_build_query($params)); | ||||
|         $curl->get($this->api . $method, http_build_query($params)); | ||||
|         $curl->close(); | ||||
|         return $curl->response; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user