...
Live события
| Якорь | ||||
|---|---|---|---|---|
|
Подключение к соккет-серверу
Для работы, используем centrifugeработы с соккет-сервером используется инструмент Centrifuge.
Подробности о доступных SDK для работы с Centrifuge можно получить в официальной документации Centrifuge.
Эндпоинт для подключения: https://twin24.ai/centrifugo/connection/websocket
Пример на js
1. Создаем экземпляр класса Centrifuge, передаем ему URL сервера и создаем подключение:
| Блок кода | ||
|---|---|---|
| ||
const centrifuge = new Centrifuge( |
...
`${protocol}://${window.location.host}/centrifugo/connection/websocket`, |
...
{ |
...
data: { |
...
application: 'notifications', |
...
settings: { token: 'your_auth_token_here' }, |
...
}, |
...
}, |
...
) |
2. Обрабатываем connection, и publication события, в самом publication получаем и реагируем на события
| Блок кода | ||
|---|---|---|
| ||
centrifuge.on('connected', () => { |
...
// some logic when connected |
...
}) |
...
centrifuge.on('publication', (publication) => { |
...
const payload = publication.data |
...
switch (payload.event) { |
...
case 'chatMessageRead': |
...
// handle chatMessageRead event |
...
break |
...
default: |
...
console.warn('Unknown event type:', payload.event) |
...
} |
...
}) |
Список событий
События, которые
...
получает оператор
- chatSessionStarted - Оператору пришла новая сессия чата (при этом не факт, что оператору, может быть просто в помощь)
- operatorNotFound - Сессия не нашла оператора и упала в помощь
- chatSessionAdded - Сессия пришла оператору
- chatMessageCreated - Оператору пришло новое сообщение в чате
- chatMessageEdited - Кто-то из операторов изменил сообщение в чате
- chatMessageRead - Оператору пришло сообщение
...
- , что клиент прочитал сообщение в чате
- chatSessionDeleted - Есть кейсы, когда сессия после отправки определенного сообщения уходит на бота - и тогда пропадает у оператора
- chatSessionSwitchedFromBotToOperator - Сессия была переключена с бота на оператора
- sessionClosedByClient - Клиент закрыл сессию
- operatorBlocked - Оператора
...
- заблокировали на платформе
...
- — требуется выкинуть оператора из кабинета
- chatUserExists - Кто-то зашел в текущего оператора.
...
- Текущий сеанс оператора нужно закрыть
- typing - пользователь печатает сообщение в чате
- operatorLoggedOut - сделать оператора
...
- офлайн
- clientOnline - клиент стал онлайн
- clientOffline - клиент стал
...
- офлайн
| Блок кода | ||||
|---|---|---|---|---|
|
События, что отправляет оператор в центрифугу:
typing - отправляет оператор, когда печатает сообщение в чате
...
chatSessionStarted: { |
...
event: 'chatSessionStarted', |
...
data: { |
...
id: string; |
...
chatId: string; |
...
clientId: string | null; |
...
name: string; |
...
markedAsSpam: boolean; |
...
operatorId: string | number | null; |
...
operatorType?: string; |
...
channel: string; |
...
botStatus?: string | null; |
...
status: string; |
...
startedAt: string; |
...
clientOnlineAt?: string | null; |
...
clientOfflineAt?: string | null; |
...
lastMessageCreatedAt: string | null; |
...
lastMessage?: Unknown | null; |
...
clientExternalId?: string | null; |
...
clientBanned?: boolean | null; |
...
referer?: string; |
...
clientIp?: string | null; |
...
coordinates?: { |
...
city?: string | null; |
...
latitude?: number | null; |
...
zip_code?: string | null; |
...
longitude?: number | null; |
...
time_zone?: string | null; |
...
region_name?: string | null; |
...
country_code?: string | null; |
...
country_name?: string | null; |
...
} | null; |
...
unreadClientMessageCount?: number | null; |
...
unreadClientMessageExists?: boolean | null; |
...
messengerType?: string | null; |
...
whatsappPhone?: string | null; |
...
timerFirstAnswer?: number; |
...
timerNextAnswers?: number; |
...
lastOperatorMessage?: Unknown | null; |
...
firstClientMessageAfterLastOperatorMessage?: Unknown | null; |
...
operatorAssignedAt: string | null; |
...
previousOperator?: { |
...
id?: string | null; |
...
name?: string | null; |
...
} | null; |
...
} |
...
} |
operatorNotFound: {
action: 'operatorNotFound';
data: {
botStatus: string; // например, 'HELP'
chatId: string;
operatorType: string;
sessionId: string;
status: string;
};
}
...
clientOffline: {
action: 'clientOffline',
data: {
id: string;
}
}
События, которые отправляет оператор
- typing – отправляет оператор, когда печатает сообщение в чате