{
  "openapi": "3.0.4",
  "info": {
    "title": "TGBot SaaS API",
    "description": "Multi-tenant Telegram support API. Customers DM a bot; each customer becomes a forum topic in the bot's manager supergroup, and managers reply inside the topic. The webhook endpoint is called by Telegram — the management endpoints provision companies/bots and search leads.",
    "version": "v1"
  },
  "paths": {
    "/api/bots/{botId}/set-webhook": {
      "post": {
        "tags": [
          "Bots"
        ],
        "summary": "Register the bot's Telegram webhook",
        "description": "Calls setWebhook pointing Telegram at /webhook/{companyToken}/{botId}, using the company token as the secret.",
        "operationId": "SetBotWebhook",
        "parameters": [
          {
            "name": "botId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookResultModel"
                }
              }
            }
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },
    "/api/bots/{botId}/registration": {
      "put": {
        "tags": [
          "Bots"
        ],
        "summary": "Configure the bot's pre-registration flow",
        "description": "Enables/disables registration, toggles which fields are collected, and sets the editable welcome and post-registration messages.",
        "operationId": "UpdateBotRegistration",
        "parameters": [
          {
            "name": "botId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RegistrationSettingsRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BotModel"
                }
              }
            }
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },
    "/api/bots/{botId}/webhook": {
      "delete": {
        "tags": [
          "Bots"
        ],
        "summary": "Remove the bot's Telegram webhook",
        "operationId": "DeleteBotWebhook",
        "parameters": [
          {
            "name": "botId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "No Content"
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },
    "/api/bots/{botId}/topics": {
      "get": {
        "tags": [
          "Bots"
        ],
        "summary": "Search leads by property",
        "description": "Returns the bot's topics whose lead has a property matching the given key (and optional value contains-filter).",
        "operationId": "SearchTopicsByLeadProperty",
        "parameters": [
          {
            "name": "botId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "key",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "value",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/TopicModel"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/companies": {
      "post": {
        "tags": [
          "Companies"
        ],
        "summary": "Create a company",
        "description": "Creates a tenant company and generates its unique webhook token.",
        "operationId": "CreateCompany",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateCompanyRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CompanyModel"
                }
              }
            }
          }
        }
      },
      "get": {
        "tags": [
          "Companies"
        ],
        "summary": "List companies",
        "operationId": "ListCompanies",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/CompanyModel"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/companies/{companyId}/bots": {
      "post": {
        "tags": [
          "Companies"
        ],
        "summary": "Register a bot under a company",
        "description": "Validates the token via getMe, stores the bot id/username, and binds it to the manager supergroup.",
        "operationId": "CreateBot",
        "parameters": [
          {
            "name": "companyId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateBotRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BotModel"
                }
              }
            }
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },
    "/api/topics/{topicId}/properties": {
      "put": {
        "tags": [
          "Topics"
        ],
        "summary": "Set a lead property",
        "description": "Creates or updates a searchable key/value property on the topic's lead.",
        "operationId": "UpsertLeadProperty",
        "parameters": [
          {
            "name": "topicId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SetPropertyRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TopicModel"
                }
              }
            }
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },
    "/webhook/{companyToken}/{botId}": {
      "post": {
        "tags": [
          "Webhook"
        ],
        "summary": "Telegram webhook receiver",
        "description": "Called by Telegram for every update of a company's bots. Authenticates the company token (also sent back in the X-Telegram-Bot-Api-Secret-Token header), enqueues the raw update for off-thread processing, and returns 200 immediately. Not intended to be called by clients directly.",
        "operationId": "TelegramWebhook",
        "parameters": [
          {
            "name": "companyToken",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "botId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": { }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK"
          },
          "401": {
            "description": "Unauthorized"
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "BotModel": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "companyId": {
            "type": "string",
            "format": "uuid"
          },
          "telegramBotId": {
            "type": "integer",
            "format": "int64"
          },
          "username": {
            "type": "string",
            "nullable": true
          },
          "title": {
            "type": "string",
            "nullable": true
          },
          "managerChatId": {
            "type": "integer",
            "format": "int64"
          },
          "isActive": {
            "type": "boolean"
          },
          "registrationEnabled": {
            "type": "boolean"
          },
          "collectFirstName": {
            "type": "boolean"
          },
          "collectLastName": {
            "type": "boolean"
          },
          "collectPhone": {
            "type": "boolean"
          },
          "collectEmail": {
            "type": "boolean"
          },
          "welcomeMessage": {
            "type": "string",
            "nullable": true
          },
          "postRegistrationMessage": {
            "type": "string",
            "nullable": true
          },
          "firstNamePrompt": {
            "type": "string",
            "nullable": true
          },
          "lastNamePrompt": {
            "type": "string",
            "nullable": true
          },
          "phonePrompt": {
            "type": "string",
            "nullable": true
          },
          "emailPrompt": {
            "type": "string",
            "nullable": true
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": false
      },
      "CompanyModel": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string",
            "nullable": true
          },
          "webhookToken": {
            "type": "string",
            "nullable": true
          },
          "isActive": {
            "type": "boolean"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": false
      },
      "CreateBotRequest": {
        "type": "object",
        "properties": {
          "token": {
            "type": "string",
            "description": "Bot API token from BotFather.",
            "nullable": true
          },
          "managerChatId": {
            "type": "integer",
            "description": "Chat id of the forum supergroup where customer topics are created. The bot must be an admin there with \"Manage Topics\".",
            "format": "int64"
          },
          "title": {
            "type": "string",
            "description": "Optional display title; defaults to the bot's name from getMe.",
            "nullable": true
          }
        },
        "additionalProperties": false,
        "description": "Payload to register a Telegram bot under a company."
      },
      "CreateCompanyRequest": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "description": "Display name of the company.",
            "nullable": true
          }
        },
        "additionalProperties": false,
        "description": "Payload to create a new tenant company."
      },
      "LeadPropertyModel": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "key": {
            "type": "string",
            "nullable": true
          },
          "value": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      },
      "RegistrationSettingsRequest": {
        "type": "object",
        "properties": {
          "enabled": {
            "type": "boolean",
            "description": "Master switch for the registration flow."
          },
          "collectFirstName": {
            "type": "boolean",
            "description": "Ask for the customer's first name."
          },
          "collectLastName": {
            "type": "boolean",
            "description": "Ask for the customer's last name."
          },
          "collectPhone": {
            "type": "boolean",
            "description": "Ask for the customer's phone."
          },
          "collectEmail": {
            "type": "boolean",
            "description": "Ask for the customer's email."
          },
          "welcomeMessage": {
            "type": "string",
            "description": "Message sent first; null/empty uses the built-in default.",
            "nullable": true
          },
          "postRegistrationMessage": {
            "type": "string",
            "description": "Message sent after all fields; null/empty uses the built-in default.",
            "nullable": true
          },
          "firstNamePrompt": {
            "type": "string",
            "description": "Question asked for the first name; null/empty uses the built-in default.",
            "nullable": true
          },
          "lastNamePrompt": {
            "type": "string",
            "description": "Question asked for the last name; null/empty uses the built-in default.",
            "nullable": true
          },
          "phonePrompt": {
            "type": "string",
            "description": "Question asked for the phone; null/empty uses the built-in default.",
            "nullable": true
          },
          "emailPrompt": {
            "type": "string",
            "description": "Question asked for the email; null/empty uses the built-in default.",
            "nullable": true
          }
        },
        "additionalProperties": false,
        "description": "Payload to configure a bot's pre-registration flow and its editable messages."
      },
      "RegistrationStatus": {
        "enum": [
          0,
          1,
          2
        ],
        "type": "integer",
        "format": "int32"
      },
      "SetPropertyRequest": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string",
            "description": "Property name (e.g. \"source\").",
            "nullable": true
          },
          "value": {
            "type": "string",
            "description": "Property value (e.g. \"facebook\").",
            "nullable": true
          }
        },
        "additionalProperties": false,
        "description": "Payload to upsert a single searchable lead property on a topic."
      },
      "TopicModel": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "botId": {
            "type": "string",
            "format": "uuid"
          },
          "customerChatId": {
            "type": "integer",
            "format": "int64"
          },
          "messageThreadId": {
            "type": "integer",
            "format": "int32"
          },
          "title": {
            "type": "string",
            "nullable": true
          },
          "firstName": {
            "type": "string",
            "nullable": true
          },
          "lastName": {
            "type": "string",
            "nullable": true
          },
          "phone": {
            "type": "string",
            "nullable": true
          },
          "email": {
            "type": "string",
            "nullable": true
          },
          "customerUsername": {
            "type": "string",
            "nullable": true
          },
          "registrationStatus": {
            "$ref": "#/components/schemas/RegistrationStatus"
          },
          "properties": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LeadPropertyModel"
            },
            "nullable": true
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": false
      },
      "WebhookResultModel": {
        "type": "object",
        "properties": {
          "url": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      }
    }
  },
  "tags": [
    {
      "name": "Bots"
    },
    {
      "name": "Companies"
    },
    {
      "name": "Topics"
    },
    {
      "name": "Webhook"
    }
  ]
}