--- /dev/null
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use yii\helpers\Json;
+use yii_app\helpers\ClientHelper;
+use yii_app\records\ChatbotAction;
+
+class ChatbotActionController extends BaseController
+{
+ public function actionLog() {
+ Yii::$app->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
--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240923_084450_create_table_chatbot_action
+ */
+class m240923_084450_create_table_chatbot_action extends Migration
+{
+ const TABLE_NAME = 'erp24.chatbot_action';
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $this->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);
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "chatbot_action".
+ *
+ * @property int $id
+ * @property string $phone Номер телефона клиента
+ * @property string $created_at Дата создания
+ * @property string $action Текстовое описание действия. Например, pressInfoBtn
+ * @property string|null $json Доп.поле в формате json
+ */
+class ChatbotAction extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'chatbot_action';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['phone', 'created_at', 'action'], 'required'],
+ [['created_at'], 'safe'],
+ [['json'], 'string'],
+ [['phone'], 'string', 'max' => 16],
+ [['action'], 'string', 'max' => 255],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function attributeLabels()
+ {
+ return [
+ 'id' => 'ID',
+ 'phone' => 'Phone',
+ 'created_at' => 'Created At',
+ 'action' => 'Action',
+ 'json' => 'Json',
+ ];
+ }
+}