dart-telegram-bot
所属分类:聊天室
开发工具:Dart
文件大小:0KB
下载次数:0
上传日期:2023-10-19 15:34:28
上 传 者:
sh-1993
说明: Telegram bot API的简单纯包装器,
(A simple pure wrapper for Telegram bot API,)
文件列表:
CHANGELOG.md (4538, 2023-10-19)
LICENSE (1479, 2023-10-19)
analysis_options.yaml (40, 2023-10-19)
example/ (0, 2023-10-19)
example/bot_example.dart (6399, 2023-10-19)
example/main.dart (1599, 2023-10-19)
lib/ (0, 2023-10-19)
lib/dart_telegram_bot.dart (231, 2023-10-19)
lib/src/ (0, 2023-10-19)
lib/src/entities/ (0, 2023-10-19)
lib/src/entities/internal/ (0, 2023-10-19)
lib/src/entities/internal/bot.dart (7274, 2023-10-19)
lib/src/entities/internal/entities/ (0, 2023-10-19)
lib/src/entities/internal/entities/chat_id.dart (438, 2023-10-19)
lib/src/entities/internal/entities/http_file.dart (570, 2023-10-19)
lib/src/entities/internal/exceptions/ (0, 2023-10-19)
lib/src/entities/internal/exceptions/api_exceptions.dart (427, 2023-10-19)
lib/src/entities/internal/exceptions/lib_exceptions.dart (1190, 2023-10-19)
lib/src/entities/internal/helpers/ (0, 2023-10-19)
lib/src/entities/internal/helpers/bot_command_parser.dart (1341, 2023-10-19)
lib/src/entities/internal/helpers/util.dart (166, 2023-10-19)
lib/src/entities/internal/tgapi_client.dart (4579, 2023-10-19)
lib/src/entities/internal/tgapi_methods.dart (72950, 2023-10-19)
lib/src/entities/telegram/ (0, 2023-10-19)
lib/src/entities/telegram/animation.dart (2435, 2023-10-19)
lib/src/entities/telegram/audio.dart (2510, 2023-10-19)
lib/src/entities/telegram/bot_command.dart (1051, 2023-10-19)
lib/src/entities/telegram/bot_command_scope.dart (380, 2023-10-19)
lib/src/entities/telegram/bot_command_scope_all_chat_administrators.dart (559, 2023-10-19)
lib/src/entities/telegram/bot_command_scope_all_group_chats.dart (512, 2023-10-19)
lib/src/entities/telegram/bot_command_scope_all_private_chats.dart (504, 2023-10-19)
lib/src/entities/telegram/bot_command_scope_chat.dart (589, 2023-10-19)
lib/src/entities/telegram/bot_command_scope_chat_administrators.dart (693, 2023-10-19)
lib/src/entities/telegram/bot_command_scope_chat_member.dart (760, 2023-10-19)
lib/src/entities/telegram/bot_command_scope_default.dart (547, 2023-10-19)
... ...
# Dart Telegram Bot [![Dart CI/CD](https://github.com/KaikyuLotus/dart-telegram-bot/actions/workflows/dart-dev.yml/badge.svg)](https://github.com/KaikyuLotus/dart-telegram-bot/actions/workflows/dart-dev.yml)
Dart Telegram Bot is a [Dart](https://dart.dev) wrapper for [Telegram](https://telegram.org/)
bot [API](https://core.telegram.org/bots/api). \
It is compatible with Native, Flutter and JS.
[![Bot API Version](https://img.shields.io/badge/Bot%20API-6.9-blue.svg?style=flat-square)](https://core.telegram.org/bots/api)
[![Dart Version](https://img.shields.io/badge/Dart-2.12.0-blue.svg?style=flat-square)](https://dart.dev)
Using Dart Telegram Bot is straightforward, here's an example echo bot:
```dart
import 'package:dart_telegram_bot/dart_telegram_bot.dart';
import 'package:dart_telegram_bot/telegram_entities.dart';
void main() {
Bot(
// Insert your bot token here
token: 'BOT_TOKEN',
// Once the bot is ready this function will be called
// You can start the bot here
onReady: (bot) => bot.start(clean: true),
// Register a new callback for new updates
).onUpdate((bot, update) async {
// Send a message to the update chat with the received message
bot.sendMessage(ChatID(update.message!.chat.id), update.message!.text!);
});
}
```
Bot start may fail when bot token is invalid or with network issues.\
To handle such cases follow the next example:
```dart
void main() {
Bot(
token: 'BOT_TOKEN',
onReady: (bot) => bot.start(clean: true),
// Handle start failure
onStartFailed: (bot, e, s) => print('Start failed'),
);
}
```
Also, you may want to disable or allow only certain update types.\
To do so follow the next example:
```dart
void main() {
Bot(
token: 'BOT_TOKEN',
onReady: (bot) => bot.start(clean: true),
// Either allow all types but some
allowedUpdates: UpdateType.allBut([UpdateType.channelPost]),
// OR allow only a list of types
allowedUpdates: [UpdateType.message, UpdateType.editedMessage],
);
}
```
Dart Telegram Bot also supports a more OOP approach.
The following example is still an echo bot, this time with OOP approach:
```dart
class MyBot extends Bot {
MyBot() : super(token: 'BOT_TOKEN') {
onUpdate(updateHandler);
}
@override
Future onReady(Bot bot) => bot.start(clean: true);
@override
Future onStartFailed(Bot bot, Object err, StackTrace st) async {
print('Bot failed to start: $err');
}
Future updateHandler(Bot bot, Update update) async {
bot.sendMessage(ChatID(update.message!.chat.id), update.message!.text!);
}
}
```
Written by Kaikyu Lotus ([Telegram](https://t.me/Kaikyu))
近期下载者:
相关文件:
收藏者: