Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Created March 20, 2017 03:33
Show Gist options
  • Select an option

  • Save cyberlex404/8a2a61033725d3b222dbeed96c2987d2 to your computer and use it in GitHub Desktop.

Select an option

Save cyberlex404/8a2a61033725d3b222dbeed96c2987d2 to your computer and use it in GitHub Desktop.
Example Telegram bot
<?php
namespace Drupal\telegram_holiday\Plugin\TelegramBots;
use Drupal\telegram_bots_api\TelegramBotBase;
use Telegram\Bot\Commands\Command;
use Telegram\Bot\Objects\Update;
use Drupal\holiday\InquiryInterface;
use Drupal\telegram_holiday\Commands\QuestionCommand;
use Drupal\telegram_holiday\Entity\TelegramDialog;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @TelegramBot(
* id = "BraslavskieHolidayBot",
* bot = "braslavskie_holiday_bot",
* description = @Translation("BraslavskieHolidayBot"),
* token = "297444900:AAEsE27lmVuetArcC6PBzK7y3SMc1v6pia4"
* )
*/
class BraslavskieHolidayBot extends TelegramBotBase {
public function commands() {
return [
\Telegram\Bot\Commands\HelpCommand::class,
\Drupal\telegram_holiday\Commands\NotifyCommand::class,
\Drupal\telegram_holiday\Commands\InquiryCommand::class,
];
}
public function executeUpdate(Update $update) {
$fromId = $update->getMessage()->getFrom()->getId();
$dialog = TelegramDialog::load($fromId);
if ($dialog instanceof TelegramDialog) {
$command = $dialog->getCommand();
$commands = $this->api()->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->api->sendMessage([
'chat_id' => $fromId,
'text' => "\xE2\x96\xAAНомер заявки\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->api()->sendMessage([
'chat_id' => $fromId,
'text' => "\xE2\x96\xAAКод доступа\xE2\x9D\x93",
]);
$dialog->setQuestion('access_code')->save();
}elseif (is_null($data['access_code']) && $dialog->getQuestion() == 'access_code') {
$inquiry = \Drupal::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->api()->sendMessage([
'chat_id' => $fromId,
'text' => "Неверный код. Попробуйте еще раз...",
]);
}
}else {
$this->api()->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->api()->sendMessage([
'chat_id' => $fromId,
'text' => "Окей! Настраиваю... \xF0\x9F\x9A\x80\xF0\x9F\x9A\x80\xF0\x9F\x9A\x80"
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment