Conversation import

The following example showcases the ability to import an existing conversation from the external integration.
From the business logic point, the client already exists and we don’t need an Incoming lead to be created.

Let’s create a contact to better showcase the process.

Connecting the channel to the account

Request example

        
POST https://amojo.amocrm.com/v2/origin/custom/f90ba33d-c9d9-44da-b76c-c349b0ecbe41/connect
Date: Mon, 03 Oct 2020 15:11:21 +0000
Content-Type: application/json
Content-MD5: a5e8ae04332a6d0aac15f01ad05d40e3
X-Signature: 85d94e7f21bdd3971c2721ca22793e45a96d0c75
        
    

Response

        
{
  "account_id": "af9945ff-1490-4cad-807d-945c15d88bec",
  "scope_id": "f90ba33d-c9d9-44da-b76c-c349b0ecbe41_af9945ff-1490-4cad-807d-945c15d88bec",
  "title": "ChatIntegration",
  "hook_api_version": "v2"
}
        
    

Creating a new chat

Request example

        
POST https://amojo.amocrm.com/v2/origin/custom/f90ba33d-c9d9-44da-b76c-c349b0ecbe41_af9945ff-1490-4cad-807d-945c15d88bec/chats
Date: Mon, 03 Oct 2020 15:17:55 +0000
Content-Type: application/json
Content-MD5: 28623103cc158fde408e6ecd1965d437
X-Signature: 7cc462e28eadbb842a2a49edb469f15f24c56676
        
    

Response

        
{
    "conversation_id": "skc-8e3e7640-49af-4448-a2c6-d5a421f7f217",
    "user": {
        "id": "sk-1376265f-86df-4c49-a0c3-a4816df41af9",
        "avatar": "https:/example.com/users/avatar.png",
        "name": "Example Client",
        "profile": {
            "phone": "14151112233",
            "email": "example.client@example.com"
        },
        "profile_link": "https://example.com/profile/example.client"
    }
}
        
    

Creating a contact

Request example

        
POST /api/v4/contacts HTTP/1.1
Host: https://onlinechat.amocrm.com
Content-Type: application/json
Authorization: Bearer xxxx

[
    {
        "first_name": "Jack200803",
        "last_name": "Tester"
    }

]
        
    

Response

        
{
    "_links": {
        "self": {
            "href": "https://onlinechat.amocrm.com/api/v4/contacts"
        }
    },
    "_embedded": {
        "contacts": [
            {
                "id": 3102959,
                "request_id": "0",
                "_links": {
                    "self": {
                        "href": "https://onlinechat.amocrm.com/api/v4/contacts/3102959"
                    }
                }
            }
        ]
    }
}
        
    

Linking the chat to the contact

Request example

        
POST /api/v4/contacts/chats HTTP/1.1
Host: https://onlinechat.amocrm.com


[
	{
		"contact_id": 3102959,
		"chat_id":"6cbab3d5-c4c1-46ff-b710-ad59ad10805f"
	}

]
        
    

Response

        
{
    "_total_items": 1,
    "_embedded": {
        "chats": [
            {
                "chat_id": "6cbab3d5-c4c1-46ff-b710-ad59ad10805f",
                "contact_id": 3102959,
                "id": 26219,
                "request_id": "0"
            }
        ]
    }
}
        
    

Let’s check that the chat is linked properly

The step is not necessary, we just showcase the ability to acquire contacts linked to chats. You can read more about the method here

Request example

        
POST /api/v4/contacts/chats HTTP/1.1
Host: https://onlinechat.amocrm.com


[
	{
		"contact_id": 3102959,
		"chat_id":"6cbab3d5-c4c1-46ff-b710-ad59ad10805f"
	}

]
        
    

Response

        
{
    "_total_items": 1,
    "_embedded": {
        "chats": [
            {
                "chat_id": "6cbab3d5-c4c1-46ff-b710-ad59ad10805f",
                "contact_id": 3102959,
                "id": 26219,
                "request_id": "0"
            }
        ]
    }
}
        
    

Importing the message from the client

Request example

        
POST https://amojo.amocrm.com/v2/origin/custom/f90ba33d-c9d9-44da-b76c-c349b0ecbe41_af9945ff-1490-4cad-807d-945c15d88bec
Date: Mon, 03 Oct 2020 16:06:48 +0000
Content-Type: application/json
Content-MD5: 204dc99bcaa899db72954f16de987022
X-Signature: 6201ab8b5154bfa2af847920f1dfeab74b125489

{
  "account_id": "af9945ff-1490-4cad-807d-945c15d88bec",
  "event_type": "new_message",
  "payload": {
    "timestamp": 1596470808,
    "msgid": "5f283618af2c8",
    "conversation_id": "skc-8e3e7640-49af-4448-a2c6-d5a421f7f217",
    "sender": {
      "id": "sk-1376265f-86df-4c49-a0c3-a4816df41af9",
      "avatar": "https:/example.com/users/avatar.png",
      "name": "Example Client",
      "profile": {
          "phone": "14151112233",
          "email": "example.client@example.com"
      },
      "profile_link": "https://example.com/profile/example.client"
    },
    "message": {
      "type": "text",
      "text": "Can we discuss a discount?"
    },
    "silent": true
  }
}
        
    

Response

        
{
  "new_message": {
    "msgid": "1bf6a765-ec6f-4680-8cd5-6f2d31f78ebc"
  }
}
        
    

Importing a message from the account user

Request example

        
POST https://amojo.amocrm.com/v2/origin/custom/f90ba33d-c9d9-44da-b76c-c349b0ecbe41_af9945ff-1490-4cad-807d-945c15d88bec
Date: Mon, 03 Oct 2020 16:09:12 +0000
Content-Type: application/json
Content-MD5: 671efccd7cedf63b72823faf9a8037ff
X-Signature: 61a129968e586e8a021c2dfcc6be08e896a4fd77

{
  "account_id": "af9945ff-1490-4cad-807d-945c15d88bec",
  "event_type": "new_message",
  "payload": {
    "timestamp": 1596470952,
    "msgid": "skm-5f2836a8ca468",
    "conversation_id": "skc-8e3e7640-49af-4448-a2c6-d5a421f7f217",
    "sender": {
      "id": "sk-e1982bb8-bdf1-4fa5-841d-7d5cebd77f7b",
      "ref_id": "d8d9f9c4-9611-4794-a136-a253a13e1bb5",
      "name": "Sales Manager"
    },
    "receiver": {
      "id": "sk-1376265f-86df-4c49-a0c3-a4816df41af9",
      "avatar": "https:/example.com/users/avatar.png",
      "name": "Example Client",
      "profile": {
          "phone": "14151112233",
          "email": "example.client@example.com"
      },
      "profile_link": "https://example.com/profile/example.client"
    },
    "message": {
      "type": "text",
      "text": "Sure thing. We can discuss special terms if you'd like to purchase atleast for a 1 year."
    },
    "silent": true
  }
}
        
    

Response

        
{
  "new_message": {
    "msgid": "3985523d-78b3-45b7-aeaf-142405bbf1dc"
  }
}

        
    

Getting the communication history

Request example

        
GET https://amojo.amocrm.com/v2/origin/custom/f90ba33d-c9d9-44da-b76c-c349b0ecbe41_af9945ff-1490-4cad-807d-945c15d88bec/chats/6cbab3d5-c4c1-46ff-b710-ad59ad10805f/history?limit=50&offset=0
Date: Mon, 03 Oct 2020 16:23:14 +0000
Content-Type: application/json
Content-MD5: d41d8cd98f00b204e9800998ecf8427e
X-Signature: 5693447e362dcb3bc77abeb27888ff4a0ca53a1c
        
    

Response

        
{
  "messages": [
    {
      "timestamp": 1596470953,
      "sender": {
        "id": "d8d9f9c4-9611-4794-a136-a253a13e1bb5",
        "name": "Sales Manager"
      },
      "receiver": {
        "id": "86a0caef-41ec-49ac-814b-b27da2cea267",
        "client_id": "sk-1376265f-86df-4c49-a0c3-a4816df41af9",
        "avatar": "https:/example.com/users/avatar.png",
        "name": "Example Client",
        "phone": "79151112233",
        "email": "example.client@example.com"
      },
      "message": {
        "id": "3985523d-78b3-45b7-aeaf-142405bbf1dc",
        "client_id": "skm-5f2836a8ca468",
        "type": "text",
        "text": "Sure thing. We can discuss special terms if you'd like to purchase atleast for a 1 year."
        "media": "",
        "thumbnail": "",
        "file_name": "",
        "file_size": 0
      }
    },
    {
      "timestamp": 1596470809,
      "sender": {
        "id": "86a0caef-41ec-49ac-814b-b27da2cea267",
        "client_id": "sk-1376265f-86df-4c49-a0c3-a4816df41af9",
        "avatar": "https:/example.com/users/avatar.png",
        "name": "Example Client",
        "phone": "14151112233",
        "email": "example.client@example.com"
      },
      "message": {
        "id": "1bf6a765-ec6f-4680-8cd5-6f2d31f78ebc",
        "client_id": "5f283618af2c8",
        "type": "text",
        "text": "Can we discuss a discount?"
        "media": "",
        "thumbnail": "",
        "file_name": "",
        "file_size": 0
      }
    }
  ]
}