axiy_bot 0.2.0 copy "axiy_bot: ^0.2.0" to clipboard
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);
}
0
likes
150
points
89
downloads

Documentation

API reference

Publisher

verified publisheraxiy.uz

Weekly Downloads

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.

Homepage

Topics

#bot #chatbot #messenger #axiy

License

MIT (license)

Dependencies

args, cryptography, spacetimedb

More

Packages that depend on axiy_bot