disconnectFromLine method

Future<void> disconnectFromLine()

Disconnect this bot from a connected line channel.

Implementation

Future<void> disconnectFromLine() async {
  try {
    if (_bot.id == null || _bot.accessChannel == null) {
      GetIt.I<BotnoiClient>().finishedFailed();
      GetIt.I<BotnoiClient>().error.add("[connectToLineManual] : bot '${_bot.botName}' doesn't have an id, try reloading it");
      return;
    }
    http.Response? response;
    if (_bot.accessChannel!["line"]["first_token"].toString().isNotEmpty) {
      Uri url = Uri.parse("${GetIt.I<BotnoiClient>().endpoint}/developer/platform-api/connect/line-manual");
      response = await http.put(
        url,
        headers: _getHeader,
        body: jsonEncode({
          "bot_id": _bot.id,
          "first_token": "",
          "second_token": "",
          "active": false,
        }),
      );
    } else if (_bot.accessChannel!["line_modular"]["bot_id"].toString().isNotEmpty) {
      Uri url = Uri.parse("${GetIt.I<BotnoiClient>().endpoint}/developer/platform-api/connect/line-modular/detach?bot_id=${_bot.id}");
      response = await http.delete(
        url,
        headers: _getHeader,
      );
    } else {
      GetIt.I<BotnoiClient>().finishedFailed();
      GetIt.I<BotnoiClient>().error.add("[disconnectFromLine] : bot '${_bot.botName}' doesn't have a connected line channel");
      return;
    }
    if (response.statusCode == 200 || response.statusCode == 204 || response.statusCode == 201) {
      GetIt.I<BotnoiClient>().finishedSuccessfully();
      return;
    }
    GetIt.I<BotnoiClient>().finishedFailed();
    GetIt.I<BotnoiClient>().error.add("[disconnectFromLine] : ${response.reasonPhrase ?? "ERROR"}");
    return;
  } catch (e) {
    GetIt.I<BotnoiClient>().finishedFailed();
    GetIt.I<BotnoiClient>().error.add("[disconnectFromLine] : $e");
    return;
  }
}