how can I send an IP messaging using twilio-csharp library - javascript

I just start create a live chat app with Twilio.
I have downloaded the twilio-csharp library from twilio.com/docs/csharp/install and started with a sample console application.
code example from Twilio:
// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio.IpMessaging;
class Example {
static void Main (string[] args) {
// Find your Account Sid and Auth Token at twilio.com/user/account
const string accountSid = "accountSid";
const string authToken = "authToken";
const string serviceSid = "serviceSid";
const string channelSid = "channelSid";
// List all Messages
var client = new IpMessagingClient(accountSid, authToken);
MessageResult result = client.ListMessages(serviceSid, channelSid);
Console.WriteLine(result);
}
}
But this code doesn't work in my case, I got always an errors "type or namespace could not be found".
I have these like reference:
I tried tutorial of IP Messaging , it works. But in the tutorial, they used Javascript SDK to initialize the IP messaging client
tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
So I just don't understand how can I use this C# library to send an IP Messaging, or maybe I can just control my IP Messaging applications from the client side?

I unstand why I got this error.
I just follow the steps of installation on twilio-csharp library gitHub. But this project's README is only for SMS project. If we want to use Library for IpMessaging, we must also import another Library, Twilio.IpMessaging.
So What I got know like reference and the project IP Messaging works.
Hope this will help the others.

Related

Google sheets API JavaScript: Access sheet with auth

I'm able to access a public google sheet from within my react app but am now trying access the sheet with credentials. I found a tutorial that walks me thru setting up credentials but the code isn't working for me.
const {google} = require('googleapis');
const keys = require('./interstitials-key.json');
const client = new google.auth.JWT(
keys.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/spreadsheets']
);
client.authorize(function(err, tokens){
if(err){
console.log(err);
return;
}
else{
console.log('connected');
}
});
I'm getting this error:
"TypeError: Expected input to be a Function or Object, got undefined"
This is a known issue, you'll find reference over here:
https://github.com/googleapis/google-api-nodejs-client/issues/1614
I've reproduced it, certainly it's not fixed, you'll face this error as soon as you call the library
const {google} = require('googleapis');
Some of the resources used on the library are not available on the client side, so, it's not possible to call it from the React side, so you either use it on the server side or you have to use the google javascript client api.

Connect to postgresdb using typescript from nodejs server

I am new to typescript and nodejs. I wanted to connect a server running on node to postgresdb in a containerized environment. There are examples available for this without typescirpt (only java script ) .
Any example or link would be helpful .
Thanks
Edit :
This JS Example
const { Client } = require('pg')
const client = new Client()
await client.connect()
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
await client.end()
This is the example taken from https://node-postgres.com/ and can we convert that into typescript , Using typescirpt is design decision so i cant use javascript, wanted to try something and experiment before giveup on it.
I ended up using typeorm and that can be used with TS.
https://github.com/typeorm/typeorm
Installation and examples available in Github Repo.
https://github.com/typeorm/typeorm#Installation
This might not work for everyone but seems best fit for me and the library looks stable.

CometD client Java APIn how to?

For my project I have to use pubsub and cometD subscriber.
I use Oracle Weblogic application server for two applciations.
One of them publish some messages to pubsubs channels and the other one subscribe to channels to display the messages.
My pubsub server is on the weblogic application server too and configured with some xml files (weblogic.xml and weblogic-pubsub.xml).
Here is how my pubsub server is configured (weblogic-pubsub.xml):
<wlps:channel>
<wlps:channel-pattern>/gip/**</wlps:channel-pattern>
</wlps:channel>
<wlps:channel-constraint>
<wlps:channel-resource-collection>
<wlps:channel-resource-name>all-permissions</wlps:channel-resource-name>
<wlps:description>Grant all permissions for everything by everyone</wlps:description>
<wlps:channel-pattern>/gip/*</wlps:channel-pattern>
</wlps:channel-resource-collection>
</wlps:channel-constraint>
And it works well because my second application can susbscribe to channel with the cometD subscirber javascript API and dojo toolkit.
So now the subscription is done client side of my web application thanks to this Javascript API.
Here is how the subscription is done client side (Javascript API) with the dojo toolkit:
//Initialize Dojo (CometD) for pubsub events
dojo.require("dojo.io.script");
dojo.require("dojox.cometd");
dojo.require("dojox.cometd.callbackPollTransport");
dojo.addOnLoad(function ()
{
console.log("on load dojo");
dojox.cometd.init("/WebInterface/cometd", {
});
dojox.cometd.subscribe("/gip/**", onEvent);
initMap();
});
This client side implementation works well, the onEvent() function is well fired when messages reach the pubsub channel.
Now, I would like the subscription and the message handling are done server side. For this, I understood that CometD has also a client Java API allowing to subscribe to pubsub channel and to handle the messages.
But I have not succeeded to do that.
Here is now what I tried to do for the server side following the CometD 3 documentation (https://docs.cometd.org/current/reference/#_java_client) :
import com.vaadin.ui.CustomComponent;
import java.util.HashMap;
import java.util.Map;
import org.cometd.bayeux.Channel;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.client.ClientSession;
import org.cometd.bayeux.client.ClientSessionChannel;
import org.cometd.client.BayeuxClient;
import org.cometd.client.transport.ClientTransport;
import org.cometd.client.transport.LongPollingTransport;
import org.eclipse.jetty.client.HttpClient;
public class WireServerCometD extends CustomComponent {
private static final String CHANNEL = "/gip";
private final ClientSessionChannel.MessageListener gipListener = new GIPListener();
public WireServerCometD() {
System.out.println("Wire CometD constructor");
setSizeFull();
setWidth(50, Unit.PERCENTAGE);
setHeight(300, Unit.PIXELS);
addStyleName("customBackground");
try {
// Create (and eventually set up) Jetty's HttpClient:
HttpClient httpClient = new HttpClient();
// Here set up Jetty's HttpClient, for example:
// Prepare the transport
Map<String, Object> options = new HashMap<String, Object>();
ClientTransport transport = new LongPollingTransport(options, httpClient);
// Create the BayeuxClient
ClientSession client = new BayeuxClient("http://localhost:8080/WebInterface/cometd", transport);
client.getChannel(CHANNEL).addListener(new ClientSessionChannel.MessageListener() {
public void onMessage(ClientSessionChannel channel, Message message) {
if (message.isSuccessful()) {
// Here handshake is successful
System.out.println("Handshake is successfull");
}
}
});
client.handshake();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static class GIPListener implements ClientSessionChannel.MessageListener {
public void onMessage(ClientSessionChannel channel, Message message) {
System.out.println("message received");
}
}
}
This is a Vaadin framework component, the channel subscription and message listener are done in the try block.
I have the following error at the code line HttpClient httpClient = new HttpClient(); :
SEVERE:
java.lang.IncompatibleClassChangeError: org/eclipse/jetty/client/HttpClient
And the onMessage function is never fired ...
Can you bring me some help please ?
Thank you,
First of all, I think WebLogic may ship a very old version of CometD, or a very customized one that does not match the official one from the CometD project.
The dojox.cometd.callbackPollTransport was not something that ever existed in the CometD project, it was probably a draft attempt when CometD was 0.x, or something that was not officially released, or something created by WebLogic.
Your chances to have an official CometD 3.x client work with the "CometD" shipped by WebLogic are very slim. I doubt they are compatible.
Furthermore, I don't think Vaadin will be able to translate the component you wrote above in JavaScript.
A while back, people wrote bindings for CometD in JavaScript, but those never entered officially the CometD project (lack of traction, see https://github.com/cometd/cometd/issues/63), so I am not sure in what state they are now.
The IncompatibleClassChangeError is probably due to the fact that you are using a JDK older than JDK 7, and CometD 3.x only works with JDK 7+.
I'm afraid you will have to rethink the whole system.
I would suggest to stick with the official CometD on the server (not the one shipped by WebLogic), and if you really have to use Vaadin/GWT have a look at how people wrote those bindings in the past, and perhaps replicate them if you can't use those libraries.
Once you have the Vaadin/GWT bindings in place, and the official CometD in the server, and JDK 7+, you should be good.

authenticating a service account to call a Google API with JavaScript client library

I want to make JSON-RPC calls from localhost (WAMP environment) to the Google FusionTables API (and a couple of other APIs) using the Google Client Library for JavaScript
Steps I have taken:
setup a project on the Google Developer Console
enabled the FusionTables API
created a service account and downloaded the JSON file.
successfully loaded the JS client library with the auth package: gapi.load('client:auth2', initAuth);
constructed the init method parameter the following 3 ways:
the downloaded JSON verbatim
the downloaded JSON modified to include the scope
just the client ID and scope
tried (and failed) to initialize the GoogleAuth instance: gapi.auth2.init(params)
function failed(reason) {
console.log(reason);
}
gapi.load('client:auth2', initAuth);
function initAuth() {
var APIkey = 'MY API KEY';
gapi.client.setApiKey(APIkey); //I understand this to be unnecessary with authorized requests, included just for good measure
var GDTSAKey = 'MY SERVICE ACCOUNT KEY';
var scopes = 'https://www.googleapis.com/auth/fusiontables';
gapi.auth2.init({
client_id: "101397488004556049686",
scope: 'https://www.googleapis.com/auth/fusiontables'
}).then(signin, failed("couldn't initiate"));
//passing the downlaoded JSON object verbatim as parameter to init didn't work either
} //initAuth()
function signin() {
gapi.auth2.getAuthInstance().signIn().then(makeAPIcall), failed("couldn't sign-in");
}
function makeAPIcall(){
gapi.client.load('fusiontables', 'v2', function(){
var tableId = '1PSI_...';
var table = gapi.client.fusiontables.table.get(tableId);
document.querySelector("#result").innerHTML = table;
});
}
based on JS client library >> Samples
the gapi.auth2.init method invokes the second callback (which I understand to be an error handler): failed("couldn't initiate"), but then, curiously, I also get `couldn't sign in' which could only have originated from within the provided success handler. What's going on? How do I get this to work?
Note: I am only willing to try the CORS/xhr, if there is no way to do it with JS client lib.
What's going on?
You are trying to use a service account with the Google JavaScript client library which does not support service accounts.
How do I get this to work?
Switch to Oauth2 authentication or if you must use a service account switch to a server sided language like PHP or python for example. Which support service account authentication.

How Can I use MongoDb with angular

I have a controller that I can use the functions in the template. I am using IONIC, but I can't access the mongoDB, How can I do it ? I have no idea how to do this.
$scope.InsertUser = function(){
user = $("#NameId").val();
db.users.insert(user);
}
so the log wrote "db is not defined", I agree that I cant use this on clientSide but Where is the serverSide ?. my project directory tree:
enter image description here
I've already saw a lot of videos or tutorials but is the same place I declare the line : db.users.insert(user);
I do not understand Where I need to declare my collections or where I can put my CRUD methods.
you need declare all of this in the server side. I'm not sure which sever side you uses ,but I uses play framework with java as server side. What I do is triggers a GET request from angular with restAngular , to a function in the server side.
example:
client side:
return: function(){
restAngular.all("/get").get().then(function(result){
$scope.array = result;
})
}
(the GET request will get it response from this function)server side:
private final MongoClient mongoClient = new MongoClient();
private final MongoDatabase db = mongoClient.getDatabase("test");
private final MongoCollection<Document> usersCollection = MongoDatabase.getCollection("posts");
public static Result getResult(){
return usersCollection.find().sort(descending("date"))
.limit(limit)
.into(new ArrayList<Document>());
}
this is just a little example how to do it with angular as client side and java as server side. for more help you can comment her.

Categories

Resources