Telegram.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if($text == '/'){
  14. \Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);
  15. }
  16. }
  17. if (isset($message['location'])) {
  18. $user = User::where('telegram_id', $message['from']['id'])->first();
  19. if ($user){
  20. $lastLocation = $user->lastCoordinates;
  21. if ($lastLocation){
  22. $minLat = $message['location']['latitude'] - 0.0005;
  23. $maxLat = $message['location']['latitude'] + 0.0005;
  24. $minLng = $message['location']['longitude'] - 0.0008;
  25. $maxLng = $message['location']['longitude'] + 0.0008;
  26. $newLat = false;
  27. $newLng = false;
  28. if ($lastLocation->lat < $minLat || $lastLocation->lat > $maxLat){
  29. $newLat = $message['location']['latitude'];
  30. }
  31. if ($lastLocation->lng < $minLng || $lastLocation->lng > $maxLng){
  32. $newLng = $message['location']['longitude'];
  33. }
  34. if($newLat || $newLng){
  35. $newLocation = new Location();
  36. $newLocation->lat = $newLat ?: $lastLocation->lat;
  37. $newLocation->lng = $newLng ?: $lastLocation->lng;
  38. $newLocation->user_id = $user->id;
  39. $newLocation->save();
  40. Log::info('я сохранил новую координату');
  41. }
  42. } else {
  43. $newLocation = new Location();
  44. $newLocation->lat = $message['location']['latitude'];
  45. $newLocation->lng = $message['location']['longitude'];
  46. $newLocation->user_id = $user->id;
  47. $newLocation->save();
  48. }
  49. }
  50. }
  51. }
  52. }