smspva.service.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { GetNumberResponse, GetNumberService } from './get-number-service'
  2. import { RcsNumberSource } from '../entities/rcs-number.entity'
  3. import axios from 'axios'
  4. import { InternalServerErrorException, Logger } from '@nestjs/common'
  5. const API_KEY01 = 'uNW56fGr0zstfs87Xn0e1l2gCYVnb1'
  6. const API_KEY02 = 'rTTL8pZtKkQ60zjU82bvbMEP7G6XGU'
  7. const SERVICE = 'opt259'
  8. const axiosInstance = axios.create({
  9. baseURL: 'http://8.218.211.187/activation/',
  10. headers: {
  11. uhost: 'api.smspva.com',
  12. uprotocol: 'https'
  13. }
  14. })
  15. const countryMap = {
  16. US: 'US',
  17. US1: 'US',
  18. US2: 'US',
  19. US3: 'US',
  20. GB: 'UK'
  21. }
  22. export class smspva extends GetNumberService {
  23. source: RcsNumberSource
  24. apikey: string
  25. constructor(key: string, source: RcsNumberSource) {
  26. super()
  27. this.apikey = key
  28. this.source = source
  29. }
  30. async getNumber(country: string, num?: number): Promise<GetNumberResponse> {
  31. let countryCode = countryMap[country]
  32. if (!countryCode) {
  33. countryCode = country
  34. }
  35. const res = await axiosInstance.get(`number/${countryCode}/${SERVICE}`, {
  36. headers: {
  37. apikey: this.apikey
  38. }
  39. })
  40. if (res.data.statusCode !== 200) {
  41. throw new InternalServerErrorException(res.data.error.description)
  42. }
  43. return {
  44. number: res.data.data.phoneNumber,
  45. orderId: res.data.data.orderId,
  46. operatorCode: '',
  47. operatorName: '',
  48. rawResponse: res.data
  49. }
  50. }
  51. async releaseNumber(number: string) {
  52. const res = await axiosInstance.put(
  53. `cancelorder/${Number(number)}`,
  54. {},
  55. {
  56. headers: {
  57. apikey: this.apikey
  58. }
  59. }
  60. )
  61. if (res.data.statusCode === 200) {
  62. Logger.log(`release orderId ${number} success`)
  63. } else {
  64. throw new InternalServerErrorException(res.data.error.description)
  65. }
  66. }
  67. async retriveMessage(orderId: string, num?: number): Promise<string> {
  68. const res = await axiosInstance.get(`sms/${Number(orderId)}`, {
  69. headers: {
  70. apikey: this.apikey
  71. }
  72. })
  73. if (res.data.statusCode === 200) {
  74. return res.data.data.sms.fullText
  75. }
  76. }
  77. async blockNumber(orderId: string) {
  78. try {
  79. await axiosInstance.put(
  80. `blocknumber/${orderId}`,
  81. {},
  82. {
  83. headers: {
  84. apikey: this.apikey
  85. }
  86. }
  87. )
  88. } catch (error) {
  89. Logger.error(`block number ${orderId} failed`, error)
  90. }
  91. }
  92. async cacheNumber(country: string, size: number) {
  93. return null
  94. }
  95. }