To manage Live Chats, sessions, and channels or get Live Chat messages and metrics, use the properties of the LinesCollections type.

Working with Live Chats

You can get Live Chats and their parameters using the Lines.search method. For example, here is how you can get a Live chat named Some live chat:

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 get a specific channel using the LineChannels.search method. A channel can be assigned one of the following statuses: Active, Disconnected, or Error (see LineChannelState). For example, you can get all inactive channels in the following way:

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

You can change a channel’s status using LineChannelItemRef type methods. Here is how you can disconnect a channel:

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(); 

You can make a disconnected channel active again. However, this is only available for certain types of channels: telegramBot, inbox, viberbot, livechat, and instagram (see LineChannelType). Here is an example of activating a disconnected channel:

// Search for  disconnected channels of the types that can be activated 
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 users who write to Live Chats using channels (messengers). To search for clients, use the LineClients.search method. For example, you can find a Telegram user by account:

// Search for a `telegram` type channel 
const channel = await  System.lines.channels.search() 
    .where((f, g) => g.and( 
        f. __deletedAt.eq(null), 
        f._type.eq(LineChannelType.telegram), 
    ))  
    .first(); 
// Search for a client linked with the 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 with a client, use the LineClientItem.save method. You need to save a link to the app item in the LineClientData._applicationItem property of the client. Note that the app has to be available for linking with an account in the settings of the client’s Live Chat. Here is an example of linking an account with an app item:

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 containing a conversation between a Live Chat’s client and its operator. A session can be assigned one of the following statuses (see LineSessionState):

  • ** In queue**. The operator has not yet been assigned. No one can respond to the client’s messages.
  • Assigned to a bot. ELMA Bot is responding to the client’s messages.
  • Assigned to an operator. A specific operator is responding to the client’s messages.
  • Closed. The conservation within the session is finished. If the client sends another message, a new session will be created. You can search for sessions and create them using methods provided by the LineSessions type. For example, you can find an active 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 the operator, use the LineSessions.create method. This method accepts the channel that the session needs to be created in and the account of the client. Depending on the channel type, this can be an account (or nickname), a phone number, an email, etc.
    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); 
    
    To manage a session you created (assign it to an operator, a bot, or an operator group, close it, or get messages from it), use methods of the LineSessionItemRef type. For example, you can assign a session to a specific operator in the following way:
    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); 
    
    Here is how you can get 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(); 
    
    You can also mark a session as spam. This is the same as closing a session, except that the metrics of such a session will not be taken into account in reports. It is impossible to mark a session as spam for the internal and support channel 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(); 
    await session. markAsSpam(); 
    
    If a session is the last in a chain of conversations with a customer or if its type is inbox, you can also remove it from spam and resume working on it. The metrics of such a session will be included in the statistics again.
    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(); 
    

Type aliases

LineSessionPriorityEnumBase

LineSessionPriorityEnumBase: Low | Medium | High

LineSessionStateEnumBase

LineSessionStateEnumBase: Waiting | AssignedToBot | AssignedToOperator | Closed | MarkedAsSpam
  • 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.