使用节点js和UCWA聊天应用程序

时间:2016-04-13 10:10:03

标签: node.js lync ucwa

我正在尝试使用节点js和ucwa创建聊天应用程序。我想从节点js aplication向已注册的lync用户发送消息。因为我正在使用ucwa。我能够通过身份验证,授权和创建应用程序调用ucwa apis.After之后我需要一些帮助将IM发送给注册的lync用户。可以请一些人帮我一些代码示例吗?

我正在使用UCWA Lync authentication - 500 web ticket is invalid中的代码。 在这一步之后如何通过节点js和UCWA发送IM?

以下是我的代码:

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');
});

1 个答案:

答案 0 :(得分:1)

看起来您正在正确处理事件,但是您没有解析事件结果以查看自上一个周期以来是否发生了任何事情。我建议查看文档(GettingStarted-Events)以确保处理特殊情况。

您对startMessaging的请求看起来很好,但实际上没有查看来自事件通道的事件,很难知道您已正确连接并且另一方接受了邀请。您会在事件渠道中看到messagingInvitation,表明是否接受/拒绝/其他失败。

您对sendMessage的请求不正确,因为您无法发送JSON样式的请求,而是发送text / plain或text / html。消息正文将是要发送的(未)格式化文本。如果你纠正了,你可以发送信息。

当你修复并想知道如何处理收到的消息时,请查看message的文档,因为您将再次需要处理事件通道中的数据。