SettingController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Http\Admin\Telegram;
  3. use AdminSection;
  4. use App\Setting;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. use Psr\Log\NullLogger;
  9. use Telegram\Bot\Api;
  10. class SettingController extends Controller
  11. {
  12. public function index(){
  13. return AdminSection::view(view('admin.telegram', Setting::getSettings()));
  14. }
  15. public function store(Request $request){
  16. Setting::where('key', '!=', NULL)->delete();
  17. foreach ($request->except('_token') as $key => $value){
  18. $setting = new Setting;
  19. $setting->key = $key;
  20. $setting->value = $request->$key;
  21. $setting->save();
  22. }
  23. return redirect()->route('admin.setting.index');
  24. }
  25. public function setWebhook(Request $request){
  26. $result = $this->sendTelegramData($request->url ? 'setWebhook' : 'deleteWebhook', [
  27. 'query' => ['url' => $request->url . '/' . \Telegram::getAccessToken()]
  28. ]);
  29. return redirect()->route('admin.setting.index')->with('status', $result);
  30. }
  31. public function getWebhookInfo(){
  32. $result = $this->sendTelegramData('getWebhookInfo');
  33. return redirect()->route('admin.setting.index')->with('status', $result);
  34. }
  35. public function sendTelegramData ($rout = '', $params = [], $method = 'POST'){
  36. $client = new Client( ['base_uri' => 'https://api.telegram.org/bot' . \Telegram::getAccessToken() . '/'] );
  37. $result = $client->request( $method, $rout, $params);
  38. return (string) $result->getBody();
  39. }
  40. }