dart_telegram_bot 1.0.0 copy "dart_telegram_bot: ^1.0.0" to clipboard
dart_telegram_bot: ^1.0.0 copied to clipboard

A Telegram Bot API wrapper made to make fast Telegram bots with as less code as possible.

example/main.dart

import 'dart:io';

import 'package:dart_telegram_bot/dart_telegram_bot.dart';
import 'package:dart_telegram_bot/telegram_entities.dart';

class MyBot extends Bot {
  MyBot(String token) : super(token: token);

  Future onBotCommand(Bot bot, Update update) async {
    var args = BotCommandParser.fromMessage(update.message!)!.args;
    if (args.isEmpty) {
      return bot.sendMessage(
        ChatID(update.message!.chat.id),
        'No arguments provided',
      );
    }

    if (args.length < 2) {
      return bot.sendMessage(
        ChatID(update.message!.chat.id),
        'No enough arguments provided',
      );
    }

    // Do stuff with correct arguments provided
  }

  @override
  Future onReady(Bot bot) async {
    print('Bot ${bot.name} ready');
    onCommand('my_command', onBotCommand);
    await start(clean: true);
  }
}

void main() async {
  var token = Platform.environment['BOT_TOKEN']!;

  /// Either extend Bot and override [onReady] and [onStartFailed]
  MyBot(token);

  /// Or initialize your bot and pass [onReady] and
  /// [onStartFailed] as parameters
  Future onUpdate(Bot bot, Update update) async {
    if (update.message?.text == 'stop') {
      await bot.sendMessage(ChatID(update.message!.chat.id), 'Stopping...');
      bot.stop();
    }
  }

  Future onReady(Bot bot) async {
    print('Bot ${bot.name} ready');
    bot.onUpdate(onUpdate);
    await bot.start(clean: true);
  }

  Bot(
    token: token,
    onReady: onReady,
    onStartFailed: (bot, e, s) => print('Start failed'),
    allowedUpdates: UpdateType.allBut([UpdateType.channelPost]),
  );
}
42
likes
110
pub points
78%
popularity

Publisher

unverified uploader

A Telegram Bot API wrapper made to make fast Telegram bots with as less code as possible.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

http, logging

More

Packages that depend on dart_telegram_bot