confirmLogin method
run executable telegram bot api
Implementation
Future<bool> confirmLogin({
http.Client? httpClient,
}) async {
httpClient ??= http_client;
Map<String, String> headers = {
"Content-length": "0",
"Content-Type": "application/x-www-form-urlencoded",
"origin": "https://oauth.telegram.org",
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
};
Uri uri_api = Uri.parse("https://oauth.telegram.org").replace(
pathSegments: [
"auth",
"auth",
],
queryParameters: {
"bot_id": botId,
"origin": botDomain,
"request_access": "write",
"embed": "1",
"confirm": "1",
"allow_write": "1",
},
);
String first_data = await session.get(
url: uri_api.toString(),
headers: headers,
httpClient: httpClient,
);
String searchData = () {
List<String> first_datas = first_data.split("\n").toList();
for (var i = 0; i < first_datas.length; i++) {
String first_data = first_datas[i];
if (RegExp(r"confirm_url", caseSensitive: false).hasMatch(first_data)) {
return first_data;
}
}
return "";
}();
String hash_confirm =
(RegExp("hash=([a-z0-9_]+)").stringMatch(searchData) ?? "")
.replaceAll(RegExp(r"^(hash=)", caseSensitive: false), "")
.trim();
if (hash_confirm.isNotEmpty) {
uri_api = uri_api.replace(
pathSegments: [
"auth",
"auth",
],
queryParameters: {
"bot_id": botId,
"origin": botDomain,
"request_access": "write",
"embed": "1",
"confirm": "1",
"allow_write": "1",
"hash": hash_confirm,
},
);
}
String ans = await session.get(
url: uri_api.toString(),
headers: headers,
httpClient: httpClient,
);
// print(ans);
Map userData = {"@type": "error"};
try {
String id = ans.split('"id":')[1].split(',')[0];
String firstName = ans.split('"first_name":"')[1].split('",')[0];
String username = ans.split('"username":"')[1].split('",')[0];
String hash = ans.split('"hash":"')[1].split('"')[0];
userData["id"] = id;
userData["first_name"] = firstName;
userData["username"] = username;
userData["hash"] = hash;
userData["@type"] = "userTelegramLoginWidget";
return true;
} catch (e, stack) {
userData["@type"] = "error";
}
return ans == 'true';
}