sendToTelegram static method
Future<void>
sendToTelegram(
- String botToken,
- String chatId,
- LogModel? logModel,
- LogDispatcherOptions options,
Implementation
static Future<void> sendToTelegram(String botToken, String chatId,
LogModel? logModel, LogDispatcherOptions options) async {
final url = 'https://api.telegram.org/bot$botToken/sendMessage';
final payload = {
'chat_id': chatId,
'text': _createPayload(logModel, options),
'parse_mode': 'MarkdownV2',
};
try {
final response = await HttpClient()
.postUrl(Uri.parse(url))
.then((HttpClientRequest request) {
request.headers.set(HttpHeaders.contentTypeHeader, 'application/json');
request.write(jsonEncode(payload));
return request.close();
});
final responseBody = await response.transform(utf8.decoder).join();
final jsonResponse = jsonDecode(responseBody);
if (jsonResponse['ok'] == true) {
_printSuccess('Message sent to Telegram successfully.');
} else {
_printError(
'Failed to send message to Telegram: ${jsonResponse['description']}');
}
} catch (e) {
_printError('Error sending message to Telegram: $e');
}
}