/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $access_token = generate_access_token();
     $email = 'a1t1-' . generate_email() . '@telegramlogin.com';
     $user = new User();
     $user->name = 'david pichsenmeister';
     $user->username = '******';
     $user->email = $email;
     $user->telegram_id = 41478911;
     $user->access_token = $access_token;
     $user->save();
     $app = new App();
     $app->user_id = $user->id;
     $app->name = 'Telegram Login';
     $app->client_id = 314159265;
     $app->client_secret = generate_client_secret();
     $app->website = env('URL');
     $app->redirect_url = env('URL') . '/login';
     $app->save();
     $tg = new TelegramUser();
     $tg->telegram_id = 41478911;
     $tg->name = 'david pichsenmeister';
     $tg->username = '******';
     $tg->save();
     $auth = new Auth();
     $auth->app_id = $app->id;
     $auth->telegram_user_id = $tg->id;
     $auth->email = $email;
     $auth->access_token = $access_token;
     $auth->save();
 }
 private function start($message, $tg)
 {
     $key = trim(str_replace('/start', '', $message['text']));
     $token = Token::findByToken($key);
     $app = App::findOrFail($token->app_id);
     try {
         $auth = Auth::findByAppAndTelegramUser($app, $tg);
     } catch (ModelNotFoundException $e) {
         $auth = new Auth();
         $auth->app_id = $app->id;
         $auth->telegram_user_id = $tg->id;
         $auth->email = 'a' . $app->id . 't' . $tg->id . '-' . generate_email() . '@telegramlogin.com';
     }
     $auth->access_token = generate_access_token();
     $auth->active = true;
     $auth->save();
     $code = Code::create(array('app_id' => $app->id, 'auth_id' => $auth->id, 'code' => generate_code()));
     $url = $app->redirect_url . '?code=' . $code->code;
     if ($token->query_string) {
         $url .= '&' . $token->query_string;
     }
     $text = 'Please click this link to finish your signup at *' . $app->name . '*: ' . PHP_EOL;
     $text .= '[Click here](' . $url . ')';
     $params = array('text' => $text, 'chat_id' => $tg->telegram_id);
     $this->send($params);
     $token->delete();
     if ($app->client_id == 314159265) {
         $tg->status = str_replace('state=', '', $token->query_string);
     } else {
         $tg->status = 'access_granted';
     }
     $tg->save();
 }
 private function start($message)
 {
     $key = trim(str_replace('/start', '', $message['text']));
     $token = Token::findByToken($key);
     $app = App::findOrFail($token->app_id);
     $from = $message['from'];
     $telegramId = $from['id'];
     $telegramName = $from['first_name'];
     if (array_key_exists('last_name', $from)) {
         $telegramName .= ' ' . $from['last_name'];
     }
     if (array_key_exists('username', $from)) {
         $username = $from['username'];
     }
     try {
         $tg = TelegramUser::findByTelegramId($telegramId);
     } catch (ModelNotFoundException $e) {
         $tg = new TelegramUser();
         $tg->telegram_id = $telegramId;
     }
     $tg->name = $telegramName;
     $tg->save();
     if ($tg->status != '/start') {
         $tg->status = '/start';
         $tg->save();
         if (isset($username)) {
             $tg->username = $username;
         }
         try {
             $auth = Auth::findByAppAndTelegramUser($app, $tg);
         } catch (ModelNotFoundException $e) {
             $auth = new Auth();
             $auth->app_id = $app->id;
             $auth->telegram_user_id = $tg->id;
             $auth->email = generate_email() . '-' . $app->id . '-' . $tg->id . '@telegramlogin.com';
         }
         $auth->access_token = generate_access_token();
         $auth->active = true;
         $auth->save();
         $code = Code::create(array('app_id' => $app->id, 'auth_id' => $auth->id, 'code' => generate_code()));
         $url = $app->redirect_url . '?code=' . $code->code;
         if ($token->query_string) {
             $url .= '&' . $token->query_string;
         }
         $text = 'Please click this link to finish your signup at *' . $app->name . '*: ' . PHP_EOL;
         $text .= '[Click here](' . $url . ')';
         $params = array('text' => $text, 'chat_id' => $telegramId);
         $success = false;
         $trys = 0;
         while (!$success && $trys < 5) {
             $success = $this->send($params)['ok'];
             sleep(1);
             $trys++;
         }
         $token->delete();
         if ($app->client_id == 314159265) {
             $tg->status = str_replace('state=', '', $token->query_string);
         } else {
             $tg->status = 'access_granted';
         }
         $tg->save();
     }
 }