Git Product home page Git Product logo

line-bot-sdk-dart's Introduction

Library of the LINE Messaging API for Dart

pub package

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

  • Validation with signature from x-line-signature
  • Support parser and test cases for webhook event objects
  • Support the following APIs:
    • Reply message
    • Push message (Not yet test)
    • Get user profile
    • Get bot info
    • Get bot followers

Example

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

import 'package:line_bot/line_bot.dart';

Future main() async {
  var envVars = Platform.environment;
  var lineChannelSecret = envVars['LINE_CHANNEL_SECRET'];
  var lineChannelAccessToken = envVars['LINE_CHANNEL_ACCESS_TOKEN'];
  var lineBotApi = LineBotApi(lineChannelAccessToken);
  var webhookParser = WebhookParser(lineChannelSecret);
  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 {
  WebhookEvent message;
  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) {
    var messages = [
      Message(type: 'text', text: message.events[0].message.text)
    ];
    await lineBotApi.replyMessage(message.events[0].replyToken, messages);
  }
  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.

line-bot-sdk-dart's People

Contributors

cychiang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.