Git Product home page Git Product logo

nestjs-airgram's Introduction

Nest Logo

NPM Version Package License NPM Downloads

Description

Airgram module for Nest.

Installation

NPM

$ npm i -s nestjs-airgram

Yarn

$ yarn add nestjs-airgram

Quick Start

Once the installation process is complete, now we need TDLib 1.7 binaries, how to get them you need to build them by this guide, or you can found here, then place binaries somewhere in your project workspace. Then we can import the module TdlModule either synchronously or asynchronosly into the root AppModule.

It is important to specify the correct path to the TDLib binaries in the command field. It is directly passed to dlopen / LoadLibrary. Check your OS documentation to see where it searches for the library.

 

Synchronous configuration

import { Module } from '@nestjs/common';
import { AirgramModule } from 'nestjs-airgram';

@Module({
  imports: [
    AirgramModule.forRoot({
      apiId: 'YOUR_APP_ID',
      apiHash: 'YOUR_API_HASH',
      command: path.resolve('tdjson.dll'), // Path to tdlib
      auth: {
        // ...
      },
    }),
  ],
})
export class AppModule {}

Then we can inject Airgram into our services. And use decorators for events.

import { Injectable, OnModuleInit } from '@nestjs/common';
import {
  Airgram,
  Context,
  GetMeMiddleware,
  UpdateNewMessageMiddleware,
} from 'airgram';
import {
  ExtractMiddlewareContext,
  InjectAirgram,
  OnEvent,
  OnRequest,
  OnUpdate,
} from 'nestjs-airgram';

@Injectable()
export class AppService implements OnModuleInit {
  constructor(@InjectAirgram() private airgram: Airgram) {
    setTimeout(() => this.onModuleInit(), 4000);
  }

  async onModuleInit(): Promise<void> {
    const me = await this.airgram.api.getMe();
    console.log('[Me]', me);
  }

  @OnEvent()
  onAnyEvent(ctx: Context): void {
    // This code will be invoked before every request and after all updates.
  }

  @OnUpdate()
  async onUpdate(update: unknown): Promise<void> {
    // This code will be invoked after update.
  }

  @OnRequest()
  async onRequest(): Promise<void> {
    // This code will be invoked before request.
  }

  @OnEvent('getMe')
  onGetMe(ctx: ExtractMiddlewareContext<GetMeMiddleware>): void {
    console.log('"GetMe" request triggered', ctx);
    // This code will be invoked before "Get me" request.
  }

  @OnEvent('updateNewMessage')
  onNewMessage(
    ctx: ExtractMiddlewareContext<UpdateNewMessageMiddleware>,
  ): void {
    console.log('"NewMessage" update triggered', ctx);
    // This code will be invoked after "New message" update.
  }
}

nestjs-airgram's People

Contributors

sar1008 avatar

Watchers

 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.