0

我正在尝试使用节点 js 和 ucwa 创建聊天应用程序。我想从节点 js 应用程序向注册的 lync 用户发送消息。为此,我正在使用 ucwa。我能够通过调用 ucwa apis 进行身份验证、授权和创建应用程序之后我需要一些帮助来向注册的 lync 用户发送即时消息。有人可以帮我提供一些代码示例吗?

我正在使用UCWA Lync 身份验证中的代码 - 500 网络票证无效。在这一步之后如何通过节点 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');
});
4

1 回答 1

1

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

您对 startMessaging 的请求看起来不错,但如果不实际查看来自事件频道的事件,很难知道您已正确连接并且对方接受了邀请。您将在事件通道中看到一个消息传递邀请,该消息应指示是否被接受/拒绝/其他失败。

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

当您解决此问题并想知道如何处理收到的消息时,请查看消息文档,因为您将再次被要求处理事件通道中的数据。

于 2016-04-22T20:27:23.723 回答