【问题标题】:Botman yii2. Properties of the conversation lostBotman yii2.会话的属性丢失
【发布时间】:2020-12-07 10:22:37
【问题描述】:

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

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

【问题讨论】:

    标签: php yii2 botman


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2012-11-04
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      相关资源
      最近更新 更多