Sunday, 16 February 2020

Select contacts from MSSQL and send them a SMS using Nexmo

Hi r/node !I'm a little bit stuck with an app I'm developing and I was hoping that you could lead me in the right direction on how to progress - thank you in advance!!First the TL;DR: I've made an app with node.js which lets me send SMS using the Nexmo API. What I want to change is instead of typing in the phone numbers, I want to connect to my Microsoft SQL Database where some numbers are stored, select a few (or at least one :D ) numbers and send every recepient the same message.I have limited experience with JavaScript (trying my best to learn as much I can) and I've discovered node.js after looking for a way to develop an app which let's me access a Microsoft SQL Database containing my contacts, select the ones I want to address and send them a SMS.I've read a lot about node.js and found some simple tutorials which I used to create what I have right now. TutorialExpress.js and ejs is used to display a simple form as a Webpage.NPM provides a package to connect to a MSSQL Server and fetch data out of it: https://www.npmjs.com/package/mssqlI'm stuck with my logic/knowledge on the following thing:After installing and configuring the MSSQL package, how do I tell node.js that I want to search for a Name and select the phone number. How is this passed on to the client-side/frontend? How do I create a searchbar and can the data be directly accessed from the database? How do I insert the selected number into the form in order to send it?​So far my app is structured like this:|app.js|Node_modules|public / js / main.js|views / index.html/ styles.cssCode:main.js// values of the formconst numberInput = document.getElementById('number'),textInput = document.getElementById('msg'),button = document.getElementById('button'),response = document.querySelector('.response'); //used querySelector because we are adressing a class not an Id//check if the button was clickedbutton.addEventListener('click', send, false);//catch parsed data from responseData for socket.ioconst socket = io();socket.on('smsStatus', function(data){    response.innerHTML = '
Text has been sent to ' + data.number + '
'}) //catching smsStatus from app.js//create send functionfunction send() {const number = numberInput.value.replace(/\D/g, '');const text = textInput.value;//make post request with this data using Fetch API || this needs to be "catched" on the server side why additional code is written to app.jsfetch('/', {        method: 'post',        headers: {'Content-Type': 'application/json'},//turn this into json string        body: JSON.stringify({            number: number,            text: text})})//fetch api uses promises so we need to use then/catch to get a response.then(function(res){console.log(res);}).catch(function(err){console.log(err);});}​app.jsconst express = require('express');const bodyParser = require('body-parser');const ejs = require('ejs');const Nexmo = require('nexmo');const socketio = require('socket.io');//Init NEXMOconst nexmo = new Nexmo({    apiKey: 'mkey',    apiSecret: 'mysecret'}, {debug: true});//Init appconst app = express();//engine setupapp.set('view engine', 'html');app.engine('html', ejs.renderFile);//public folder client side javascriptapp.use(express.static(__dirname + '/public'));//bodyParser middlewareapp.use(bodyParser.json());app.use(bodyParser.urlencoded( {    extended: true}));//index route (before this state, when opened localhost:4000, the message was 'Cannot GET')app.get('/', (req, res) => {    res.render('index');})//Catch submitted FORM from HTMLapp.post('/', (req, res) => {//change values that come from the form into variablesconst number = req.body.number;const text = req.body.text;const from = 'nexmo';nexmo.message.sendSms(from, number, text, {tpye: 'unicode'},(err, responseData) => {if(err) {console.log(err);} else {console.dir(responseData);//Get data from the responseDataconst data = {            id: responseData.messages[0]['message-id'],            number: responseData.messages[0]['to']}//Emit the responseData to the client to display a live information via socket.io || catch on client side is happening in main.js        io.emit('smsStatus', data);}});});//define Portconst port = 4000;//start serverconst server = app.listen(port, () => console.log('Server started on port ${port}'));//Connect to Socket.ioconst io = socketio(server);io.on('connection', (socket) => {console.log('Connected');io.on('disconnect', () => {console.log('Disconnected');})})//Socket.io Adavanced notifications. Confirmation is not only displayed in the innerHTML 

 but as an actual browser notification//Step 1 request Permission to notifiy//Step 2 creating function that passes data via socket.io function displayStatus(message) {var notification = new Notification('Nexmo', {      body: message//icon: 'images/XX.png'});}index.html

SMS APP

SMS Versand

              ​Every tip is precious so I'm grateful for any suggestions/docs/etc.

Submitted February 16, 2020 at 08:37PM by Ok-Suggestion

No comments:

Post a Comment