0.1.0 • Published 8 months ago

nest-line-bot v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

nest-line-bot

NPM version

LINE Bot module for Nest

Installation

To begin using it, we first install the required dependencies.

$ npm install --save nest-line-bot @line/bot-sdk

Getting started

Once the installation is complete, import the LineBotModule into the root AppModule and run the forRoot() static method as shown below:

import { Module } from '@nestjs/common';
import { LineBotModule } from 'nest-line-bot';

@Module({
  imports: [
    LineBotModule.forRoot({
      channelAccessToken: 'LINE_CHANNEL_ACCESS_TOKEN',
      channelSecret: 'LINE_CHANNEL_SECRET',
    }),
  ],
})
export class AppModule {}

Next, inject the line.messagingApi.MessagingApiClient instance using the @InjectLineMessagingApiClient() decorator.

import { Injectable } from '@nestjs/common';
import { InjectLineMessagingApiClient } from 'nest-line-bot';
import * as line from '@line/bot-sdk';

@Injectable()
export class AppService {
  constructor(
    @InjectLineMessagingApiClient() private readonly client: line.messagingApi.MessagingApiClient,
  ) {}

  async pushMessage() {
    await this.client.pushMessage({
      to: 'LINE_USER_ID',
      messages: [{ type: 'text', text: 'hello, world' }]
    });
  }
}

Webhook

The LINE Platform sends an HTTP POST request with a webhook event object to the webhook URL (bot server) you register in the LINE Developers Console.

This module offers a decorator to help you easily build a webhook server. By using the @OnWebhookEvent() decorator in your controller, you can define an endpoint to receive webhook events.

import { OnWebhookEvent } from '@fugletrader/core';
import { Body, Controller, Post } from '@nestjs/common';

@Controller()
export class NotifierController {
  @Post('/webhook')
  @OnWebhookEvent()
  async webhook(@Body() body) {
    return body.events;
  }
}

Async configuration

When you need to pass module options asynchronously instead of statically, use the forRootAsync() method. As with most dynamic modules, Nest provides several techniques to deal with async configuration.

One technique is to use a factory function:

LineBotModule.forRootAsync({
  useFactory: () => ({
    channelAccessToken: 'LINE_CHANNEL_ACCESS_TOKEN',
    channelSecret: 'LINE_CHANNEL_SECRET',
  }),
});

Like other factory providers, our factory function can be async and can inject dependencies through inject.

LineBotModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    channelAccessToken: configService.get('LINE_CHANNEL_ACCESS_TOKEN'),
    channelSecret: configService.get('LINE_CHANNEL_SECRET'),
  }),
  inject: [ConfigService],
});

Alternatively, you can configure the LineBotModule using a class instead of a factory, as shown below.

LineBotModule.forRootAsync({
  useClass: LineBotConfigService,
});

The construction above instantiates LineBotConfigService inside LineBotModule, using it to create an options object. Note that in this example, the LineBotConfigService has to implement LineBotOptionsFactory interface as shown below. The LineBotModule will call the createLineBotOptions() method on the instantiated object of the supplied class.

@Injectable()
class LineBotConfigService implements LineBotOptionsFactory {
  createLineBotOptions(): LineBotModuleOptions {
    return {
      channelAccessToken: 'LINE_CHANNEL_ACCESS_TOKEN',
      channelSecret: 'LINE_CHANNEL_SECRET',
    };
  }
}

If you want to reuse an existing options provider instead of creating a private copy inside the LineBotModule, use the useExisting syntax.

LineBotModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: LineBotConfigService,
});

References

License

MIT

0.1.0

8 months ago