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

Library of the LINE Messaging API for Dart. The Library follows the LINE official Messaging API document and helps you to build your Bot much simpler and easier.

example/main.dart

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();
}
0
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Library of the LINE Messaging API for Dart. The Library follows the LINE official Messaging API document and helps you to build your Bot much simpler and easier.

Repository (GitHub)
View/report issues

License

Apache-2.0 (LICENSE)

Dependencies

crypto, http, json_annotation

More

Packages that depend on line_bot