12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace App\Http\Controllers;
- use App\Location;
- use App\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class Telegram extends Controller
- {
- public function locations(Request $request){
- $message = $request['message'];
- if (isset($message['text'])){
- $text = substr($message['text'], 0, 1);
- if($text == '/'){
- \Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);
- }
- }
- if (isset($message['location'])) {
- $user = User::where('telegram_id', $message['from']['id'])->first();
- if ($user){
- $lastLocation = $user->lastCoordinates;
- if ($lastLocation){
- $minLat = $message['location']['latitude'] - 0.0005;
- $maxLat = $message['location']['latitude'] + 0.0005;
- $minLng = $message['location']['longitude'] - 0.0008;
- $maxLng = $message['location']['longitude'] + 0.0008;
- $newLat = false;
- $newLng = false;
- if ($lastLocation->lat < $minLat || $lastLocation->lat > $maxLat){
- $newLat = $message['location']['latitude'];
- }
- if ($lastLocation->lng < $minLng || $lastLocation->lng > $maxLng){
- $newLng = $message['location']['longitude'];
- }
- if($newLat || $newLng){
- $newLocation = new Location();
- $newLocation->lat = $newLat ?: $lastLocation->lat;
- $newLocation->lng = $newLng ?: $lastLocation->lng;
- $newLocation->user_id = $user->id;
- $newLocation->save();
- Log::info('я сохранил новую координату');
- }
- } else {
- $newLocation = new Location();
- $newLocation->lat = $message['location']['latitude'];
- $newLocation->lng = $message['location']['longitude'];
- $newLocation->user_id = $user->id;
- $newLocation->save();
- }
- }
- }
- }
-
- }
|