From: Alexander Smirnov Date: Mon, 23 Sep 2024 13:15:02 +0000 (+0300) Subject: [ERP-184] база и апи приёма чат-логов X-Git-Tag: 1.5~8^2 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=2689083bf082202f2466222ee3635ca1afd69932;p=erp24_rep%2Fyii-erp24%2F.git [ERP-184] база и апи приёма чат-логов --- 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', + ]; + } +}