繁体   English   中英

机器人 yii2。 会话的属性丢失

[英]Botman yii2. Properties of the conversation lost

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() function 中。 但所有其他 \Yii::warning($this->alertTitle); 返回 NULL,,.? 结果,警报 object 的保存不起作用。 请。 帮我。 一些想法? 我认为,这可能是一些缓存+序列化问题。 我试图将缓存方法更改为 Redis。 结果相同。

问题出在这个 function 调用和缓存中:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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