Pass Twilio SMS Post Data from Node to React - javascript

I'm currently using Twilio to receive SMS messages on my server. I need to display the return data in React. Twilio only sends the data server side through a POST request. If I send a text from my phone. The Twilio POST request will be send the data to my server. How can I get the POST data sent to my React app? The below code is how I receive the data from Twilio.
app.post('/sms', (req, res) => {
var msgFrom = req.body.From;
var msgBody = req.body.Body;})

yes you can and there are two ways:
1- you can use the the HTTP regular request model, you can achieve this by sending a request checking for updates and if there is what and update the front end.
2- long polling or streams sockets to emit the data to your applications from the back end.

Related

How would I create an android app with an array stored on a web server using android studio?

I am trying to make a 'shower thoughts' app. It has three categories stored in arrays. Underneath each are nested the actual list of user posts that the user can see (as arrays) . Of course, a user wont be able to see others posts. I have a rasbpi I am using as a webserver. The way I thought of doing this is a bit of javascript code on the web server that contain two functions: One that adds a users post to an array (with the input arguements being the post itself) and one that returns the array so that the app can update. However, I have no idea how to CALL this js file on the web server from the application. How would I do this?
You will have to construct a web API for this. You can do this using NodeJS on the server and then send requests to this API in Android
Basically what you are trying to do is create a simple Server-Client Architecture.
There are multiple ways to do so. I am going to explain using NodeJS as the server and volley on the client (android) side.
Firstly you can use Volley, to create an API call from android, that would interact with the NodeJS API, which in turn will create the array and store it in either SQL or any other form you want.
For reference you can check out this two projects.
This is an Android app which is using Volley to communicate with the server.
This is the NodeJS Server which is a simple REST API.
You can also use this link to learn basics about NodeJS if you don't know.
This is the basic NodeJS code to create a server and then send a response to the client.
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
This is the basic Android Volley code to make a request to the server.
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
textView.setText("Response is: "+ response.substring(0,5));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
textView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
If you don't understand any of these code you can comment below.

Getting data from a API on the client side without giving the key

Currently I am making a page that display's data gathered from an API. Most of the data is updated on the server side every 4 hours but some of it is updated whenever a client requests the index route. As a result, there is a delay in the index file being sent since the data needs to be updated. I want to gather the updated data after the page has been requested and sent so there is no delay. My first idea was to make the request on the client side which will handle updating the display after the data is gathered but from my knowledge, I don't know how to do that without giving them the API key. Should I approach the problem this way or is there a better way to do it? I'm using Express for the back-end, Axios is used to make the get requests, and EJS is the template engine.
Here is the code:
// This is called before the data is send in a for loop
data.gameData[i].player_count = await SteamModule.liveGetCurrentPlayers(data.gameData[i].appid);
res.render('index', {data: data});
// This is the function that is called
liveGetCurrentPlayers: async (id) => {
const res = await axios.get(`${base}/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?key=${key}&appid=${id}`, {timeout: 1000}).catch(err => {
Logger.error("Error getting updated user data per request");
return 'Error';
});
if(res.data) {
return res.data.response.player_count;
} else {
return 'Error';
}
}
Here's a bit of a drawing to explain what I've said in comments.....
(the code you showed should constantly update - without other info I can't help with whatever the other issue was, but this is the overall idea....)
Where:
Client requests data from you (your server)
Your server sends html and css to show a 'frame' of the page (no data, just something for them to see and feel like something is happening...)
Your server requests data from the API server (all the various "20-ish" things you said you wanted to serve....)
As the data is updated (or you may have it already), you send the data to the client, updating their 'frame' page with current data.
You can maintain your keys on the server side and add restriction that those API's can only be accessed by your client side URL. So you would access the API and it will maintain your session and handle the authorized KEY part as well.
Anything and everything on the client side is accessible if it running in your browser.
You can add security measures on the server but not on client side for protecting your key.

MQTT server to client communication

I want to communicate between server and client using mosca.At first case i subscribe and publish data from client.And receive that data into server.But i face some problem again i want publish the data from server and receive that data from same client.
Example: From front-end send same data to the server.After receiving those data i want store those data in database.After save the data i want send the some response(data) to the same client using Mosca(in Javascript).
Thanks for giving answer.
If you trying to use a MQTT client on browser you should use a option to activate websocket.
Something like this:
const mosca = require("./");
const server = new mosca.Server({
http: {
port: 3000, //use this port to connect
bundle: true,
static: './'
}
});

Twilio: Where do the params from Twilio.Device.connect() get sent to

The Twilio JS library has a function called Twilio.Device.connect() which takes params. In the documentation it says these params get sent to the server, but it never specifies which server endpoint it goes to or how to set this up. https://www.twilio.com/docs/client/device
Can anybody explain?
Twilio evangelist here.
The parameters get converted into form encoded values and included in the HTTP request that is made to the URL configured for your TwiML App.
So for example, if you include parameters like this:
params = {"PhoneNumber": "+15555555555"};
Twilio.Device.connect(params);
they will get converted into this set of form encoded values and included in the POST request Twilio makes to your Twiml Apps Voice URL:
PhoneNumber=+15555555555
You can use whatever mechanism in your server side framework that exposes form values to grab those parameters and use them to generate and return TwiML. For example, in PHP:
$phoneNumber = $_REQUEST["PhoneNumber"];
Hope that helps.
Twilio provide web-hook for different events like when call initiated, ringing, completed etc, so you can get your custom parameter from web-hook ,
For Example Lets suppose you want channelId in server side, so First create one GET/POST API and assign to statusCallback url like in TwiML Bin
<Client
statusCallbackEvent='initiated ringing answered completed'
statusCallbackMethod= "GET" statusCallback="https://{{SERVER_ENDPOINT}}/twilio/peer-to-peer-call/{{channelId}}" >
{{To}}
</Client>
Now you can retrieve channelId as request params or request query in your server

Pass query params from node.js server to socket.io

I'm writing a node.js app, that can generate url's like this:
http://examle.com/?param1=val&param2=val
I wonder if users will follow this urls to my app - is this possible to get params from this url and pass it to socket.io socket object that represents connection with user that was brought to site by that url.
I.E.:
user followed this url;
node.js express server handles that url and get the query params from it;
Now I want to pass these params back to user but with socket.io emit(), not to whole sockets but only to user that was brought by url.
Is this goal achivable?
Should I use some id that will be passed with url and along socket.io handshake process and than store socket in array with id as key, to get this socket later in get request of express server?

Categories

Resources