12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?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);
- Log::info($text);
- if($text == '/'){
- Log::info($text);
- \Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);
- }
- }
- if (isset($message['location'])) {
- Log::info($message['from']['id']);
- $user = User::where('telegram_id', $message['from']['id'])->get();
- Log::info(['User--->' => $user]);
- if ($user){
- $lastLocation = $user->lastCoordinates;
- $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('я сохранил новую координату');
- }
- }
- Log::info('я ничего не сохранил');
- }
- }
- }
|