public function resolve(Application $app)
 {
     $content = json_decode($app->getRequest()->getContent(), true);
     if (isset($content['message'])) {
         $message = $content['message'];
         $session_id = $message['from']['id'];
         $app->getSession($session_id);
         $app->setSession('first_name', $message['from']['first_name']);
         $app->setSession('last_name', $message['from']['last_name']);
         $app->saveSession();
         $text = ltrim($message['text'], '/');
         $commands = explode(' ', $text);
         $command = ucfirst($commands[0]);
         $response = 'Unknown command :(';
         if (in_array($command, $app->getTelegram()->getActionList())) {
             $action_class = "TelegramBot\\Bot\\Action\\{$command}";
             if (class_exists($action_class)) {
                 /** @var ActionBase $Action */
                 $Action = new $action_class();
                 $response = $Action->execute($message, $app);
             }
         }
         $Command = new SendMessage();
         $app->getApiRequest()->send($Command, ['chat_id' => $message['chat']['id'], 'text' => $response]);
     }
 }
示例#2
0
 /**
  * @param array $message
  * @param Application $app
  * @return string
  */
 public function execute(array $message, Application $app)
 {
     $return = '';
     $params = explode(' ', ltrim($message['text'], '/'));
     $params_cnt = count($params);
     if ($params_cnt == 1) {
         $session = $app->getSession();
         if (isset($session['routes'])) {
             $return .= "Your routes: \n";
             foreach ($session['routes'] as $name => $location) {
                 $return .= $name . ' - ' . $location . "\n";
             }
         }
         $return .= 'Use /' . $this->getName() . " <name> <address> to add new route point \n";
         $return .= 'Use /' . $this->getName() . " <name> to start \n";
     } else {
         if ($params_cnt == 3) {
             $session = $app->getSession();
             $routes = isset($session['routes']) ? $session['routes'] : [];
             $routes[$params[1]] = $params[2];
             $app->setSession('routes', $routes);
             $app->saveSession();
             $return = 'Route added!';
         } else {
             if ($params_cnt == 2) {
                 $session = $app->getSession();
                 if (isset($session['routes']) && isset($session['routes'][$params[1]])) {
                     $return .= 'Start route to: ' . $session['routes'][$params[1]] . "\n";
                 } else {
                     $return .= 'Unknown route ' . $params[1] . "\n";
                 }
             }
         }
     }
     return $return;
 }