簡體   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