axiy_bot 0.2.0
axiy_bot: ^0.2.0 copied to clipboard
Write bots for the Axiy messenger in plain Dart: receive messages and button presses, send replies with inline and reply keyboards, and run Mini Apps.
example/main.dart
/// The smallest useful Axiy bot: answers /start with two buttons, answers
/// their presses and echoes everything else.
///
/// AXIY_BOT_TOKEN=<token> dart run example/main.dart
///
/// Add `--test` for a bot made in the test build of the app.
library;
import 'dart:io';
import 'package:axiy_bot/axiy_bot.dart';
Future<void> main(List<String> arguments) async {
final token = Platform.environment['AXIY_BOT_TOKEN'];
if (token == null || token.isEmpty) {
stderr.writeln('AXIY_BOT_TOKEN ni bering: bot tokeni.');
exit(64);
}
final bot = await AxiyBot.connect(
token: token,
server: arguments.contains('--test')
? AxiyServer.test
: AxiyServer.production,
);
bot.messages.listen((message) async {
try {
if (parseBotCommand(message.text)?.name == 'start') {
await bot.send(
message.conversationId,
'Salom! Tugmalardan birini bosing.',
buttons: inlineKeyboard([
[
InlineButton.callback('Ha', 'yes'),
InlineButton.callback("Yo'q", 'no'),
],
]),
);
} else {
await bot.reply(message, 'Siz yozdingiz: ${message.text}');
}
} on AxiyBotException catch (error) {
stderr.writeln(error.message);
}
});
bot.callbacks.listen((press) async {
try {
await bot.answer(
press,
text: press.data == 'yes' ? 'Ajoyib!' : 'Mayli, keyinroq.',
);
} on AxiyBotException catch (error) {
stderr.writeln(error.message);
}
});
stdout.writeln('Bot ishlayapti. Chiqish: Ctrl+C');
await ProcessSignal.sigint.watch().first;
await bot.close();
exit(0);
}