sendJson method
Sends a given Map (JSON) to Telegram using the configured formatter.
data: The JSON data to be sent.overrideChatId: (Optional) Overrides the defaultchatIdfor this single message.parseMode: (Optional) How the message should be parsed. Defaults toMarkdownV2.
Implementation
Future<void> sendJson({
required Map<String, dynamic> data,
String? overrideChatId,
ParseMode parseMode = ParseMode.MarkdownV2,
}) async {
try {
final messageText = _formatter.format(data, parseMode: parseMode);
await sendMessage(
text: messageText,
overrideChatId: overrideChatId,
parseMode: parseMode,
);
} catch (e) {
print("Error while formatting or sending JSON data: $e");
// If formatting fails, attempt to send raw data
try {
await sendMessage(
text: 'Could not format JSON. Raw data:\n${data.toString()}',
overrideChatId: overrideChatId,
);
} catch (e2) {
print("Failed to send raw data as well: $e2");
}
}
}