connectToFacebook method
Connect this bot to a facebook channel. You will have to manually store the access token and the facebook user id that you get from the facebook login page using callbackUrl.
-
{callbackUrl} is the url that will be called after the connection is completed, either successfully or not. This url will have the access token and the facebook user id that you will have to save for later use.
-
{autoRedirect} is whether to automatically redirect the page to the facebook login page or not. Default is true.
If {autoRedirect} is true,
-- On desktop and mobile, this will automatically open the facebook login page in the browser.
-- On web, this will redirect the page to the facebook login page.
- {newWindow} is whether to open the facebook login page in a new window or not. Default is false. This is only used when {autoRedirect} is true and the platform is web.
Implementation
Future<String?> connectToFacebook({
required String callbackUrl,
bool autoRedirect = true,
bool newWindow = false,
}) async {
try {
if (_bot.id == null) {
GetIt.I<BotnoiClient>().finishedFailed();
GetIt.I<BotnoiClient>().error.add("[connectToFacebook] : bot '${_bot.botName}' doesn't have an id, try reloading it");
return null;
}
Uri url = Uri.parse("${GetIt.I<BotnoiClient>().endpoint}/developer/platform-api/connect/facebook?callback=$callbackUrl");
http.Response response = await http.post(
url,
headers: _getHeader,
);
if (response.statusCode == 200 || response.statusCode == 204 || response.statusCode == 201) {
GetIt.I<BotnoiClient>().finishedSuccessfully();
dynamic result = jsonDecode(utf8.decode(response.bodyBytes));
if (autoRedirect) {
if (kIsWeb && !newWindow) {
html.window.open(result["redirect_url"], "_self");
} else {
launchUrlString(result["redirect_url"]);
}
}
return result["redirect_url"].toString();
}
GetIt.I<BotnoiClient>().finishedFailed();
GetIt.I<BotnoiClient>().error.add("[connectToFacebook] : ${response.reasonPhrase ?? "ERROR"}");
return null;
} catch (e) {
GetIt.I<BotnoiClient>().finishedFailed();
GetIt.I<BotnoiClient>().error.add("[connectToFacebook] : $e");
return null;
}
}