From 2689083bf082202f2466222ee3635ca1afd69932 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Mon, 23 Sep 2024 16:15:02 +0300 Subject: [PATCH] =?utf8?q?[ERP-184]=20=D0=B1=D0=B0=D0=B7=D0=B0=20=D0=B8=20?= =?utf8?q?=D0=B0=D0=BF=D0=B8=20=D0=BF=D1=80=D0=B8=D1=91=D0=BC=D0=B0=20?= =?utf8?q?=D1=87=D0=B0=D1=82-=D0=BB=D0=BE=D0=B3=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../controllers/ChatbotActionController.php | 47 ++++++++++++++++ ...923_084450_create_table_chatbot_action.php | 33 ++++++++++++ erp24/records/ChatbotAction.php | 53 +++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 erp24/api2/controllers/ChatbotActionController.php create mode 100755 erp24/migrations/m240923_084450_create_table_chatbot_action.php create mode 100644 erp24/records/ChatbotAction.php diff --git a/erp24/api2/controllers/ChatbotActionController.php b/erp24/api2/controllers/ChatbotActionController.php new file mode 100644 index 00000000..7dfac164 --- /dev/null +++ b/erp24/api2/controllers/ChatbotActionController.php @@ -0,0 +1,47 @@ +response->format = \yii\web\Response::FORMAT_JSON; + + $request = Yii::$app->request->getRawBody(); + + try { + $result = Json::decode($request); + } catch (\Exception $ex) { + return $this->asJson(['error' => ['code' => 400, 'message' => 'Json body invalid']]); + } + + foreach (['phone', 'action'] as $field) { + if (empty($result[$field])) { + return $this->asJson(['error_id' => 2, 'error' => ['code' => 400, 'message' => 'Missing ' . $field]]); + } + } + + $phone = ClientHelper::phoneClear($result['phone']); + if (!ClientHelper::phoneVerify($phone)) { + return $this->asJson(['error_id' => 3, 'error' => ['code' => 400, 'message' => 'Phone is required']]); + } + + $chatbotAction = new ChatbotAction; + $chatbotAction->phone = $phone; + $chatbotAction->action = $result['action']; + $chatbotAction->created_at = date('Y-m-d H:i:s'); + $chatbotAction->json = $result['payload'] ? Json::encode($result['payload']) : null; + $chatbotAction->save(); + + if ($chatbotAction->getErrors()) { + return $this->asJson(['error_id' => 4, 'error' => ['code' => 400, 'message' => Json::encode($chatbotAction->getErrors())]]); + } + + return $this->asJson(['response' => true]); + } +} \ No newline at end of file diff --git a/erp24/migrations/m240923_084450_create_table_chatbot_action.php b/erp24/migrations/m240923_084450_create_table_chatbot_action.php new file mode 100755 index 00000000..6e8d1fb4 --- /dev/null +++ b/erp24/migrations/m240923_084450_create_table_chatbot_action.php @@ -0,0 +1,33 @@ +createTable(self::TABLE_NAME, [ + 'id' => $this->primaryKey(), + 'phone' => $this->string(16)->notNull()->comment('Номер телефона клиента'), + 'created_at' => $this->dateTime()->notNull()->comment('Дата создания'), + 'action' => $this->string()->notNull()->comment('Текстовое описание действия. Например, pressInfoBtn'), + 'json' => $this->text()->null()->comment('Доп.поле в формате json') + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable(self::TABLE_NAME); + } +} diff --git a/erp24/records/ChatbotAction.php b/erp24/records/ChatbotAction.php new file mode 100644 index 00000000..207d696c --- /dev/null +++ b/erp24/records/ChatbotAction.php @@ -0,0 +1,53 @@ + 16], + [['action'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'phone' => 'Phone', + 'created_at' => 'Created At', + 'action' => 'Action', + 'json' => 'Json', + ]; + } +} -- 2.39.5