Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nexmo update #335

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/nexmo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
],
"dependencies": {
"@openfn/language-common": "^1.8.1",
"nexmo": "2.1.1",
"request": "^2.88.2"
"@vonage/auth": "^1.5.0",
"@vonage/server-sdk": "^3.7.0"
},
"devDependencies": {
"@openfn/buildtools": "workspace:^1.0.2",
Expand All @@ -49,4 +49,4 @@
"./package.json": "./package.json"
},
"types": "types/index.d.ts"
}
}
69 changes: 43 additions & 26 deletions packages/nexmo/src/Adaptor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
execute as commonExecute,
expandReferences,
composeNextState,
} from '@openfn/language-common';
import Nexmo from 'nexmo';
import { expandReferences } from '@openfn/language-common/util';
import { Auth } from '@vonage/auth';
import { Vonage } from '@vonage/server-sdk';

/**
* Execute a sequence of operations.
Expand All @@ -24,13 +25,36 @@ export function execute(...operations) {
};

return state => {
return commonExecute(...operations)({
return commonExecute(
createClient,
...operations,
cleanupState
)({
...initialState,
...state,
});
};
}

function createClient(state) {
const { apiKey, apiSecret } = state.configuration;
// TODO: throws an error if apiKey not specified in configuration
// TODO: should we set a default server if server not defined?
const options = {};
const credentials = new Auth({
apiKey: apiKey,
apiSecret: apiSecret,
});

const vonage = new Vonage(credentials, options);

return { ...state, client: vonage };
}

function cleanupState(state) {
delete state.client;
return state;
}
/**
* Sends an SMS message to a specific phone number
* @public
Expand All @@ -44,31 +68,24 @@ export function execute(...operations) {
*/
export function sendSMS(from, toNumber, message) {
return state => {
const { apiKey, apiSecret } = state.configuration;

const nexmo = new Nexmo({
apiKey: apiKey,
apiSecret: apiSecret,
});
const [resolvedFrom, resolvedToNumber, resolvedMessage] = expandReferences(
state,
from,
toNumber,
message
);

return new Promise((resolve, reject) => {
nexmo.message.sendSms(from, toNumber, message, (error, response) => {
if (error) {
console.error(error);
reject(error);
} else if (response.messages[0].status != '0') {
console.error('Nexmo Error:');
console.error(response);
reject(response);
} else {
console.log(response);
resolve(response);
}
return state.client.sms
.send({ resolvedToNumber, resolvedFrom, resolvedMessage })
.then(resp => {
console.log('Message sent successfully');
const nextState = composeNextState(state, resp);
return nextState;
})
.catch(err => {
console.log('There was an error sending the messages.');
console.error(err);
});
}).then(response => {
const nextState = composeNextState(state, response);
return nextState;
});
};
}

Expand Down
Loading
Loading