Receiving incoming events
There are two ways to receive incoming API events:
- via webhook — by subscribing to webhooks
- via WebSockets, using the pusher-api protocol
To work with the pusher-api, many ready-made libraries are available in different programming languages. Let’s consider an example of receiving events using the pusher-js library.
1. Include pusher-js
<script src="https://js.pusher.com/7.0/pusher.min.js"></script>
Note: In the connection settings, you only need to specify your accessToken obtained via the v1.tokens.make method (do not change other variables).
let pusher = new Pusher('ChatsAppApiProdKey', {
wsHost: 'socket.chatapp.online',
wssPort: 6001,
disableStats: true,
authEndpoint: 'https://api.chatapp.online/broadcasting/auth',
auth: {
headers: {
'Authorization': '$2y$10$l.SQOBOaqz3ZU65JAlEK4ughTCzIbcVGpJIUNTVnGpaKOYOd4M' // accessToken obtained via v1.tokens.make
}
},
enabledTransports: ['ws'],
forceTLS: true
});
There are 3 types of private channels:
private-v1.licenses.1229.messengers.grWhatsApp— messenger channel in a lineprivate-v1.companies.2224— company channelprivate-v1.users.685— user channel
let channel = pusher.subscribe('private-v1.licenses.1229.messengers.grWhatsApp');
// or
let channel = pusher.subscribe('private-v1.licenses.1229.messengers.telegram');
Note: One messenger in one line corresponds to one channel.
1229 is your line ID.
grWhatsApp is the messenger type in the line.
let channelCompany = pusher.subscribe('private-v1.companies.2224');
Note: One company equals one channel.
2224 is your company ID.
let channelUser = pusher.subscribe('private-v1.users.685');
// incoming/outgoing message
channel.bind('message', (data) => {
console.log(data);
});
// message sending status
channel.bind('messageStatus', (data) => {
console.log(data);
});
// chat tag added/removed
channel.bind('chatTag', (data) => {
console.log(data);
});
// employee status
channelCompany.bind('employeeStatus', (data) => {
console.log(data);
});
// quick response added/updated/deleted
channelUser.bind('quickResponse', (data) => {
console.log(data);
});
- message (incoming/outgoing message)
- messageSystem (system message)
- messageStatus (message delivery status)
- messageDestroy (message deletion)
- messageReaction (reaction to message)
- chatTag (chat tag added/removed)
- chatStatus (chat status set)
- chatResponsible (responsible assigned to chat)
- chatResponsiblePin (responsible pinned/unpinned)
- chatProducer (chat creator assigned)
- chatAccomplice (accomplice added/removed)
- chatAuditor (auditor added/removed)
- chatLevel (chat level set)
- chatConversation (24-hour window opened — WhatsApp only)
- chatRead (chat marked as read)
- chatUnread (chat marked as unread)
- chatProcess (chat process set)
- chatParticipantAdd (participant added to group — Telegram/TelegramBot)
- chatParticipantDestroy (participant removed from group — Telegram/TelegramBot)
- chatComment (chat comment set)
- chatConsentSendMessage (consent to send messages set)
- chatArchive (chat archived/restored)
- chatCustomField (chat custom field updated)
Available events for company channel:
- employeeStatus (employee status)
- employeeWorkday (employee workday)
Available events for user channel:
- quickResponseList (quick response list added/updated/deleted)
- quickResponse (quick response added/updated/deleted)
Full example code:
<script src="https://js.pusher.com/7.0/pusher.min.js"></script>
<script>
window.onload = function() {
let pusher = new Pusher('ChatsAppApiProdKey', {
wsHost: 'socket.chatapp.online',
wssPort: 6001,
disableStats: true,
authEndpoint: 'https://api.chatapp.online/broadcasting/auth',
auth: {
headers: {
'Authorization': '$2y$10$l.SQOBOaqz3ZU65JAlEK4ughTCzIbcVGpJIUNTVnGpaKOYOd4M'
}
},
enabledTransports: ['ws'],
forceTLS: true
});
let channel = pusher.subscribe('private-v1.licenses.1229.messengers.grWhatsApp');
channel.bind('message', (data) => {
console.log(data);
});
channel.bind('messageStatus', (data) => {
console.log(data);
});
channel.bind('chatTag', (data) => {
console.log(data);
});
let channel2 = pusher.subscribe('private-v1.licenses.1229.messengers.telegram');
channel2.bind('message', (data) => {
console.log(data);
});
channel2.bind('messageStatus', (data) => {
console.log(data);
});
channel2.bind('chatTag', (data) => {
console.log(data);
});
let channelCompany = pusher.subscribe('private-v1.companies.2224');
channelCompany.bind('employeeStatus', (data) => {
console.log(data);
});
let channelUser = pusher.subscribe('private-v1.users.685');
channelUser.bind('quickResponseList', (data) => {
console.log(data);
});
channelUser.bind('quickResponse', (data) => {
console.log(data);
});
};
</script>
{
"data": [
{
"id": "123",
"internalId": 37914,
"groupId": null,
"fromApi": false,
"fromMe": false,
"side": "in",
"time": 1637319282,
"type": "text",
"subtype": null,
"message": {
"text": "Test"
}
}
],
"meta": {
"type": "message",
"licenseId": 1229,
"messengerType": "telegram"
}
}
{
"data": [
{
"id": "9f01676a",
"type": "system",
"message": {
"text": "Set responsible Test Testov [685]"
}
}
]
}
{
"data": [
{
"id": "wamid...",
"type": "failed",
"time": 1706175215
}
]
}
{
"data": [
{
"id": "1252",
"time": 1741266878
}
]
}
{
"data": [
{
"id": "wamid...",
"reaction": " ",
"action": "react"
}
]
}
Example object: chatTag
{
"data": [
{
"type": "add",
"tag": {
"id": 29,
"name": "test 1"
}
}
]
}
Example object: chatConversation
{
"data": [
{
"conversation": {
"id": "ccb637d1",
"type": "BIC"
}
}
]
}
{
"data": [
{
"type": "close",
"chat": {
"id": "70000000000@c.us"
}
}
]
}