Created
March 10, 2017 15:33
-
-
Save cyberlex404/4a8024de6833f03f302e8c59367fc56e to your computer and use it in GitHub Desktop.
dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\telegram_holiday\Controller; | |
| use Drupal\Core\Access\AccessResult; | |
| use Drupal\Core\Config\ConfigFactory; | |
| use Drupal\Core\Config\ConfigFactoryInterface; | |
| use Drupal\Core\Controller\ControllerBase; | |
| use Drupal\Core\Entity\Query\QueryFactory; | |
| use Drupal\holiday\Entity\Inquiry; | |
| use Drupal\holiday\InquiryInterface; | |
| use Drupal\telegram_holiday\Commands\QuestionCommand; | |
| use Drupal\telegram_holiday\Entity\TelegramDialog; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| use Drupal\Core\Config\Config; | |
| use Symfony\Component\HttpFoundation\Response; | |
| use Telegram\Bot\Api; | |
| use Telegram\Bot\Commands\Command; | |
| use Telegram\Bot\Objects\Update; | |
| /** | |
| * Class Webhook. | |
| * | |
| * @package Drupal\telegram_holiday\Controller | |
| */ | |
| class Webhook extends ControllerBase { | |
| protected $commandsList = [ | |
| 'start' => '\Drupal\telegram_holiday\Commands\StartCommand::class', | |
| 'notify' => '\Drupal\telegram_holiday\Commands\NotifyCommand::class', | |
| 'inquiry' => '\Drupal\telegram_holiday\Commands\InquiryCommand::class', | |
| ]; | |
| /** | |
| * Drupal\Core\Config\Config definition. | |
| * | |
| * @var \Drupal\Core\Config\Config | |
| */ | |
| protected $telegramConfig; | |
| /** | |
| * @var Api | |
| */ | |
| protected $telegram; | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function __construct($config_factory, | |
| QueryFactory $entity_query, | |
| $entity_type_manager) { | |
| /** | |
| * @var $config_factory ConfigFactoryInterface | |
| */ | |
| $this->telegramConfig = $config_factory->get('telegram_holiday.telegrambotconfig'); | |
| $this->entityQuery = $entity_query; | |
| $this->entityTypeManager = $entity_type_manager; | |
| $token = $this->telegramConfig->get('token'); | |
| $this->telegram = new Api($token); | |
| $this->telegram->addCommands([ | |
| \Drupal\telegram_holiday\Commands\StartCommand::class, | |
| // \Drupal\telegram_holiday\Commands\HelpCommand::class, | |
| //\Drupal\telegram_holiday\Commands\MenuCommand::class, | |
| \Drupal\telegram_holiday\Commands\NotifyCommand::class, | |
| \Drupal\telegram_holiday\Commands\InquiryCommand::class, | |
| ]); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function create(ContainerInterface $container) { | |
| return new static( | |
| $container->get('config.factory'), | |
| $container->get('entity.query'), | |
| $container->get('entity_type.manager') | |
| ); | |
| } | |
| /** | |
| * Webhook. | |
| * | |
| * @return string | |
| * Return Hello string. | |
| */ | |
| public function webhook($telegram_token) { | |
| $rawData = file_get_contents("php://input"); | |
| \Drupal::logger('telegram_no_error')->warning($rawData); | |
| try { | |
| $update = $this->telegram->commandsHandler(true); | |
| if($update->getUpdateId()) { | |
| $this->checkDialogs($update); | |
| $info = [ | |
| 'Update ID:' . $update->getUpdateId(), | |
| 'Text: ' . $update->getMessage()->getText(), | |
| 'User: ' . $update->getMessage()->getFrom()->getUsername(), | |
| 'User ID: ' . $update->getMessage()->getFrom()->getId(), | |
| ]; | |
| $message = implode(" ", $info); | |
| \Drupal::logger('telegram_holiday')->info($message); | |
| } | |
| } catch (\Telegram\Bot\Exceptions\TelegramResponseException $e) { | |
| \Drupal::logger('telegram_bot_try_error')->error($e->getMessage()); | |
| echo $e; | |
| } | |
| return [ | |
| '#type' => 'markup', | |
| '#markup' => $this->t('telegram Bot'), | |
| ]; | |
| } | |
| protected function checkDialogs(Update $update) { | |
| $fromId = $update->getMessage()->getFrom()->getId(); | |
| $dialog = TelegramDialog::load($fromId); | |
| if ($dialog instanceof TelegramDialog) { | |
| $command = $dialog->getCommand(); | |
| $commands = $this->telegram->getCommands(); | |
| $commandList = []; | |
| foreach ($commands as $name => $item) { | |
| $commandList[$name] = $item; | |
| } | |
| if ($commandList[$command] instanceof QuestionCommand) { | |
| \Drupal::logger('telegram_holiday')->warning('Это команда с вопросами: '. $command); | |
| }elseif ($commandList[$command] instanceof Command) { | |
| \Drupal::logger('telegram_holiday')->warning('Это команда БЕЗ вопросов: '. $command); | |
| } | |
| \Drupal::logger('telegram_holiday_c')->info('Команды : '. implode("-" , array_keys($commandList))); | |
| $commandMethod = $command . 'Exec'; | |
| if(method_exists($this,$commandMethod)) { | |
| $this->$commandMethod($update, $dialog); | |
| }else{ | |
| \Drupal::logger('telegram_holiday')->error('Вызов несуществующего метода: '. $commandMethod); | |
| } | |
| } | |
| \Drupal::logger('telegram_holiday')->error('uid: '. $fromId); | |
| } | |
| public function notifyExec(Update $update, TelegramDialog $dialog) { | |
| $fromId = $update->getMessage()->getFrom()->getId(); | |
| $mesageText = $update->getMessage()->getText(); | |
| /** | |
| * @var $dialog TelegramDialog | |
| */ | |
| $data = $dialog->getData(); | |
| if(is_null($dialog->getData()['inquiry']) && $dialog->getQuestion() != 'inquiry') { | |
| $this->telegram->sendMessage([ | |
| 'chat_id' => $fromId, | |
| 'text' => "Номер заявки\xE2\x9D\x93" . $dialog->getQuestion(), | |
| ]); | |
| $dialog->setQuestion('inquiry')->save(); | |
| }elseif(is_null($dialog->getData()['inquiry']) && $dialog->getQuestion() == 'inquiry') { | |
| $dialog->setDataItem('inquiry', (int)$mesageText)->save(); | |
| $data = $dialog->get('data'); | |
| } | |
| if(!is_null($data['inquiry']) && is_null($data['access_code']) && $dialog->getQuestion() != 'access_code') { | |
| $this->telegram->sendMessage([ | |
| 'chat_id' => $fromId, | |
| 'text' => "Код доступа\xE2\x9D\x93", | |
| ]); | |
| $dialog->setQuestion('access_code')->save(); | |
| }elseif (is_null($data['access_code']) && $dialog->getQuestion() == 'access_code') { | |
| $inquiry = $this->entityTypeManager()->getStorage('inquiry')->load($dialog->getData()['inquiry']); | |
| if ($inquiry instanceof InquiryInterface) { | |
| $accessCode = $inquiry->getAccessCode(); | |
| if ($accessCode == $mesageText) { | |
| $dialog->setDataItem('access_code', $mesageText)->save(); | |
| }else{ | |
| $this->telegram->sendMessage([ | |
| 'chat_id' => $fromId, | |
| 'text' => "Неверный код. Попробуйте еще раз...", | |
| ]); | |
| } | |
| }else { | |
| $this->telegram->sendMessage([ | |
| 'chat_id' => $fromId, | |
| 'text' => "Видимо такой заявки не существует...\xF0\x9F\x90\x9E", | |
| ]); | |
| } | |
| //$inquiry = Inquiry::load($dialog->getData()['inquiry']); | |
| } | |
| $checkData = $dialog->getData(); | |
| if(!is_null($checkData['inquiry']) && !is_null($checkData['access_code'])) { | |
| $this->telegram->sendMessage([ | |
| 'chat_id' => $fromId, | |
| 'text' => "Окей! Настраиваю... \xF0\x9F\x9A\x80\xF0\x9F\x9A\x80\xF0\x9F\x9A\x80" | |
| ]); | |
| } | |
| } | |
| protected function execQuestion(Update $update, TelegramDialog $dialog) { | |
| } | |
| /** | |
| * Checks access for a specific request. | |
| * | |
| * @param \Drupal\Core\Session\AccountInterface $account | |
| * Run access checks for this account. | |
| */ | |
| public function access($telegram_token) { | |
| if($this->telegramConfig->get('token') == $telegram_token) { | |
| return AccessResult::allowed(); | |
| }else{ | |
| return AccessResult::forbidden(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment