@@ -12,12 +12,13 @@ import cors from '@koa/cors'
12
12
13
13
const base64Key = process . env . VONAGE_PRIVATE_KEY || ''
14
14
const buff = Buffer . from ( base64Key , 'base64' )
15
- const vonage = new Vonage ( new Auth ( {
15
+ const vonageAuth = new Auth ( {
16
16
apiKey : process . env . VONAGE_API_KEY ,
17
17
apiSecret : process . env . VONAGE_API_SECRET ,
18
18
privateKey : buff . toString ( 'ascii' ) ,
19
19
applicationId : process . env . VONAGE_APPLICATION_ID ,
20
- } ) ) ;
20
+ } ) ;
21
+ const vonage = new Vonage ( vonageAuth ) ;
21
22
22
23
async function home ( ctx : any ) {
23
24
ctx . body = { 'status' : 'ok' }
@@ -28,23 +29,14 @@ let driverNumber: string = '';
28
29
let clientNumber : string = '' ;
29
30
30
31
async function placeOrder ( ctx : any ) {
31
- console . log ( ctx . request . body )
32
- clientNumber = ctx . request . body . client_number ;
32
+ clientNumber = JSON . parse ( ctx . request . body ) . client_number ;
33
33
await vonage . numbers . getAvailableNumbers ( { country : 'US' , size : 1 , type : LineType [ 'LANDLINE-TOLL-FREE' ] } )
34
34
. then ( async ( resp : any ) => {
35
35
driverNumber = resp . numbers [ 0 ] . msisdn ;
36
- const buyURL = 'https://rest.nexmo.com/number/buy?' + new URLSearchParams ( [ [ "api_key" , process . env . VONAGE_API_KEY || '' ] , [ "api_secret" , process . env . VONAGE_API_SECRET || '' ] , [ 'country' , 'US' ] , [ 'msisdn' , driverNumber ] ] ) ;
37
- await fetch ( buyURL , {
38
- method : 'POST' ,
39
- } )
40
- . then ( resp => resp . json ( ) )
41
- . then ( async resp => {
42
- await fetch ( 'https://rest.nexmo.com/number/update?' + new URLSearchParams ( [ [ "api_key" , process . env . VONAGE_API_KEY || '' ] , [ "api_secret" , process . env . VONAGE_API_SECRET || '' ] , [ 'country' , 'US' ] , [ 'msisdn' , driverNumber ] , [ 'app_id' , process . env . VONAGE_APPLICATION_ID || '' ] ] ) , {
43
- method : 'POST' ,
44
- } )
45
- . then ( resp => resp . json ( ) )
46
- . then ( resp => {
47
- console . log ( 'Updated' , resp ) ;
36
+ await vonage . numbers . buyNumber ( { country : 'US' , msisdn : driverNumber } )
37
+ . then ( async ( ) => {
38
+ await vonage . numbers . updateNumber ( { country : 'US' , msisdn : driverNumber , applicationId : process . env . VONAGE_APPLICATION_ID } )
39
+ . then ( ( ) => {
48
40
ctx . body = { driver_number : driverNumber }
49
41
} )
50
42
. catch ( err => console . error ( err ) )
@@ -74,7 +66,6 @@ async function answerCall(ctx: any) {
74
66
}
75
67
76
68
async function answerMessage ( ctx : any ) {
77
- console . log ( deliveryPhone , driverNumber , clientNumber )
78
69
if ( ctx . request . body . from === clientNumber ) {
79
70
await vonage . messages . send ( new SMS ( 'From orderer: ' + ctx . request . body . text , deliveryPhone , driverNumber ) )
80
71
. then ( resp => { ctx . status = 200 ; ctx . body = { response : 'OK' } } )
0 commit comments