0

BotMan 版本:2.1 PHP 版本:7.3 消息服务:缓存驱动程序:SymfonyCache 描述:我试图进行对话。在每个 next 方法中,我都从对话属性中丢失了数据,这些数据之前保存在属性中!

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;
      }
   }
}

\Yii::warning($this->alertTitle);askTitle() 函数的第一个有用户的文本答案。但所有其他 \Yii::warning($this->alertTitle); 返回 NULL!!!! 结果,保存警报对象不起作用!请帮我。一些想法?我认为,这可能是一些缓存+序列化问题。我试图将缓存方法更改为 Redis。结果相同。

4

1 回答 1

0

问题出在此函数调用和缓存中:

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

如果您将在 github 中阅读其他对话 botman 问题,您将看到对话缓存和序列化中的大多数问题。不能正确缓存任何 PHP 对话代码。在这种情况下,不直接调用函数 askDescription() 和 askLocation() 会破坏对话缓存。我通过删除 askSomethingWithLettersCounting() 函数来解决这个问题。

于 2020-12-25T09:32:52.457 回答