npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-line-bot

v0.1.0

Published

LINE Bot module for Nest

Downloads

7

Readme

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