Browse Source

tbots commands

Carello Qwerty 5 years ago
parent
commit
f3de2c93b1

+ 55 - 0
app/Http/Controllers/Telegram.php

@@ -0,0 +1,55 @@
+<?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('я ничего не сохранил');
+        }
+    }
+}

+ 80 - 0
app/TCommands/MyEmail.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace App\TCommands;
+
+use App\ExternalUser;
+use App\User;
+use Illuminate\Support\Facades\Log;
+use Telegram\Bot\Actions;
+use Telegram\Bot\Commands\Command;
+
+/**
+ * Class SearchCommand.
+ */
+class MyEmail extends Command
+{
+    /**
+     * @var string Command Name
+     */
+    protected $name = 'my_email';
+
+    /**
+     * @var array Command Aliases
+     */
+    protected $aliases = ['listcommands'];
+
+    /**
+     * @var string Command Description
+     */
+    protected $description = 'Привязать почту';
+
+    /**
+     * {@inheritdoc}
+     */
+    public function handle($arguments)
+    {
+        $telegram_message = \Telegram::getWebhookUpdates()['message'];
+        $textOfMessage = $telegram_message['text'];
+        $updateId = \Telegram::getWebhookUpdates()['update_id']+1;
+        \Telegram::getWebhookUpdates(['offset'=>$updateId]);
+
+        $email = substr($textOfMessage, 9);
+
+        if ($email){
+            $trimedEmail = trim($email);
+
+            if (filter_var($trimedEmail, FILTER_VALIDATE_EMAIL)){
+                $userId = $telegram_message['from']['id'];
+                $user = User::where('email', $trimedEmail)->first();
+                Log::info('User->'. $user);
+
+                if ($user){
+
+                    if($user->telegram_id){
+                        $telegramId = $user->telegram_id == $telegram_message['from']['id'] ? true : false;
+                    } else {
+                        $telegramId = false;
+                    }
+
+                    if (!$telegramId){
+                        $user->telegram_id = $userId;
+                        $user->save();
+                    }
+
+                    $text = $telegramId ? 'Поштова скринька вже приєднана до цього номеру Telegram' : 'Вітаємо, ви приєднали скриньку';
+                } else {
+                    $text = 'користувача не зареєстровано на сайті proj.geolook.php.a-level.com.ua';
+                }
+            } else {
+                $text = 'email вказано невірно';
+            }
+
+        } else {
+            $text = 'Для приєднання поштової скриньки, вкажіть її назву через пробіл після команди /my_email.'.PHP_EOL.'Шаблон:'.PHP_EOL.'/my_email \'назва скриньки\'';
+        }
+
+        $this->replyWithMessage(compact('text'));
+
+        //Log::info(trim($email));
+    }
+}

+ 1 - 1
app/TCommands/TestCommand.php

@@ -31,7 +31,7 @@ class TestCommand extends Command
      */
     public function handle($arguments)
     {
-        Log::info($arguments);
+        Log::info(['tbot' => $arguments]);
         $text = 'Привет';
         $this->replyWithMessage(compact('text'));
 

+ 1 - 0
config/telegram.php

@@ -58,5 +58,6 @@ return [
     'commands' => [
         Telegram\Bot\Commands\HelpCommand::class,
         \App\TCommands\TestCommand::class,
+        \App\TCommands\MyEmail::class
     ],
 ];

+ 4 - 3
routes/web.php

@@ -23,9 +23,10 @@ Route::get('/user','UserController@action');
 Route::get('/user/{id}', 'UserController@show')->where('id', '[0-9]+')->name('user.position');
 Route::get('/user/{id}/{date}', 'UserController@userdata')->where(['id'=>'[0-9]+','date'=>'[0-9]{4}-[0-9]{2}-[0-9]{2}'])->name('user.date');
 Route::get('/user/{id}/last', 'UserController@showLast')->where('id', '[0-9]+')->name('last.position');
-Route::post('/tbot/'.Telegram::getAccessToken(), function(){
-    Telegram::commandsHandler(true);
-});
+//Route::post('/tbot/'.Telegram::getAccessToken(), function(){
+//    Telegram::commandsHandler(true);
+//});
+Route::post('/tbot/'.Telegram::getAccessToken(), 'Telegram@locations');
 
 //Route::post(Telegram::getAccessToken(), function () {
 //	Telegram::commandsHandler(true);