Vitalik
2 years ago
12 changed files with 194 additions and 31 deletions
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
export const ALPHASMS_API_KEY = Symbol('ALPHASMS_API_KEY') |
||||
export const ALPHASMS_FROM = Symbol('ALPHASMS_FROM') |
@ -1,3 +1,6 @@
@@ -1,3 +1,6 @@
|
||||
export interface ISmsModuleOptions { |
||||
test: boolean |
||||
|
||||
alphasmsApiKey?: string |
||||
alphasmsFrom?: string |
||||
} |
||||
|
@ -0,0 +1,45 @@
@@ -0,0 +1,45 @@
|
||||
import { HttpService } from '@nestjs/axios' |
||||
import { Inject, Injectable } from '@nestjs/common' |
||||
import { AxiosError } from 'axios' |
||||
import { catchError, firstValueFrom } from 'rxjs' |
||||
import { Sms } from 'src/core' |
||||
import { ALPHASMS_API_KEY, ALPHASMS_FROM } from '../const' |
||||
|
||||
@Injectable() |
||||
export class AlphaSmsService implements Sms.ISmsService { |
||||
@Inject(ALPHASMS_API_KEY) |
||||
private readonly alphasmsApiKey: string |
||||
|
||||
@Inject(ALPHASMS_FROM) |
||||
private readonly alphasmsFrom: string |
||||
|
||||
constructor(private readonly httpService: HttpService) {} |
||||
|
||||
onModuleInit() { |
||||
if (!this.alphasmsApiKey || !this.alphasmsFrom) throw new Error('AlphaSms not config') |
||||
} |
||||
|
||||
public async send(payload: Sms.ISendPayload): Promise<void> { |
||||
const result = await firstValueFrom( |
||||
this.httpService |
||||
.get('https://alphasms.ua/api/http.php', { |
||||
params: { |
||||
version: 'http', |
||||
key: this.alphasmsApiKey, |
||||
from: this.alphasmsFrom, |
||||
to: payload.to, |
||||
message: payload.text, |
||||
command: 'send', |
||||
}, |
||||
timeout: 36000, |
||||
}) |
||||
.pipe( |
||||
catchError((error: AxiosError) => { |
||||
console.log('error', error) |
||||
throw 'An error happened!' |
||||
}), |
||||
), |
||||
) |
||||
console.log(result) |
||||
} |
||||
} |
Loading…
Reference in new issue