https://github.com/sar1008/nestjs-airgram
Airgram module for Nest framework (node.js) ✈️
https://github.com/sar1008/nestjs-airgram
nestjs nodejs tdl telegram telegram-api typescript
Last synced: about 2 months ago
JSON representation
Airgram module for Nest framework (node.js) ✈️
- Host: GitHub
- URL: https://github.com/sar1008/nestjs-airgram
- Owner: sar1008
- License: mit
- Created: 2024-03-31T15:59:19.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-31T16:00:04.000Z (over 2 years ago)
- Last Synced: 2025-01-27T12:22:24.197Z (over 1 year ago)
- Topics: nestjs, nodejs, tdl, telegram, telegram-api, typescript
- Language: TypeScript
- Homepage:
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Description
[Airgram](https://github.com/airgram/airgram) module for [Nest](https://github.com/nestjs/nest).
## Installation
**NPM**
```bash
$ npm i -s nestjs-airgram
```
**Yarn**
```bash
$ 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](https://github.com/tdlib/td#building), or you can found [here](https://github.com/Bannerets/tdl#installation),
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
```typescript
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.
```typescript
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 {
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 {
// This code will be invoked after update.
}
@OnRequest()
async onRequest(): Promise {
// This code will be invoked before request.
}
@OnEvent('getMe')
onGetMe(ctx: ExtractMiddlewareContext): void {
console.log('"GetMe" request triggered', ctx);
// This code will be invoked before "Get me" request.
}
@OnEvent('updateNewMessage')
onNewMessage(
ctx: ExtractMiddlewareContext,
): void {
console.log('"NewMessage" update triggered', ctx);
// This code will be invoked after "New message" update.
}
}
```