Exemplo n.º 1
0
 private function getUser($code, $state)
 {
     $app = App::findByClientId(314159265);
     $params = array('code' => $code, 'client_id' => $app->client_id, 'client_secret' => $app->client_secret);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, env('URL') . '/code');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
     $json = curl_exec($ch);
     $result = json_decode($json, true);
     if (!array_key_exists('access_token', $result)) {
         app()->abort($result['error'], $result['description']);
     }
     if (array_key_exists('active', $result) && !$result['active']) {
         app()->abort($result['error'], $result['description']);
     }
     $tg = TelegramUser::findByTelegramId($result['telegram_user']['telegram_id']);
     if ($tg->status != $state) {
         app()->abort(403, 'Invalid state.');
     }
     $tg->status = 'access_granted';
     $tg->save();
     try {
         $user = User::findByTelegramId($result['telegram_user']['telegram_id']);
     } catch (ModelNotFoundException $e) {
         $user = new User();
         $user->email = $result['email'];
         $user->telegram_id = $result['telegram_user']['telegram_id'];
     }
     $user->access_token = $result['access_token'];
     $user->name = $result['telegram_user']['name'];
     $user->username = $result['telegram_user']['username'];
     $user->save();
     return $user;
 }
Exemplo n.º 2
0
 public function generateToken(Request $request, $clientId)
 {
     $app = App::findByClientId($clientId);
     $token = $this->createToken($app);
     $query = str_replace($request->url(), '', $request->fullUrl());
     if ($query && strlen($query)) {
         $token->query_string = substr($query, 1);
         $token->save();
     }
     return redirect('https://telegram.me/' . env('BOT_NAME') . '?start=' . $token->token);
 }
Exemplo n.º 3
0
 private function commandReply($message)
 {
     $telegramId = $message['from']['id'];
     $tg = TelegramUser::findByTelegramId($telegramId);
     $params = array('chat_id' => $telegramId);
     if ($tg->status == 'revoke_access') {
         $clientId = preg_replace('/[^0-9,.]/', '', $message['text']);
         try {
             $app = App::findByClientId($clientId);
             $auth = Auth::findByAppAndTelegramUser($app, $tg);
             $auth->active = false;
             $auth->save();
             $text = 'Access to this app has been revoked.';
             $tg->status = 'access_revoked';
             $params['reply_markup'] = json_encode(['hide_keyboard' => true]);
         } catch (ModelNotFoundException $e) {
             $text = 'Unknown app. Please choose an app from the given list:';
         }
     } else {
         $text = 'Unknown command.';
         $tg->status = 'unknown_command';
         $params['reply_markup'] = json_encode(['hide_keyboard' => true]);
     }
     $params['text'] = $text;
     $this->send($params);
     $tg->save();
 }