/
var
/
www
/
myprog.com.ua
/
telegram
/
controllers
/
/var/www/myprog.com.ua/telegram/controllers
mkdir
upload
Name
Size
Mode
Actions
class.api.php
8697
0644
edit
dl
rm
Edit:
/var/www/myprog.com.ua/telegram/controllers/class.api.php
(8697B)
<?php /** * @author Sergey Kozinets * @copyright 2018 * @package MyProg Telegram */ class api extends controller { private $errors = array(); public function __construct (){ parent::__construct(); $this->errors[0] = 'OK'; $this->errors[1] = 'Unknown error occured'; $this->errors[2] = 'Application is disabled. Enable your application'; $this->errors[3] = 'Unknown method passed'; $this->errors[4] = 'Incorrect signature'; $this->errors[5] = 'User authorization failed'; $this->errors[6] = 'Invalid request'; $this->errors[7] = 'Empty database'; $this->errors[8] = 'User authorization required'; $this->method_no_autorized[] = 'ping'; //$this->client_id = 0; $this->su = false; // Superuser ['menu_admin'] } public function index($url = 0){ $this->view->render('index'); } public function webhook($url = 0){ require_once "libs/vendor/autoload.php"; bootstrap::log("\nSTART ".$url[0]); try { $bot = new \TelegramBot\Api\Client(TOKEN); $body = $bot->getRawBody(); if(!empty($body)){ $json = json_decode($body,true); // Parse JSON if(isset($json['message'])){ if($json['message']['text'][0]=='/'){ $this->command($bot,$json); }else{ // bootstrap::log($body); $this->text($bot,$json); } }else{ if(isset($json['callback_query'])){ $this->callback($bot,$json); }else{ bootstrap::log("NEW QUERY"); } } //$bot->run(); bootstrap::log($body); } } catch (\TelegramBot\Api\Exception $e) { bootstrap::log($e->getMessage()); } } public function text($bot,$json){ $text = $json['message']['text']; $message_id = $json['message']['message_id']; bootstrap::log('TEXT '.$text.' MESSAGE ID '.$message_id); $chat_id = $json['message']['chat']['id']; $user_name = $json['message']['chat']['first_name']; $last_command = $this->model->get_last_command($chat_id); switch($last_command){ case '/pro': $system = $this->model->select_system($text); if($system==null){ $this->error($bot,$chat_id, 'Cистема '.$text.' не найдена!'); return; } if($this->model->set_system($chat_id,$system['id'])==null){ $this->error($bot,$chat_id, 'Ошибка подключения к системе '.$text.'!'); return; } $this->model->set_last_command($chat_id,'/pro_key'); $bot->sendMessage($chat_id, 'Введите ключ системы:'); break; case '/pro_key': //$bot->editMessageText($chat_id,$message_id,'***'); //$bot->sendMessage($chat_id, '***'); $user = $this->model->get_user($chat_id); if($user==null){ $this->error($bot,$chat_id, 'Ошибка подключения к системе!'); return; } if($user['system_id']==0){ $this->error($bot,$chat_id, 'Система не выбрана! Начните сначала /start'); return; } $system = $this->model->get_system($user['system_id']); if($system==null){ $this->error($bot,$chat_id, 'Cистема не найдена! Начните сначала /start'); return; } if($system['api_key']==$text){ $bot->sendMessage($chat_id, 'Вы подключились к системе '.$system['app_name']); $bot->sendMessage($chat_id, 'Введите логин:'); $this->model->set_last_command($chat_id,'/pro_login'); }else{ $this->error($bot,$chat_id, 'Ключ не верный! Начните сначала /start'); return; } break; case '/pro_login': default: break; } $bot->run(); } public function callback($bot,$json){ $data = $json['callback_query']['data']; bootstrap::log('CALLBACK '.$data); $chat_id = $json['callback_query']['message']['chat']['id']; $message_id = $json['callback_query']['message']['message_id']; $callback_id = $json['callback_query']['id']; $callback_message = 'Выполняю...'; switch($data){ case '/demo': $callback_message = "Подключение к DEMO"; $bot->sendMessage($chat_id, 'Подключение к демо системе...'); break; case '/pro': $callback_message = "Авторизация в PRO"; $bot->sendMessage($chat_id, 'Введите имя системы:'); $this->model->set_last_command($chat_id,$data); break; default: $callback_message = 'Комманда не найдена'; break; } $bot->answerCallbackQuery($callback_id,$callback_message,false); $bot->run(); } public function command($bot,$json){ $cmd = $json['message']['text']; bootstrap::log('COMMAND '.$cmd); $chat_id = $json['message']['chat']['id']; // Авторизация $user = $this->model->get_user($chat_id); if($user==null)$user = $this->model->add_user($chat_id, $json['message']['chat']['first_name'], $json['message']['chat']['last_name'], $json['message']['text']); if($user==null){$this->error($bot,$chat_id,"Что-то пошло не так :( Попробуйте позже...");return;} switch($cmd){ case '/start': $answer = 'Добро пожаловать '.$user['t_user_first_name'].'! Я бот MyProg который облегчит Вам жизнь :)'."\n"; $answer .= 'cmd='.$cmd."\n"; $answer .= 'chat_id='.$chat_id."\n"; $answer .= 'last_command='.$this->model->get_last_command($chat_id)."\n"; if($user['system_auth']=='yes'){ $answer .= 'Вы авторизованы в MyProg'."\n"; }else{ $answer .= 'Для начала выберите систему'."\n"; } // $keyboard = new \TelegramBot\Api\Types\ForceReply(); $keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup( [ [ ['text' => 'DEMO', 'callback_data' => '/demo'], ['text' => 'PRO', 'callback_data' => '/pro'], ['text' => 'WEB MYPROG', 'url' => 'https://myprog.com.ua'], ['text' => 'WEB DEMO', 'url' => 'https://demo.myprog.com.ua'], ] ],true ); $bot->sendMessage($chat_id, $answer, null, false, null, $keyboard); break; case '/help': $answer = 'Команды: /help - вывод справки /menu - главное меню'; $bot->sendMessage($chat_id, $answer); break; case '/menu': if($user['system_auth']=='yes'){ $answer = 'Главное меню'; $keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup( [ [ ['text' => 'Приход', 'callback_data' => '/incoming'], ['text' => 'Расход', 'callback_data' => '/consumption'], ['text' => 'Мои счета', 'callback_data' => '/bills'], ] ] ); $bot->sendMessage($chat_id, $answer, null, false, null, $keyboard); }else{ $answer = 'Вы не авторизованы. Выполните /start и подключитесь к системе'."\n"; $bot->sendMessage($chat_id, $answer); } break; case '/free_menu': $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(array(array("Курсы валют", "Погода")), true); // true for one-time keyboard $answer = 'Фичи'; $bot->sendMessage($chat_id, $answer, null, false, null, $keyboard); break; case '/get_phone': $answer = "Hi"; $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup([[["text" => "\xF0\x9F\x93\xB2 Добавить телефон"]]], false, false, false); //$answer = "<b>Ваш телефон добавлен к списку контактов!</b>"; $bot->sendMessage($chat_id, $answer, 'HTML', false, null, $keyboard); //$bot->sendMessage($chat_id, $answer); break; case '/clear': $bot->call('deleteMessages'); break; case '/hide': $answer = "Hide"; $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardRemove(); //$answer = "<b>Ваш телефон добавлен к списку контактов!</b>"; $bot->sendMessage($chat_id, $answer, 'HTML', false, null, $keyboard); break; case '/ping': $answer = 'pong'; $bot->sendMessage($chat_id, $answer); break; default: $bot->sendMessage($chat_id, "Комманда не распознана!"); break; } $this->model->set_last_command($chat_id,$cmd); $bot->run(); } public function error($bot,$chat_id,$msg){ bootstrap::log("ERROR chat_id=".$chat_id." ".$msg); $this->model->set_last_command($chat_id,''); $bot->sendMessage($chat_id, $msg); $bot->run(); } }
Save
cmd:
run