Live Chats

Access to managing live chats, sessions, and channels, as well as retrieving conversations and live chat metrics data, is provided through properties of the LinesCollections type.

Working with live chats

You can retrieve live chats and their parameters by using the Lines.search method. You can retrieve a live chat named some live chat as follows:

const line = await System.lines.lines.search()
    .where((f, g) => g.and ( 
        f.__deletedAt.eq(null), 
        f.__name.eq('some live chat'),  
    )) 
    .first(); 

Working with channels (messengers)

You can retrieve specific channels by using the LineChannels.search method. A channel can be in one of three statuses: Active, Disconnected, Error (see LineChannelState). Example of retrieving all inactive channels:

const inactiveChannels = await  System.lines.channels.search()
    .where((f, g) => g.and ( 
        f.__deletedAt.eq(null), 
        f._state.neq (LineChannelState.active), 
    )) 
    .all(); 

You can then change the channel activity status by using methods of the LineChannelItemRef type. You can disconnect a channel as follows:

const  channel = await System.lines.channels.search()
    .where((f, g) => g.and ( 
        f.__deletedAt.eq(null), 
        f._state.eq (LineChannelState.active), 
    )) 
    .first(); 
await  channel.disconnect(); 

Disconnected channels can be reconnected, but this is only possible for channels of certain types: telegramBot, inbox , viberbot, livechat, instagram (see LineChannelType). Example of reconnecting channels:

// Search for inactive channels of  allowed types 
const channels = await System.lines.channels.search() 
     .where((f, g) => g.and( 
        f.__deletedAt.eq(null), 
         f._state.neq(LineChannelState.active), 
        f._type.in([ 
             LineChannelType.inbox, 
            LineChannelType.telegramBot,  
            LineChannelType.viberbot, 
             LineChannelType.livechat, 
            LineChannelType.instagram, 
        ]) , 
    )) 
    .size(100) 
    .all(); 
for (const channel of channels) {  
    await channel.reconnect(); 
} 

Working with clients

Clients are channel (messenger) users who write to a live chat. To search for a client, you can use the LineClients.search method. For example, you can find a Telegram user by their account:

// Search for a  channel of the telegram type 
const channel = await  System.lines.channels.search() 
    .where((f, g) => g.and ( 
        f.__deletedAt.eq(null), 
        f._type.eq (LineChannelType.telegram), 
    )) 
    .first(); 
// Search for the client  linked to the found channel 
const client = await  System.lines.clients.search() 
    .where((f, g) => g.and ( 
        f.__deletedAt.eq(null), 
        f._channel.eq(channel), 
         f._username.eq('telegram_username'), 
    )) 
    .first(); 

To link an app item to a client, you can use the LineClientItem.save method. To do this, save a reference to the app item in the client's LineClientData._applicationItem field. Note that the mentioned app must be selected for linking to the account in the settings of the line to which the client is linked. Example of such linking:

const  contact = Context.data.contact!;
const client = await  System.lines.clients.search() 
    .where((f, g) => g.and ( 
        f.__deletedAt.eq(null), 
        f._username.eq ('telegram_username'), 
    )) 
    .first(); 
client.data._applicationItem  = contact; 
await client.save(); 

Working with sessions

A session is the main object that contains conversations between clients and live chat operators. A session can be in one of following statuses (see LineSessionState):

  • Queue—the session is waiting to be assigned to an operator, and no one can reply to the client's messages.
  • ** Assigned to operator**—the client is conversing with a specific operator.
  • Closed—the conversation within this session is complete. If the client writes again, a new session will be created. Sessions are searched for and created by using methods of the LineSessions type. Example of searching for an open session with a specific client:
    const client =  await System.lines.clients.search()
      .where((f, g) => g.and ( 
          f.__deletedAt.eq(null), 
          f._username.eq ('telegram_username'), 
      )) 
      .first(); 
    const session = await  System.lines.sessions.search() 
      .where((f,c) => c.and ( 
          f.__deletedAt.eq(null), 
          f._clients.all([client]),  
          f._state.neq(LineSessionState.Closed), 
      )) 
      .first(); 
    
    To create a new session on behalf of an operator, you can use the LineSessions.create method. The method accepts the channel in which the session must be created and the client's account, with whom the conversation must be started. Depending on the channel type, this can be an account (or alias), phone number, email address, and so on.
    const contact = await Context.data.contact.fetch();
    const account =  contact.data._account[0].login; 
    const newSession = await  System.lines.sessions.create(Context.data.channel, account); 
    
    You can then manage the session (assign it to an operator or operator group, close it, retrieve the conversation) by using methods of the [[LineSessionItemRef] ] type. For example, you can assign a session to a specific operator as follows:
    const session = await System.lines.sessions.search()
      .where((f,c) => c.and( 
          f.__deletedAt.eq(null), 
           f.__id.eq(Context.data.sessionId!), 
      )) 
      .first(); 
    await  session.assignToOperator(Context.data.user); 
    
    Example of retrieving the last message in a session:
    const session = await  System.lines.sessions.search()
      .where((f,c) => c.and ( 
          f.__deletedAt.eq(null), 
          f.__id.eq (Context.data.sessionId!), 
      )) 
      .first(); 
    const lastMessage = await  session.getLastMessage(); 
    
    Example of sending a system message to a session:
    const session = await System.lines.sessions.search()
      .where((f,c) => c.and( 
          f.__deletedAt.eq(null), 
           f.__id.eq(Context.data.sessionId!), 
      )) 
      .first(); 
    const message =  session.sendSystemMessage('Priority changed to 'High''); 
    
    You can also mark a session as spam. Using this method is similar to closing a session, but the metrics of such a session will not be included in statistics. It is not possible to mark a session as spam for channels of the following types: internal, support (see LineChannelType).
    const  session = await System.lines.sessions.search()
      .where((f,c) => c.and ( 
          f.__deletedAt.eq(null), 
          f.__id.eq (Context.data.sessionId!), 
      )) 
      .first(); 
    await  session.markAsSpam(); 
    
    The last session in the chain of conversations with a client, or any session of the inbox channel type (see LineChannelType), can be returned from spam to active work. The metrics of such a session will again be included in statistics.
    const session = await System.lines.sessions.search()
      .where ((f,c) => c.and( 
          f.__deletedAt.eq(null), 
          f.__id.eq (Context.data.sessionId!), 
      )) 
      .first(); 
    await  session.unmarkAsSpam(); 
    
    A session can be moved to another live chat: it will be placed in the target live chat's queue, and the assigned operator and operator group will be reset. Moving is available only for channels of the livechat and internal types (see LineChannelType).
    const session = await System.lines.sessions.search()
      .where ((f,c) => c.and( 
          f.__deletedAt.eq(null), 
          f.__id.eq (Context.data.sessionId!), 
      )) 
      .first(); 
    const line = await  System.lines.lines.search() 
      .where((f,c) => f.__name.eq('Accounting'))  
      .first(); 
    await session.changeLine(line); 
    

Type aliases

LineSessionPriorityEnumBase

LineSessionPriorityEnumBase: Low | Medium | High

LineSessionStateEnumBase

LineSessionStateEnumBase: Waiting | AssignedToBot | AssignedToOperator | Closed | MarkedAsSpam

Statuses (states) of a Live Chat session

  • Waiting. The session is in the queue.
  • AssignedToBot. The session is assigned to a bot.
  • AssignedToOperator. The session is assigned to an operator.
  • MarkedAsSpam. The session is marked as spam.
  • Closed. The session is closed.