node.jslyncucwa

Chat Application using node js and UCWA


I am trying to create a chat application using node js and ucwa.I want to send message from node js aplication to a registered lync user.For that i am using ucwa.I am able to authenticate,authorise and create application by calling ucwa apis.After that i need some help in sending IM to a registered lync user.Can some one please help me with some code examples??

I am using the code as in UCWA Lync authentication - 500 web ticket is invalid. After this step how to send IM through node js and UCWA?

Following is my code :

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){

    var http = require('request-promise');


var lync = {};

lync._authorize = function(){

    var self = this;

    var orgDomain = self.urls.user.match(/https:\/\/([\w\d\.]+)/i)[0];

   console.log('orgDomain==>'+orgDomain);

    http.get(self.urls.user).catch(function(err){
        if(err.statusCode == 401){
            var toParse = err.response.headers['www-authenticate'];
            var Oauth = toParse.match(/https:\/\/[\d\w\./_-]+/i)[0];
        console.log('Oauth==>'+Oauth);
            var loginPost = {
                grant_type: 'password',
                username: self.username,
                password: self.password
            };

            return http.post(Oauth, {form:loginPost});
        }
    }).then(function(data){
        if(data) {
            var parsed = JSON.parse(data);
            //setup authorization
            http = http.defaults({
                headers: {Authorization: parsed.token_type + ' ' + parsed.access_token}
            });
            return http.get(self.urls.user);
        }
    }).then(function(data){
        //check for split-domain scenario
        var parsed = JSON.parse(data);
        var domain = parsed._links.self.href.match(/https:\/\/([\w\d\.]+)/i)[0];

    console.log('[1] '+orgDomain);
        console.log('[2] '+domain);

        if(domain!== orgDomain){
            //split domain scenario
            self.urls.user = self.urls.user.replace(orgDomain, domain);
            http = http.defaults({
                headers: {Authorization: null}
            });

            self._authorize();
        } else { //create app
            var parsed = JSON.parse(data);
            self.urls.applications = parsed._links.applications.href;

            var registerApp = {
                culture : "en-us",
                endpointId : "2d9dc28d-4673-4035-825c-feb64be28e4e",
                userAgent : "NodeJs client"
            };
            return http.post(self.urls.applications, {body: registerApp,json:true});
        }
    }).then(function(app){//start message api

        console.log("inside start message api=>"+app);

    var parsed = JSON.parse(app);


    self.urls.startmessege=parsed._embedded.communication._links.startMessaging.href;


    var startMessage = 

       {
         "importance":"Normal",
        "sessionContext":"33dc0ef6-0570-4467-bb7e-49fcbea8e944",
        "subject":"Task Sample",
        "telemetryId":null,
        "to":"sip:lenea@contoso.com",
        "operationId":"5028e824-2268-4b14-9e59-1abad65ff39"

            };


return http.post(self.urls.startmessege, {body: startMessage, json:true});


    }).then(function(app){//events api
        console.log("inside events api=>"+app);
        var parsed = JSON.parse(app);
        self.urls.events=parsed._links.events.href;
        return http.get(self.urls.events);


}).then(function(app){//events next  api

    console.log("inside events next  api=>"+app);

        var parsed = JSON.parse(app);
        self.urls.events.next=parsed._links.next.href;
        return http.get(self.urls.events.next);


}).then(function(app){//send message  api

        console.log("inside send message  api=>"+app);
        var parsed = JSON.parse(app);
        self.urls.sendmessage=parsed._embedded.messaging._links.sendMessage.href;
        self.urls.sendmessage=self.urls.sendmessage+"?OperationContext=5028e824-2268-4b14-9e59-1abad65ff39"
        var message= 


        {
         "data":"Hello World",
        };


        return http.post(self.urls.sendmessage, {body: message, json:true});

}).then(function(app){

     console.log(app);
});

};

lync.setup = function(email, password){
    var self = this;
    var hostname = email.split('@');
    this.username = email;
    this.password = password;

    //discover urls
    return http.get('http://lyncdiscover.'+hostname[1])
        .then(function(d) {
            var parsed = JSON.parse(d);
            self.urls = {
                self: parsed._links.self.href,
                user: parsed._links.user.href,
                xframe: parsed._links.xframe.href
            };

        console.log('self.urls.user==>'+self.urls.user);
        console.log('self.urls.self==>'+self.urls.self);
        console.log('self.urls.xframe==>'+self.urls.xframe);

        return self._authorize();


        });

};



//run app
lync.setup('username@domain.com', 'password');

    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

Solution

  • It looks like you are handling events correctly, but you are not parsing the event results to see if anything has happened since the last cycle. I would suggest looking over the documentation (GettingStarted-Events) to make sure to handle special cases as well.

    Your request to startMessaging looks fine, but without actually looking at the events coming from the event channel it is hard to know that you are connected properly and that the other side accepted the invitation. You would see a messagingInvitation in the event channel that should indicate whether or not is was accepted/declined/other failure.

    Your request to sendMessage is incorrect as you are not able to send JSON-style requests, but rather text/plain or text/html. Where the body of the message would be the (un)formatted text to send. If you correct that you might be able to send a message.

    When you get that fixed and wonder how to process received messages take a look at the documentation for message as you will be again required to process the data in the event channel.