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

# How To Use

> Here you will find how to use SmartChat Assistant in your aplication

Since SmartChat Assistant is connected to a QuickBlox user, you can interact with it just like you would with a regular user. You have the option to create **private** or **group** dialogs with the assistant.

<Note>
  SmartChat Assistant is not available in the Public Dialogs
</Note>

## Create a private dialog with SmartChat Assistant

The easiest way to interact with the SmartChat Assistant is to create a **private**(1-to-1) dialog and the assistant will answer to any your message.

### iOS

To create a **private**(1-to-1) dialog, you need to set the dialog **type** field to `private` and ID of the opponent you want to create a chat with.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let assistantId = 34
    let dialog = QBChatDialog(dialogID: nil, type: .private)
    dialog.occupantIDs = [assistantId]
    QBRequest.createDialog(dialog, successBlock: { (response, createdDialog) in

    }, errorBlock: { (response) in

    })
    ```
  </Tab>

  <Tab title="Objective-C">
    ```Objective-C theme={null}
    NSNumber *assistantId = @(34);
    QBChatDialog *dialog = [[QBChatDialog alloc] initWithDialogID:nil type:QBChatDialogTypePrivate];
    dialog.occupantIDs = @[assistantId];

    [QBRequest createDialog:dialog successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nonnull createdDialog) {

    } errorBlock:^(QBResponse * _Nonnull response) {

    }];
    ```
  </Tab>
</Tabs>

### Android

To create a **private**(1-to-1) dialog, you need to set the dialog **type** to `QBDialogType.PRIVATE` and ID of the opponent you want to create a chat with.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    int assistantId = 34;
    ArrayList<Integer> occupantIdsList = new ArrayList<Integer>();
    occupantIdsList.add(assistantId);

    QBChatDialog dialog = new QBChatDialog();
    dialog.setType(QBDialogType.PRIVATE);
    dialog.setOccupantsIds(occupantIdsList);

    QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() {
        @Override
        public void onSuccess(QBChatDialog result, Bundle bundle) {

        }

        @Override
        public void onError(QBResponseException exception) {

        }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val assistantId = 34
    val occupantIdsList = ArrayList<Int>()
    occupantIdsList.add(assistantId)

    val dialog = QBChatDialog()
    dialog.type = QBDialogType.PRIVATE
    dialog.setOccupantsIds(occupantIdsList)

    // or just use DialogUtils
    // QBChatDialog dialog = DialogUtils.buildPrivateDialog(recipientId);

    QBRestChatService.createChatDialog(dialog).performAsync(object : QBEntityCallback<QBChatDialog> {
        override fun onSuccess(result: QBChatDialog?, bundle: Bundle?) {

        }

        override fun onError(exception: QBResponseException?) {

        }
    })
    ```
  </Tab>
</Tabs>

### JavaScript

To create a **private**(1-to-1) dialog, you need to set the dialog **type** to `3` and ID of the opponent you want to create a chat with.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    const assistantId = 34
    const params = {
      type: 3,
      occupants_ids: [assistantId]
    };

    QB.chat.dialog.create(params, function(error, dialog) {});
    ```
  </Tab>
</Tabs>

### ReactNative

To create a **private**(1-to-1) dialog, you need to set the **type** to `QB.chat.DIALOG_TYPE.CHAT` and ID of the opponent you want to create a chat with.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    const assistantId = 34
    const params = {
      type: QB.chat.DIALOG_TYPE.CHAT,
      occupantsIds: [assistantId]
    };

    QB.chat
      .createDialog(params)
      .then(function (dialog) {

      })
      .catch(function (e) {

      });
    ```
  </Tab>
</Tabs>

### Flutter

To create a **private**(1-to-1) dialog, you need to set the **type** to `QBChatDialogTypes.CHAT` and ID of the opponent you want to create a chat with.

<Tabs>
  <Tab title="Dart">
    ```Dart theme={null}
    int assistantId = 34;
    List<int> occupantsIds = [assistantId];
    int dialogType = QBChatDialogTypes.CHAT;

    try {
      QBDialog? createdDialog = await QB.chat.createDialog(occupantsIds, null, dialogType: dialogType);
    } on PlatformException catch (e) {

    }
    ```
  </Tab>
</Tabs>

## Create a group dialog with SmartChat Assistant

Also, your users can interact with the SmartChat Assistant in **group** dialogs.

### iOS

To create a **group** dialog for a predefined number of occupants, you need to set the dialog **type** field to `group` and IDs of opponents you want to create a chat with.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let assistantId = 34
    let chatDialog = QBChatDialog(dialogID: nil, type: .group)
    chatDialog.name = "Dialog with assistant"
    chatDialog.occupantIDs = [assistantId, 45, 55]

    QBRequest.createDialog(chatDialog, successBlock: { (response, dialog) in
        dialog.join(completionBlock: { (error) in
        })
    }, errorBlock: { (response) in

    })
    ```
  </Tab>

  <Tab title="Objective-C">
    ```C theme={null}
    NSNumber *assistantId = @(34);
    QBChatDialog *chatDialog = [[QBChatDialog alloc] initWithDialogID:nil type:QBChatDialogTypeGroup];
    chatDialog.name = @"Dialog with assistant";
    chatDialog.occupantIDs = @[assistantId, @45, @55];

    [QBRequest createDialog:chatDialog successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nonnull dialog) {
        [dialog joinWithCompletionBlock:^(NSError * _Nullable error) {

        }];
    } errorBlock:^(QBResponse * _Nonnull response) {

    }];
    ```
  </Tab>
</Tabs>

### Android

To create a **group** dialog for a predefined number of occupants, you need to set the dialog **type** to `QBDialogType.GROUP` and IDs of opponents you want to create a chat with.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    int assistantId = 34;
    ArrayList<Integer> occupantIdsList = new ArrayList<Integer>();
    occupantIdsList.add(assistantId);
    occupantIdsList.add(45);
    occupantIdsList.add(55);

    QBChatDialog dialog = new QBChatDialog();
    dialog.setName("Dialog with assistant");
    dialog.setType(QBDialogType.GROUP);
    dialog.setOccupantsIds(occupantIdsList);

    QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() {
        @Override
        public void onSuccess(QBChatDialog result, Bundle bundle) {

        }

        @Override
        public void onError(QBResponseException exception) {

        }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val assistantId = 34
    val occupantIdsList = ArrayList<Int>()
    occupantIdsList.add(assistantId)
    occupantIdsList.add(45)
    occupantIdsList.add(55)

    val dialog = QBChatDialog()
    dialog.name = "Dialog with assistant"
    dialog.type = QBDialogType.GROUP
    dialog.setOccupantsIds(occupantIdsList)

    QBRestChatService.createChatDialog(dialog).performAsync(object : QBEntityCallback<QBChatDialog> {
        override fun onSuccess(result: QBChatDialog?, bundle: Bundle?) {

        }

        override fun onError(exception: QBResponseException?) {

        }
    })
    ```
  </Tab>
</Tabs>

### JavaScript

To create a **group** dialog for a predefined number of occupants, you need to set the dialog **type** to `2` and IDs of opponents, you want to create a chat with.

<Tabs>
  <Tab title="JavaScript">
    ```JavaScript theme={null}
    const assistantId = 34;
    const params = {
      type: 2,
      occupants_ids: [assistantId, 45, 55],
      name: "Dialog with assistant"
    };

    QB.chat.dialog.create(params, function(error, dialog) {});
    ```
  </Tab>
</Tabs>

### ReactNative

To create a **group** dialog for a predefined number of occupants, you need to set the dialog **type** to `QB.chat.DIALOG_TYPE.GROUP_CHAT` and IDs of opponents you want to create a chat with.

<Tabs>
  <Tab title="JavaScript">
    ```JavaScript theme={null}
    const assistantId = 34;
    const params = {
      type: QB.chat.DIALOG_TYPE.GROUP_CHAT,
      name: 'Dialog with assistant',
      occupantsIds: [assistantId, 45, 55]
    };

    QB.chat
      .createDialog(params)
      .then(function (dialog) {

      })
      .catch(function (e) {

      });
    ```
  </Tab>
</Tabs>

### Flutter

To create a **group** dialog for a predefined number of occupants, you need to set the dialog **type** to `QBChatDialogTypes.GROUP_CHAT` and IDs of opponents you want to create a chat with.

<Tabs>
  <Tab title="Dart">
    ```Dart theme={null}
    `int assistantId = 34;
    List<int> occupantIds = [assistantId, 45, 55];
    String dialogName = "Dialog with assistant";
    int dialogType = QBChatDialogTypes.GROUP_CHAT;

    try {
      QBDialog? createdDialog = await QB.chat.createDialog(occupantIds, dialogName, dialogType: dialogType);
    } on PlatformException catch (e) {

    }
    ```
  </Tab>
</Tabs>

## Send message to assistant in group dialog with command

To be able to interact with an assistant in a **group** dialog next conditions should be met:

* **Enable** SmartChat Assistant for the **group** dialogs on the [dasboard](smartchat-assistant/smartchat-assistant-overview#create-smartchat-assistant).
* Define the **command** for the assistant on the [dashboard](smartchat-assistant/smartchat-assistant-overview#create-smartchat-assistant).
* SmartChat Assistant **must** be added to the **group** dialog and be present in the `occupant_ids`.
* Your message **must** begin with a **command**.

For example, you defined your SmartChat Assistant **command** as "SuperHelpfulAssistant".

In this case, your message may look like:

`/SuperHelpfulAssistant Help me with something`

## Send message to assistant in a group dialog with mentions

To be able to interact with an assistant in a **group** dialog next conditions should be met:

* **Enable** SmartChat Assistant for the **group** dialogs on the [dasboard](smartchat-assistant/smartchat-assistant-overview#create-smartchat-assistant).
* SmartChat Assistant **must** be added to the **group** dialog and be present in the `occupant_ids`.
* The message **must** contain the assistant's user ID in the `mentioned_user_ids` custom field.

<Note>
  If you want mention several users you must enumerate them joined by comma(,).

  Example:

  "34,45,55"
</Note>

### iOS

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let message = QBChatMessage()
    message.text = "Hi"
    message.customParameters["save_to_history"] = true
    message.customParameters["mentioned_user_ids"] = "34"

    let dialog = ...
    dialog.send(message) { (error) in

    }
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objective-c theme={null}
    QBChatMessage *message = [[QBChatMessage alloc] init];
    message.text = @"Hi";
    message.customParameters[@"save_to_history"] = @"1";
    message.customParameters[@"mentioned_user_ids"] = @"34";

    QBChatDialog *dialog = ...;
    [dialog sendMessage:message completionBlock:^(NSError * _Nullable error) {

    }];
    ```
  </Tab>
</Tabs>

### Android

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    QBChatMessage chatMessage = new QBChatMessage();
    chatMessage.setSaveToHistory(true);
    chatMessage.setBody("Hi");
    chatMessage.setProperty("mentioned_user_ids", "34");

    qbChatDialog.sendMessage(chatMessage, new QBEntityCallback<Void>() {
        @Override
        public void onSuccess(Void void, Bundle bundle) {

        }

        @Override
        public void onError(QBResponseException exception) {

        }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val chatMessage = QBChatMessage()
    chatMessage.setSaveToHistory(true)
    chatMessage.body = "Hi"
    chatMessage.setProperty("mentioned_user_ids", "34")

    qbChatDialog.sendMessage(chatMessage, object : QBEntityCallback<Void> {
        override fun onSuccess(aVoid: Void?, bundle: Bundle?) {

        }

        override fun onError(exception: QBResponseException?) {

        }
    })
    ```
  </Tab>
</Tabs>

### JavaScript

<Tabs>
  <Tab title="JavaScript">
    ```JavaScript theme={null}
    var message = {
      type: "groupchat",
      body: "Hi",
      extension: {
        save_to_history: 1,
        mentioned_user_ids: "34"
      },
    };

    try {
      QB.chat.send(jidOrUserId, message);
    } catch (e) {
      if (e.name === "ChatNotConnectedError") {

      }
    }
    ```
  </Tab>
</Tabs>

### ReactNative

<Tabs>
  <Tab title="JavaScript">
    ```JavaScript theme={null}
    const message = {
      dialogId: 'dsfsd934329hjhkda98793j2',
      body: 'H1',
      properties: {
        mentioned_user_ids: "34"
      },
      saveToHistory: true
    };

    QB.chat
      .sendMessage(message)
      .then(function () { })
      .catch(function (e) { })
    ```
  </Tab>
</Tabs>

### Flutter

<Tabs>
  <Tab title="Dart">
    ```Dart theme={null}
    String dialogId = "dsfs9344349hjkdsda9877932j2";
    String body: 'Hi';

    bool saveToHistory = true;

    Map<String, String> properties = Map();
    properties["mentioned_user_ids"] = "34";

    try {
      await QB.chat.sendMessage(dialogId, body: body, saveToHistory: saveToHistory, properties: properties);
    } on PlatformException catch (e) {

    }
    ```
  </Tab>
</Tabs>
