Skip to content

Commit

Permalink
del 'checkEnv'
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelDurachevskyi1987 committed Oct 7, 2022
1 parent f731d0f commit 07e0d5a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SMSC_LOGIN=nitra
VODAFONE_LOGIN=380997820157
VODAFONE_URL=https://a2p.vodafone.ua/
VODAFONE_BASE_URL=https://a2p.vodafone.ua/
VODAFONE_VALIDITY_PERIOD_SMS=000000230000000R
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,41 @@

add .env.test.local

```env
SMSC_PASS=secret
```env for vodafone
VODAFONE_LOGIN=38099XXXXXXX
VODAFONE_PASS=secret
VODAFONE_BASE_URL=https://address
VODAFONE_VALIDITY_PERIOD_SMS=000000230000000R
REDIS_CONN=address
```

```env for smsc
SMSC_LOGIN=login
SMSC_PASS=secret
```

## Usage

```JavaScript
import { sendSms } from 'smsc'

await sendSms('+37120128611', 'test')
// --> vodafone використовується для номерів з українським кодом оператора. для решти - smsc

// ------------------------ vodafone
await sendSms('+380689540703', 'test sms') // буде відправлено від 'ChernigivUA'
await sendSms('+380689540703', 'test sms', 'vybeeraicom') // буде відправлено від 'vybeeraicom'

// буде відправлено від вказаного відправника([sender]) і з вказаної розсилки([distributionId])
// distributionId - id розсилки яка повинна бути створена в кабінеті Vodafone.
// [sender] - відправник який повинен бути створений в кабінеті Vodafone і підв'язаний під цю розсилку
await sendSms('+380689540703', 'test sms', '[sender]', '[distributionId]')
// ------------------------ vodafone

// ------------------------ smsc
await sendSms('+37120128611', 'test sms')
await sendSms('+37120128611', 'test sms', '[sender]')
// ------------------------ smsc

```

```env.VODAFONE_VALIDITY_PERIOD_SMS (example) (скільки часу vodafone буде намагатись відправити смс)
Expand Down
55 changes: 35 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import checkEnv from '@nitra/check-env'
import { keyv } from './keyv.js'
import { isProd } from '@nitra/isenv'

checkEnv(['VODAFONE_URL', 'VODAFONE_LOGIN', 'VODAFONE_PASS', 'SMSC_LOGIN', 'SMSC_PASS'])

const login = process.env.SMSC_LOGIN
const password = process.env.SMSC_PASS
const charset = 'utf-8'

// Отправка CMC
export const sendSms = async (phones, message, sender = null) => {
export const sendSms = async (phones, message, sender = null, distributionId = null) => {
let data

if (getCountry(phones) === 'ua') {
data = await sendFromVf(phones, message, sender)
if (
!process.env.VODAFONE_BASE_URL ||
!process.env.VODAFONE_LOGIN ||
!process.env.VODAFONE_PASS ||
!process.env.VODAFONE_VALIDITY_PERIOD_SMS ||
(isProd && !process.env.REDIS_CONN)
) {
console.error(
`environment variables: "VODAFONE_BASE_URL", "VODAFONE_LOGIN", "VODAFONE_PASS", "VODAFONE_VALIDITY_PERIOD_SMS", "REDIS_CONN" must be set to send SMS via vodafone!!! ("REDIS_CONN" only prod)`
)
return { error_code: 1 }
}

data = await sendFromVf(phones, message, sender, distributionId)
} else {
if (!process.env.SMSC_LOGIN || !process.env.SMSC_PASS) {
console.error(`environment variables: "SMSC_LOGIN" and "SMSC_PASS" must be set to send SMS via smsc!!!`)
return { error_code: 1 }
}

data = await sendFromSmsc(phones, message, sender)
}

Expand Down Expand Up @@ -131,8 +145,8 @@ const getVfToken = async () => {
const vfRefreshToken = await keyv.get('vfRefreshToken')

const url = vfRefreshToken
? `${process.env.VODAFONE_URL}uaa/oauth/token?grant_type=refresh_token&refresh_token=${vfRefreshToken}`
: `${process.env.VODAFONE_URL}uaa/oauth/token?grant_type=password&username=${process.env.VODAFONE_LOGIN}&password=${process.env.VODAFONE_PASS}`
? `${process.env.VODAFONE_BASE_URL}uaa/oauth/token?grant_type=refresh_token&refresh_token=${vfRefreshToken}`
: `${process.env.VODAFONE_BASE_URL}uaa/oauth/token?grant_type=password&username=${process.env.VODAFONE_LOGIN}&password=${process.env.VODAFONE_PASS}`

// console.log('urlTokenData: ', url)

Expand All @@ -157,25 +171,26 @@ const getVfToken = async () => {
return data.access_token
}

const sendFromVf = async (phoneNumber, content, sender) => {
const sendFromVf = async (phoneNumber, content, sender, distributionId) => {
try {
let vfToken = (await keyv.get('vfToken')) || (await getVfToken())
const vfToken = (await keyv.get('vfToken')) || (await getVfToken())

if (!vfToken) {
console.log('Error: could not get a token for vodafone...')
return { error_code: 1 }
}

let distributionId
if (sender === 'vybeeraicom') {
distributionId = 3728396
} else {
sender = 'ChernigivUA'
distributionId = 3729115
if (!sender || !distributionId) {
if (sender === 'vybeeraicom') {
distributionId = 3728396
} else {
sender = 'ChernigivUA'
distributionId = 3729115
}
}

const response = fetch(
`${process.env.VODAFONE_URL}communication-event/api/communicationManagement/v2/communicationMessage/send`,
`${process.env.VODAFONE_BASE_URL}communication-event/api/communicationManagement/v2/communicationMessage/send`,
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -213,8 +228,8 @@ const sendFromVf = async (phoneNumber, content, sender) => {
const sendFromSmsc = async (phones, message, sender) => {
try {
const params = new URLSearchParams()
params.append('login', login)
params.append('psw', password)
params.append('login', process.env.SMSC_LOGIN)
params.append('psw', process.env.SMSC_PASS)
params.append('charset', charset)
params.append('fmt', 3)
params.append('phones', phones)
Expand Down
2 changes: 0 additions & 2 deletions src/keyv.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import KeyvRedis from '@keyv/redis'
import Keyv from 'keyv'
import { isProd } from '@nitra/isenv'
import checkEnv from '@nitra/check-env'

let store
if (isProd) {
checkEnv(['REDIS_CONN'])
store = new KeyvRedis(process.env.REDIS_CONN)
} else {
store = new Map()
Expand Down

0 comments on commit 07e0d5a

Please sign in to comment.