简体   繁体   中英

Botman yii2. Properties of the conversation lost

BotMan Version: 2.1 PHP Version:7.3 Messaging Service(s): Cache Driver: SymfonyCache Description: I trying to have conversation. In every next method I lost data from conversation properties, that was saved in properties before!

class GetAlertDataConversation extends AppConversation
{
   public $bot;

   public $alertTitle;

   public $alertDescription;

   public $alertLatitude;

   public $alertLongitude;

   public $alertAuthorId;

   public $alertAuthorName;

   public function __construct($bot)
   {
      $this->bot = $bot;
   }


   private function askTitle()
   {

      $this->ask('Что случилось? (кратко)', function (Answer $answer) {
         $this->alertTitle = $this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askDescription');
         \Yii::warning($this->alertTitle);
      });
   }

   public function askDescription()
   {

      $this->ask('Расскажи подробней!', function (Answer $answer) {
         $this->alertDescription = $this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askLocation');
         \Yii::warning($this->alertTitle);
      });
   }

   private function askLocation()
   {
      \Yii::warning($this->alertTitle);
      $this->askForLocation('Локация?', function (Location $location) {
         // $location is a Location object with the latitude / longitude.
         $this->alertLatitude = $location->getLatitude();
         $this->alertLongitude = $location->getLongitude();
         $this->endConversation();
         return true;
      });
   }

   private function endConversation()
   {
      \Yii::warning($this->alertTitle);
      $alertId = $this->saveAlertData();
      if ($alertId)
         $this->say("Событие номер {$alertId} зарегистрировано!");
      else
         $this->say("Ошибка при сохранении события, обратитесь к администратору!");
   }

   private function saveAlertData()
   {

      $user = $this->bot->getUser();
      $this->alertAuthorId = $user->getId();
      $this->alertAuthorName = $user->getFirstName() . ' ' . $user->getLastName();


      $alert = new Alert();
      \Yii::warning($this->alertTitle);
      $alert->name = $this->alertTitle;
      $alert->description = $this->alertDescription;
      $alert->latitude = $this->alertLatitude;
      $alert->longitude = $this->alertLongitude;
      $alert->author_id = $this->alertAuthorId;
      $alert->author_name = $this->alertAuthorName;
      $alert->chat_link = '';
      $alert->additional = '';
      if ($alert->validate()) {
         $alert->save();
         return $alert->id;
      } else {
         \Yii::warning($alert->errors);
         \Yii::warning($alert);
         return false;
      }
   }
}

There is user's text answer in the first \Yii::warning($this->alertTitle);in the askTitle() function. But all other \Yii::warning($this->alertTitle); returns NULL,,.? As the result, saving of Alert object not working. Please. help me. Some ideas? I think, that it can be by some caching + serialise problem. I was trying to change cache method to Redis. Same result.

Problem was in this function call and caching:

$this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askDescription');

If you will read other conversation botman issues in github, you wil see, that most of issues in conversation cache and serialise. Not any PHP code of conversation can be cached right. In this case, not direct call of functions askDescription() and askLocation() broken conversation caching. I fixed that by removing askSomethingWithLettersCounting() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM