In some cases, I want to print out urgent message that need to be checked immediately. You can you Firebase Crashlytics or Sentry, but I use Telegram, because it is free and I can share the urgent message for team.

Setup Telegram Bot

  • Step 1:

    Gen token, keep token Just talk to BotFather and follow a few simple steps. Once you’ve created a bot and received your authorization token, head down to the Bot API manual to see what you can teach your bot to do.

    BotFather

  • Step 2:

    Create channel, public channel and keep chanel name Channel

  • Step 3:

    add chat bot into Channel

    Add Chat Bot

Build Telegram Client

telegram_client.dart

import 'dart:math';

import 'package:http/http.dart' as http;

class TelegramClient {
  final String chatId;
  final String botToken;
  TelegramClient({
    required this.chatId,
    required this.botToken,
  });

  // Text of the message to be sent, 1-4096 characters after entities parsing
  // https://core.telegram.org/bots/api#sendmessage
  String _limitMessage(final String message) =>
      message.substring(0, min(4096, message.length));

  Future<http.Response> sendMessage(final String message) {
    final Uri uri = Uri.https(
      "api.telegram.org",
      "/bot${this.botToken}/sendMessage",
      {
        "chat_id": this.chatId,
        "text": _limitMessage(message),
        "parse_mode": "html",
      },
    );
    return http.get(uri);
  }
}

Flutter App log urgent message

Create telegramClient with:

  • botToken is token from Step 1
  • chatId is channel Id from Step 2
final TelegramClient telegramClient = new TelegramClient(
    chatId: "@flutter_logger",
    botToken: "XXXXXXXXX",
  );

Log urgent message

final String message = "Hello";
final response = await telegramClient.sendMessage(message);
print(response.statusCode);

Demo

Demo

Source code

Note

Cover Photo

Thumbnail Photo