> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wegly.com.br/llms.txt
> Use this file to discover all available pages before exploring further.

# Criação manual de chat

> Cria ou reaproveita um chat manualmente e sempre dispara um template do WhatsApp como primeira mensagem. - É obrigatório informar `template.name` e `template.language`. - O destino deve ser enviado por `wa_contact_id` ou `person_contact_id`. - Quando `person_contact_id` é usado, ele tem prioridade sobre `wa_contact_id`. - O retorno segue o mesmo formato da listagem resumida de chats.




## OpenAPI

````yaml /openapi.json post /integrations/external/chats
openapi: 3.0.3
info:
  title: >-
    Integração Externa - Leads, Oportunidades, Tarefas, Arquivos, Propostas,
    Chats, Satisfação e Campos Personalizados
  version: 1.0.0
  description: >
    Documentação oficial da API externa da Wegly para campos personalizados,
    leads, oportunidades, tarefas, arquivos, propostas, chats e satisfação
    (`/integrations/external/custom-fields`, `/integrations/external/files`,
    `/integrations/external/leads`, `/integrations/external/deals`,
    `/integrations/external/tasks`, `/integrations/external/proposals`,
    `/integrations/external/chats` e `/integrations/external/satisfaction`).
    Todas as descrições estão em português e refletem as regras aplicadas pelo
    serviço.
servers:
  - url: https://api.wegly.com.br
    description: Ambiente de produção Wegly
security:
  - bearerAuth: []
tags:
  - name: Campos Personalizados
    description: >-
      Consulta dos campos personalizados disponíveis para uso nas integrações
      externas.
  - name: Arquivos
    description: >-
      Upload de arquivos públicos de integração vinculados diretamente a leads
      ou oportunidades.
  - name: Leads
    description: >-
      Operações de listagem, criação, consulta e atualização de leads via
      integração externa.
  - name: Oportunidades
    description: Operações de criação de oportunidades (deals) via integração externa.
  - name: Tarefas
    description: >-
      Operações de listagem, criação, consulta, atualização e remoção de tarefas
      vinculadas a leads, oportunidades, entregas, pessoas ou organizações.
  - name: Satisfação
    description: >-
      Geração de links públicos para formulários de satisfação via integração
      externa.
  - name: Propostas
    description: >-
      Operações de listagem, criação, consulta, atualização e remoção de
      propostas via integração externa.
  - name: Chats
    description: >-
      Operações principais de listagem, criação, consulta e resumo de não lidas
      de chats via integração externa.
paths:
  /integrations/external/chats:
    post:
      tags:
        - Chats
      summary: Criação manual de chat
      description: >
        Cria ou reaproveita um chat manualmente e sempre dispara um template do
        WhatsApp como primeira mensagem. - É obrigatório informar
        `template.name` e `template.language`. - O destino deve ser enviado por
        `wa_contact_id` ou `person_contact_id`. - Quando `person_contact_id` é
        usado, ele tem prioridade sobre `wa_contact_id`. - O retorno segue o
        mesmo formato da listagem resumida de chats.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateExternalChat'
            examples:
              chatManual:
                summary: Abertura manual com template
                value:
                  whatsapp_phone_id: 0b28ee57-7a57-4407-a9c5-5cd4e5a5fd6c
                  wa_contact_id: '11999999999'
                  template:
                    name: saudacao_inicial
                    language: pt_BR
                  template_fields:
                    - component: BODY
                      type: TEXT
                      index: 1
                      value: João
      responses:
        '201':
          description: Chat criado ou reaproveitado com sucesso.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatSummary'
        '400':
          description: >-
            Template obrigatório ausente, destino inválido ou outra regra de
            negócio não atendida.
        '401':
          description: Token de integração ausente ou inválido.
        '403':
          description: >-
            Permissão `crm/chats.write` ausente ou número fora do escopo da
            chave.
        '404':
          description: Número do WhatsApp, pessoa, contato ou agente não encontrados.
components:
  schemas:
    CreateExternalChat:
      type: object
      required:
        - whatsapp_phone_id
        - template
      anyOf:
        - required:
            - wa_contact_id
        - required:
            - person_contact_id
      properties:
        whatsapp_phone_id:
          type: string
          format: uuid
        wa_contact_id:
          type: string
          maxLength: 50
        person_contact_id:
          type: string
          format: uuid
        remote_profile_name:
          type: string
          maxLength: 255
          nullable: true
        crm_person_id:
          type: string
          format: uuid
          nullable: true
        assigned_agent_id:
          type: string
          format: uuid
          nullable: true
        status:
          $ref: '#/components/schemas/ChatStatusEnum'
        template:
          $ref: '#/components/schemas/ChatTemplateInfo'
        template_fields:
          type: array
          items:
            $ref: '#/components/schemas/ChatTemplateField'
    ChatSummary:
      type: object
      properties:
        id:
          type: string
          format: uuid
        wa_contact_id:
          type: string
        contact_name:
          type: string
        contact_number:
          type: string
        status:
          $ref: '#/components/schemas/ChatStatusEnum'
        chat_type:
          $ref: '#/components/schemas/ChatTypeEnum'
        unread_count:
          type: integer
        is_manually_unread:
          type: boolean
        priority:
          $ref: '#/components/schemas/ChatPriorityValue'
        archived_at:
          type: string
          format: date-time
          nullable: true
        assigned_agent_id:
          type: string
          format: uuid
          nullable: true
        phone_number:
          $ref: '#/components/schemas/ChatPhoneSummary'
        assigned_agent:
          type: object
          allOf:
            - $ref: '#/components/schemas/ChatAgentSummary'
          nullable: true
        last_message_at:
          type: string
          format: date-time
          nullable: true
        last_message:
          type: object
          allOf:
            - $ref: '#/components/schemas/ChatMessageSummary'
          nullable: true
        crm_person:
          type: object
          allOf:
            - $ref: '#/components/schemas/ChatCrmPersonSummary'
          nullable: true
    ChatStatusEnum:
      type: string
      enum:
        - OPEN
        - RESOLVED
        - SNOOZED
    ChatTemplateInfo:
      type: object
      required:
        - name
        - language
      properties:
        name:
          type: string
        language:
          type: string
    ChatTemplateField:
      type: object
      required:
        - component
        - type
        - value
      properties:
        component:
          type: string
          enum:
            - HEADER
            - BODY
            - BUTTONS
        type:
          type: string
          enum:
            - TEXT
            - IMAGE
            - VIDEO
            - DOCUMENT
            - LOCATION
        value:
          type: string
        index:
          type: number
        button_index:
          type: number
        card_index:
          type: number
    ChatTypeEnum:
      type: string
      enum:
        - INDIVIDUAL
        - GROUP
    ChatPriorityValue:
      type: integer
      enum:
        - 0
        - 1
        - 2
        - 3
      description: 0 = baixa, 1 = normal, 2 = alta, 3 = urgente.
    ChatPhoneSummary:
      type: object
      properties:
        id:
          type: string
          format: uuid
        display_number:
          type: string
          nullable: true
    ChatAgentSummary:
      type: object
      properties:
        agent_id:
          type: string
          format: uuid
        user:
          type: object
          allOf:
            - $ref: '#/components/schemas/ExternalUserSummary'
          nullable: true
    ChatMessageSummary:
      type: object
      properties:
        id:
          type: string
          format: uuid
        type:
          $ref: '#/components/schemas/ChatMessageTypeEnum'
        origin:
          $ref: '#/components/schemas/ChatMessageOriginEnum'
        from_me:
          type: boolean
        sent_via:
          $ref: '#/components/schemas/ChatMessageSentViaEnum'
        status:
          $ref: '#/components/schemas/ChatMessageDeliveryStatusEnum'
        body_text:
          type: string
          nullable: true
        content_payload:
          type: object
          nullable: true
          additionalProperties: true
        reactions:
          type: array
          items:
            $ref: '#/components/schemas/ChatReaction'
        meta_timestamp:
          type: string
        is_history:
          type: boolean
        history_phase:
          type: integer
          nullable: true
        history_chunk_order:
          type: integer
          nullable: true
        history_progress:
          type: integer
          nullable: true
        is_media_placeholder:
          type: boolean
        agent:
          type: object
          nullable: true
          properties:
            id:
              type: string
              format: uuid
            user:
              type: object
              allOf:
                - $ref: '#/components/schemas/ExternalUserSummary'
              nullable: true
        user:
          type: object
          allOf:
            - $ref: '#/components/schemas/ExternalUserSummary'
          nullable: true
        crm_marketing_lead_source:
          type: array
          nullable: true
          items:
            type: object
            additionalProperties: true
    ChatCrmPersonSummary:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
    ExternalUserSummary:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
        avatar:
          type: string
          nullable: true
    ChatMessageTypeEnum:
      type: string
      enum:
        - text
        - reaction
        - image
        - video
        - audio
        - document
        - template
        - interactive
        - location
        - call
        - contacts
        - unsupported
    ChatMessageOriginEnum:
      type: string
      enum:
        - INBOUND
        - API_OUTBOUND
        - MOBILE_APP_ECHO
        - SYSTEM
    ChatMessageSentViaEnum:
      type: string
      enum:
        - CONTACT
        - API
        - MOBILE_DEVICE
        - SYSTEM
    ChatMessageDeliveryStatusEnum:
      type: string
      enum:
        - processing
        - sent
        - delivered
        - read
        - failed
        - received
        - deleted
    ChatReaction:
      type: object
      properties:
        id:
          type: string
          format: uuid
        emoji:
          type: string
        actor:
          type: string
          enum:
            - CONTACT
            - BUSINESS
        reactor_wa_id:
          type: string
        reactor_user_id:
          type: string
          format: uuid
          nullable: true
        created_at:
          type: string
          format: date-time
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: >
        Utilize o token de integração no header `Authorization: Bearer {token}`.
        O domínio de origem (Origin/Referer) deve respeitar `allowed_domain`,
        quando configurado na chave.

````