> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# NSChatAgent

> Chat functions. Manage chat channels, sessions, and messages.

Chat functions. Manage chat channels, sessions, and messages.

## Constructors

### NSChatAgent()

```crmscript theme={null}
NSChatAgent
```

Chat functions. Manage chat channels, sessions, and messages.

## Methods

## AcceptChatSessionTransfer(Integer)

Accept the transfer: assign the session to the user. If the session is not being transferred, nothing happens.

```crmscript theme={null}
NSChatSessionEntity AcceptChatSessionTransfer(Integer chatSessionId)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
NSChatSessionEntity res = agent.AcceptChatSessionTransfer(chatSessionId);
```

## AddChatMessage(Integer,NSChatMessage)

Adds a new message to a chat session

```crmscript theme={null}
NSChatMessage AddChatMessage(Integer chatSessionId, NSChatMessage message)
```

**Returns:** [CRMScript.NetServer.NSChatMessage](CRMScript.NetServer.NSChatMessage)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
NSChatMessage message;
NSChatMessage res = agent.AddChatMessage(chatSessionId, message);
```

## AddChatTopicUserAgent(Integer,NSChatTopicAgent)

Adds a user to a chat topic

```crmscript theme={null}
NSChatTopicAgent AddChatTopicUserAgent(Integer chatTopicId, NSChatTopicAgent useragent)
```

**Returns:** [CRMScript.NetServer.NSChatTopicAgent](CRMScript.NetServer.NSChatTopicAgent)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
NSChatTopicAgent useragent;
NSChatTopicAgent res = agent.AddChatTopicUserAgent(chatTopicId, useragent);
```

## ChatSessionsForUser()

Gets all chat sessions which this user is a member of.

```crmscript theme={null}
NSChatSessionEntity[] ChatSessionsForUser()
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)\[]

<Note>
  Members means that you have at least one of: Can Respond, Notifications, Listen or Manager
</Note>

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatSessionEntity[] res = agent.ChatSessionsForUser();
```

## ChatTopicsForUser()

Gets all chat topics which this user is a member of.nd, Notifications, Listen or Manager.

```crmscript theme={null}
NSChatTopicEntity[] ChatTopicsForUser()
```

**Returns:** [CRMScript.NetServer.NSChatTopicEntity](CRMScript.NetServer.NSChatTopicEntity)\[]

<Note>
  Members means that you have at least one of: Can Respond, Notifications, Listen or Manager
</Note>

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatTopicEntity[] res = agent.ChatTopicsForUser();
```

## CreateChatSessionForTopic(Integer)

Creates a new session on a chat topic

```crmscript theme={null}
NSChatSessionEntity CreateChatSessionForTopic(Integer chatTopicId)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
NSChatSessionEntity res = agent.CreateChatSessionForTopic(chatTopicId);
```

## CreateDefaultChatSessionEntity()

Sets default values into a new NSChatSessionEntity.

```crmscript theme={null}
NSChatSessionEntity CreateDefaultChatSessionEntity()
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

<Note>
  NetServer calculates default values (for example Country) on the entity, which is required when creating/storing a new instance
</Note>

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatSessionEntity thing = agent.CreateDefaultChatSessionEntity();
thing = agent.SaveChatSessionEntity(thing);
```

## CreateDefaultChatTopicAgent()

Sets default values into a new NSChatTopicAgent.

```crmscript theme={null}
NSChatTopicAgent CreateDefaultChatTopicAgent()
```

**Returns:** [CRMScript.NetServer.NSChatTopicAgent](CRMScript.NetServer.NSChatTopicAgent)

<Note>
  NetServer calculates default values (for example Country) on the entity, which is required when creating/storing a new instance
</Note>

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatTopicAgent thing = agent.CreateDefaultChatTopicAgent();
thing = agent.SaveChatTopicAgent(thing);
```

## CreateDefaultChatTopicEntity()

Sets default values into a new NSChatTopicEntity.

```crmscript theme={null}
NSChatTopicEntity CreateDefaultChatTopicEntity()
```

**Returns:** [CRMScript.NetServer.NSChatTopicEntity](CRMScript.NetServer.NSChatTopicEntity)

<Note>
  NetServer calculates default values (for example Country) on the entity, which is required when creating/storing a new instance
</Note>

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatTopicEntity thing = agent.CreateDefaultChatTopicEntity();
thing = agent.SaveChatTopicEntity(thing);
```

## DeleteChatSessionEntity(Integer)

Deletes the NSChatSessionEntity

```crmscript theme={null}
Void DeleteChatSessionEntity(Integer chatSessionEntity)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
agent.DeleteChatSessionEntity(123);
```

## DeleteChatTopicEntity(Integer)

Deletes the NSChatTopicEntity

```crmscript theme={null}
Void DeleteChatTopicEntity(Integer chatTopicEntity)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
agent.DeleteChatTopicEntity(123);
```

## DeleteChatTopicUserAgent(Integer,String)

Removes a user from a topic

```crmscript theme={null}
Void DeleteChatTopicUserAgent(Integer chatTopicId, String username)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
String username;
agent.DeleteChatTopicUserAgent(chatTopicId, username);
```

## GetChatMessages(Integer,Integer)

Gets all or some of the messages in a chat session

```crmscript theme={null}
NSChatMessage[] GetChatMessages(Integer chatSessionId, Integer after)
```

**Returns:** [CRMScript.NetServer.NSChatMessage](CRMScript.NetServer.NSChatMessage)\[]

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
Integer after;
NSChatMessage[] res = agent.GetChatMessages(chatSessionId, after);
```

## GetChatPresence()

Gets the chat presence status for all chat users.

```crmscript theme={null}
NSChatPresence[] GetChatPresence()
```

**Returns:** [CRMScript.NetServer.NSChatPresence](CRMScript.NetServer.NSChatPresence)\[]

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatPresence[] res = agent.GetChatPresence();
```

## GetChatSessionEntity(Integer)

Gets an NSChatSessionEntity object.

```crmscript theme={null}
NSChatSessionEntity GetChatSessionEntity(Integer chatSessionEntityId)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatSessionEntity thing = agent.GetChatSessionEntity(123);
```

## GetChatTopicEntity(Integer)

Gets an NSChatTopicEntity object.

```crmscript theme={null}
NSChatTopicEntity GetChatTopicEntity(Integer chatTopicEntityId)
```

**Returns:** [CRMScript.NetServer.NSChatTopicEntity](CRMScript.NetServer.NSChatTopicEntity)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatTopicEntity thing = agent.GetChatTopicEntity(123);
```

## GetChatTopicUserAgent(Integer,String)

Gets a user assigned to a topic

```crmscript theme={null}
NSChatTopicAgent GetChatTopicUserAgent(Integer chatTopicId, String username)
```

**Returns:** [CRMScript.NetServer.NSChatTopicAgent](CRMScript.NetServer.NSChatTopicAgent)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
String username;
NSChatTopicAgent res = agent.GetChatTopicUserAgent(chatTopicId, username);
```

## GetChatTopicUserAgentList(Integer)

Gets a list of users assigned to a chat topic.

```crmscript theme={null}
NSChatTopicAgent[] GetChatTopicUserAgentList(Integer chatTopicId)
```

**Returns:** [CRMScript.NetServer.NSChatTopicAgent](CRMScript.NetServer.NSChatTopicAgent)\[]

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
NSChatTopicAgent[] res = agent.GetChatTopicUserAgentList(chatTopicId);
```

## GetChatTranscript(Integer,Bool)

Gets the chat transcript, formatted as plain text or HTML.

```crmscript theme={null}
String GetChatTranscript(Integer chatSessionId, Bool html)
```

**Returns:** [CRMScript.Global.String](CRMScript.Global.String) - chat transcript, formatted as plain text or HTMl.

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
Bool html;
String res = agent.GetChatTranscript(chatSessionId, html);
```

## GetUserAgentList(Bool,Bool)

Gets list of users that can be assigned to chat topics.

```crmscript theme={null}
NSMDOListItem[] GetUserAgentList(Bool flat, Bool onlyPresent)
```

**Returns:** [CRMScript.NetServer.NSMDOListItem](CRMScript.NetServer.NSMDOListItem)\[]

<Note>
  Users with Chat-CALs from the MDO list 'chatuser'
</Note>

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Bool flat;
Bool onlyPresent;
NSMDOListItem[] res = agent.GetUserAgentList(flat, onlyPresent);
```

## IsWithinOpeningHours(Integer)

Checks if we are right now within the opening hours of the given topic. Will use timezones to calculate if configured.

```crmscript theme={null}
Bool IsWithinOpeningHours(Integer chatTopicId)
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.Bool)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
Bool res = agent.IsWithinOpeningHours(chatTopicId);
```

## PickUpChatSession(Integer)

Answer the session: assign the session to the user. The welcome message is sent to the customer.

```crmscript theme={null}
NSChatSessionEntity PickUpChatSession(Integer chatSessionId)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity) - Returns NULL if session was not in queue.

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
NSChatSessionEntity res = agent.PickUpChatSession(chatSessionId);
```

## PickUpFirstChatSession()

Answer the first available session from the queue: assign the session to the user. The welcome message is sent to the customer.

```crmscript theme={null}
NSChatSessionEntity PickUpFirstChatSession()
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatSessionEntity res = agent.PickUpFirstChatSession();
```

## RejectChatSessionTransfer(Integer)

Do not want. Deny the transfer to the user. If the session is not being transferred, nothing happens.

```crmscript theme={null}
NSChatSessionEntity RejectChatSessionTransfer(Integer chatSessionId)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
NSChatSessionEntity res = agent.RejectChatSessionTransfer(chatSessionId);
```

## ResetChatSession(Integer)

Resets chat session, puts it back on the queue for pickup.

```crmscript theme={null}
NSChatSessionEntity ResetChatSession(Integer chatSessionId)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

<Note>
  Used by bots to hand off a session to human
</Note>

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
NSChatSessionEntity res = agent.ResetChatSession(chatSessionId);
```

## SaveChatPresence(NSChatPresence\[])

Saves the chat presence for specified users

```crmscript theme={null}
Void SaveChatPresence(NSChatPresence[] chatPresence)
```

**Returns:** [CRMScript.Global.Void](CRMScript.Global.Void)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
NSChatPresence[] chatPresence;
agent.SaveChatPresence(chatPresence);
```

## SaveChatSessionEntity(NSChatSessionEntity)

Updates the existing NSChatSessionEntity or creates a new NSChatSessionEntity if the id parameter is 0

```crmscript theme={null}
NSChatSessionEntity SaveChatSessionEntity(NSChatSessionEntity chatSessionEntity)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

**Example:**

```crmscript theme={null}
NSChat  agent;
NSChatSessionEntity thing = agent.CreateDefaultChatSessionEntity();
thing = agent.SaveChatSessionEntity(thing);
```

## SaveChatTopicEntity(NSChatTopicEntity)

Updates the existing NSChatTopicEntity or creates a new NSChatTopicEntity if the id parameter is 0

```crmscript theme={null}
NSChatTopicEntity SaveChatTopicEntity(NSChatTopicEntity chatTopicEntity)
```

**Returns:** [CRMScript.NetServer.NSChatTopicEntity](CRMScript.NetServer.NSChatTopicEntity)

**Example:**

```crmscript theme={null}
NSChat  agent;
NSChatTopicEntity thing = agent.CreateDefaultChatTopicEntity();
thing = agent.SaveChatTopicEntity(thing);
```

## TransferChatSession(Integer,String)

Requests to send the session to another user. If the session does not belong to the user, nothing happens.

```crmscript theme={null}
NSChatSessionEntity TransferChatSession(Integer chatSessionId, String toAssociate)
```

**Returns:** [CRMScript.NetServer.NSChatSessionEntity](CRMScript.NetServer.NSChatSessionEntity)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatSessionId;
String toAssociate;
NSChatSessionEntity res = agent.TransferChatSession(chatSessionId, toAssociate);
```

## UpdateChatTopicUserAgent(Integer,String,NSChatTopicAgent)

Updates a user's role in a chat topic

```crmscript theme={null}
NSChatTopicAgent UpdateChatTopicUserAgent(Integer chatTopicId, String username, NSChatTopicAgent useragent)
```

**Returns:** [CRMScript.NetServer.NSChatTopicAgent](CRMScript.NetServer.NSChatTopicAgent)

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
String username;
NSChatTopicAgent useragent;
NSChatTopicAgent res = agent.UpdateChatTopicUserAgent(chatTopicId, username, useragent);
```

## UpdateChatTopicUserAgent(Integer,NSChatTopicAgent\[])

Updates users roles in a chat topic

```crmscript theme={null}
NSChatTopicAgent[] UpdateChatTopicUserAgents(Integer chatTopicId, NSChatTopicAgent[] useragents)
```

**Returns:** [CRMScript.NetServer.NSChatTopicAgent](CRMScript.NetServer.NSChatTopicAgent)\[]

**Example:**

```crmscript theme={null}
NSChatAgent agent;
Integer chatTopicId;
NSChatTopicAgent[] useragents;
NSChatTopicAgent[] res = agent.UpdateChatTopicUserAgents(chatTopicId, useragents);
```


## Related topics

- [Namespace CRMScript.NetServer](/en/automation/crmscript/reference/CRMScript.NetServer.md)
- [Transfer a chat to another agent](/en/chat/learn/transfer-to-agent.md)
- [NSChatTopicAgent](/en/automation/crmscript/reference/CRMScript.NetServer.NSChatTopicAgent.md)
- [Delete Chat Topic User Agent](/en/api/reference/restful/agent/chat_agent/delete-chat-topic-user-agent.md)
- [Create Default Chat Topic Agent](/en/api/reference/restful/agent/chat_agent/create-default-chat-topic-agent.md)
- [Update Chat Topic User Agent](/en/api/reference/restful/agent/chat_agent/update-chat-topic-user-agent.md)
