line_bot 0.0.1 copy "line_bot: ^0.0.1" to clipboard
line_bot: ^0.0.1 copied to clipboard

outdated

Library of the LINE Messaging API for Dart

Library of the LINE Messaging API for Dart

IMPORTANT: This is not an official SDK by LINE

Introduction #

We make it easy to developers to code their bots with Dart! A few steps to create a parser and bot to handle requests and reply messages. The library is based on the official LINE Messaging API Document and inspired by the official SDK: line-bot-sdk-python

Implemented methods, events and messages #

  • ✅ Support parser and test cases for webhook event objects
  • ✅ Validation with signature from x-line-signature
  • ✅ Reply message with TextMessage

Example #

import 'dart:convert';
import 'dart:io';

import 'package:line_bot_sdk_dart/line_bot.dart';

Future main() async {
  var envVars = Platform.environment;
  var lineChannelSecret = envVars['LINE_CHANNEL_SECRET'];
  var lineChannelAccessToken = envVars['LINE_CHANNEL_ACCESS_TOKEN'];
  var webhookParser = WebhookParser(lineChannelSecret);
  var lineBotApi = LineBotApi(lineChannelAccessToken);
  var server = await HttpServer.bind(
    InternetAddress.loopbackIPv4,
    8080,
  );
  await for (var request in server) {
    if (request.method == 'POST' && request.uri.path == '/callback') {
      handleRequest(request, webhookParser, lineBotApi);
    } else {
      handleUnSupportedRequest(request);
    }
  }
}

void handleRequest(HttpRequest request, WebhookParser webhookParser,
    LineBotApi lineBotApi) async {
  var message;
  var replyMessage;
  var response = request.response;
  var content = await utf8.decoder.bind(request).join();
  try {
    message = webhookParser.parser(
        content.toString(), request.headers['x-line-signature'][0]);
  } on InvalidSignatureError {
    await response
      ..statusCode = HttpStatus.badRequest
      ..write('InvalidSignatureError')
      ..close();
    return;
  }
  if (message.events.isNotEmpty) {
    replyMessage = ReplyMessage(
        replyToken: message.events[0].replyToken,
        messages: [
          Message(type: 'text', text: message.events[0].message.text)
        ]);
    await lineBotApi.replyMessage(replyMessage);
  }
  response
    ..statusCode = HttpStatus.ok
    ..write('Request Accepted.')
    ..close();
}

void handleUnSupportedRequest(HttpRequest request) {
  var response = request.response;
  response
    ..statusCode = HttpStatus.methodNotAllowed
    ..write('Unsupported request: ${request.method}.')
    ..close();
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0
likes
30
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

Library of the LINE Messaging API for Dart

Repository (GitHub)
View/report issues

License

Apache-2.0 (license)

Dependencies

crypto, http, json_annotation

More

Packages that depend on line_bot