dartgram 0.5.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

DartGram #

Intuitive Telegram API handler in Dart with minimal dependencies.

It is async-oriented, meaning every update is handled independently of the others.

It is designed to follow a set of rules defined by the user that are examined for every update. A simple syntax allows the user to specify various filters that each have to match a given message for its corresponding rule to be executed.

The entities laid out are designed to closely follow the types as specified by the Telegram docs. Several methods are then added to render the code more fluent and intuitive to write (for example a message can be reply with .reply(text) or deleted with .delete(), a user can be kicked with .kick(), etc.)

Usage #

A simple usage example:

import 'package:dartgram/dartgram.dart';

main() async {
  var bot = Bot('12345:token');

  bot.onCommand('ping')
  .then((m) async {
    await m.reply('pong');
  });

  var isReply = (Message m) => m.reply_to_message != null;
  var isAdmin = (Message m) => m.from.id == 12345678;

  bot.onCommand('info')
  .when(isReply)
  .and(isAdmin)
  .then((m) async {
    await m.reply(m.reply_to_message.raw.toString());
  });

  await bot.start();
}

You can also invoke any Telegram API endpoint manually by specifying the parameters:

  var message = await bot.request<Message>('sendMessage', {
    'chat_id': 178578488,
    'text': 'hi'
  });

The object returned will be casted directly from the response returned by Telegram into the specified type, with all its available intuitive methods (reply, delete, etc)

Features and bugs #

This is a work-in-progress and I intend on adding new features as I need them for my personal bots. If you desperately need a feature, you can request it.

0.0.1 #

  • Initial version, created by Stagehand

0.1.0 #

  • Good chunk of the logic coded, actual features will come shortly

0.5.0 #

  • Added enough features to make a somewhat decent bot with it

example/example.dart

import 'package:dartgram/dartgram.dart';

void main(List<String> args) async {
  var bot = Bot('12345:token');

  // Standalone request (executes immediately)
  // In this example the response (if successful) is casted into a Message object

  var message = await bot.request<Message>('sendMessage', {
    'chat_id': 12345678,
    'text': 'hello world'
  });

  // The returned object contains all the goody functions:
  await message.edit('Hello, world!');

  // Setting up rules (executes when the polling loop starts)

  // Simple command
  bot.onCommand('ping')
  .then((m) async {
    await m.reply('pong');
  });

  // Custom filters (can be stored and reused, etc)
  var isReply = (Message m) => m.reply_to_message != null;
  var isAdmin = (Message m) => m.from.id == 12345678;

  bot.onCommand('info')
  .when(isReply)
  .and(isAdmin)
  .then((m) async {
    await m.reply(m.reply_to_message.toString());
  });

  // Handle messages that aren't commands
  bot.onMessage
  .when((m) => m.text.contains('hello'))
  .then((m) async {
    await m.reply('hi');
  });

  // Launch the update handling loop
  await bot.start();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dartgram: ^0.5.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dartgram/dartgram.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
9
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Feb 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health issues and suggestions

Document public APIs. (-1 points)

432 out of 432 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/dartgram.dart.

Run dartfmt to format lib/dartgram.dart.

Format lib/src/bot.dart.

Run dartfmt to format lib/src/bot.dart.

Format lib/src/entities/callback_query.dart.

Run dartfmt to format lib/src/entities/callback_query.dart.

Fix additional 18 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/entities/chat.dart (Run dartfmt to format lib/src/entities/chat.dart.)
  • lib/src/entities/chat_permissions.dart (Run dartfmt to format lib/src/entities/chat_permissions.dart.)
  • lib/src/entities/files/animation.dart (Run dartfmt to format lib/src/entities/files/animation.dart.)
  • lib/src/entities/files/audio.dart (Run dartfmt to format lib/src/entities/files/audio.dart.)
  • lib/src/entities/files/chat_photo.dart (Run dartfmt to format lib/src/entities/files/chat_photo.dart.)
  • lib/src/entities/files/document.dart (Run dartfmt to format lib/src/entities/files/document.dart.)
  • lib/src/entities/files/file.dart (Run dartfmt to format lib/src/entities/files/file.dart.)
  • lib/src/entities/files/photo.dart (Run dartfmt to format lib/src/entities/files/photo.dart.)
  • lib/src/entities/files/video.dart (Run dartfmt to format lib/src/entities/files/video.dart.)
  • lib/src/entities/files/video_note.dart (Run dartfmt to format lib/src/entities/files/video_note.dart.)
  • lib/src/entities/keyboard.dart (Run dartfmt to format lib/src/entities/keyboard.dart.)
  • lib/src/entities/message.dart (Run dartfmt to format lib/src/entities/message.dart.)
  • lib/src/entities/message_entity.dart (Run dartfmt to format lib/src/entities/message_entity.dart.)
  • lib/src/entities/sticker.dart (Run dartfmt to format lib/src/entities/sticker.dart.)
  • lib/src/entities/user_profile_photos.dart (Run dartfmt to format lib/src/entities/user_profile_photos.dart.)
  • lib/src/entity.dart (Run dartfmt to format lib/src/entity.dart.)
  • lib/src/http_handler.dart (Run dartfmt to format lib/src/http_handler.dart.)
  • lib/src/rule.dart (Run dartfmt to format lib/src/rule.dart.)

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
pedantic ^1.8.0