我正在尝试使用节点 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');
});