bind method

Future<NIMResult<V2NIMUserAIBot>> bind(
  1. String qrCode,
  2. V2NIMUserAIBot bot
)

Implementation

Future<NIMResult<V2NIMUserAIBot>> bind(
  String qrCode,
  V2NIMUserAIBot bot,
) async {
  final accid = bot.accid;
  if (accid == null || accid.isEmpty) {
    return NIMResult.failure(message: 'no selected bot');
  }
  var selected = bot;
  var token = selected.token;
  if (token == null || token.isEmpty) {
    final detail = await NimCore.instance.aiService.getUserAIBot(
      V2NIMGetUserAIBotParams(accid: accid),
    );
    if (!detail.isSuccess || detail.data == null) {
      return NIMResult.failure(
        code: detail.code,
        message: detail.errorDetails,
      );
    }
    token = detail.data!.token;
    if (token?.isEmpty ?? true) {
      return NIMResult.failure(message: 'token unavailable');
    }
    final index = bots.indexWhere((item) => item.accid == selected.accid);
    if (index >= 0) {
      bots[index] = detail.data!;
    }
    selected = detail.data!;
  }
  final bindResult = await NimCore.instance.aiService.bindUserAIBotToQrCode(
    V2NIMBindUserAIBotToQrCodeParams(
      accid: accid,
      token: token,
      qrCode: qrCode,
    ),
  );
  if (!bindResult.isSuccess) {
    return NIMResult.failure(
      code: bindResult.code,
      message: bindResult.errorDetails,
    );
  }
  final latest = await NimCore.instance.aiService.getUserAIBot(
    V2NIMGetUserAIBotParams(accid: accid),
  );
  if (latest.isSuccess && latest.data != null) {
    return latest;
  }
  return NIMResult.success(data: selected);
}