Telegram.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Location;
  4. use App\User;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Log;
  7. class Telegram extends Controller
  8. {
  9. public function locations(Request $request){
  10. $message = $request['message'];
  11. if (isset($message['text'])){
  12. $text = substr($message['text'], 0, 1);
  13. Log::info($text);
  14. if($text == '/'){
  15. Log::info($text);
  16. \Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);
  17. }
  18. }
  19. if (isset($message['location'])) {
  20. Log::info($message['from']['id']);
  21. $user = User::where('telegram_id', $message['from']['id'])->get();
  22. Log::info(['User--->' => $user]);
  23. if ($user){
  24. $lastLocation = $user->lastCoordinates;
  25. $minLat = $message['location']['latitude'] - 0.0005;
  26. $maxLat = $message['location']['latitude'] + 0.0005;
  27. $minLng = $message['location']['longitude'] - 0.0008;
  28. $maxLng = $message['location']['longitude'] + 0.0008;
  29. $newLat = false;
  30. $newLng = false;
  31. if (!$lastLocation->lat > $minLat && !$lastLocation->lat < $maxLat){
  32. $newLat = $message['location']['latitude'];
  33. }
  34. if (!$lastLocation->lng > $minLng && !$lastLocation->lng < $maxLng){
  35. $newLng = $message['location']['longitude'];
  36. }
  37. if($newLat || $newLng){
  38. $newLocation = new Location();
  39. $newLocation->lat = $newLat ?: $lastLocation->lat;
  40. $newLocation->lng = $newLng ?: $lastLocation->lng;
  41. $newLocation->user_id = $user->id;
  42. $newLocation->save();
  43. Log::info('я сохранил новую координату');
  44. }
  45. }
  46. Log::info('я ничего не сохранил');
  47. }
  48. }
  49. }