Open krishnaprasadhg opened 5 years ago
What happens after calling room.transfer
endpoint?
Did you get any error?
I'm getting the below error while getting the X-Auth-Token (curl" http://localhost:3000/api/v1/login -d "user=ServiceDesk&password=ServiceDesk")
Here ServiceDesk is bot. I tried with admin credentials also.. same error...
Error: socket hang up
at createHangUpError (_http_client.js:323:15)
at Socket.socketOnEnd (_http_client.js:426:23)
at Socket.emit (events.js:194:15)
at endReadableNT (_stream_readable.js:1125:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at RedirectableRequest.
I'm getting the below error while getting the X-Auth-Token (curl" http://localhost:3000/api/v1/login -d "user=ServiceDesk&password=ServiceDesk")
The error is not related to SDK stuff, right?
Hello,
I'm trying to transfer or forward the session to Live Agent. But session is not getting transferred. Please help me. bot Source code is as below.
I'm calling APIs to create a live chat room with live agent(which is successful), but forward or transfer session is not happening.
const { driver } = require('@rocket.chat/sdk'); // customize the following with your server and BOT account information const HOST = 'localhost:3000'; const USER = 'ServiceDesk'; const PASS = 'ServiceDesk'; const BOTNAME = 'ServiceDesk'; // name bot response to const SSL = false; // server uses https ? const ROOMS = ['GENERAL'];
var myuserid; // this simple bot does not handle errors, different message types, server resets // and other production situations
var Client = require('node-rest-client').Client; var client = new Client(); var regVisitorToken, randomToken, visitorId, visitorToken, roomId, xuserid, xauthtoken, endUserName, messageUserId;
function generateRandomToken(length) { var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; }
//Getting initial data with random string const startSessionTransfer = async () => { randomToken = generateRandomToken(18); console.log('randomToken : '+randomToken); var url = "http://localhost:3000/api/v1/livechat/visitor/"+randomToken; const dw = await client.get(url, function (data, response) { // parsed response body as js object console.log(data); regVisitorToken = data.success; const regVistitor = registerVisitor(); }); }
const registerVisitor = async () =>{ if(regVisitorToken == true) { var args = { data: {"visitor":{
"name": endUserName, "email":"EndUser@gmail.com", "token": randomToken, "phone":"55 51 5555-5555", "customFields":[
{ "key":"address", "value":"Rocket.Chat street", "overwrite":true } ] }}, headers: { "Content-Type": "application/json" } };
}
const openNewChatRoom = async () =>{ var url = "http://localhost:3000/api/v1/livechat/room?token="+visitorToken; client.get(url, function (data, response) { console.log(data); roomId = data.room._id; console.log("Opening new room with live chat Agent, roomId: "+ roomId); const sendMessage = sendMsgToLiveChatRoom(); }); }
const sendMsgToLiveChatRoom = async () =>{ var args = { data: { "token": randomToken, "rid": roomId, "msg": "Trasfferring session from bot..." }, headers: { "Content-Type": "application/json" } }; client.post("http://localhost:3000/api/v1/livechat/message", args, function (data, response) { console.log(data); const forwardSession = getHeaderAuthToken(); }); }
const getHeaderAuthToken = async () =>{ var args = { data: { "user": "ServiceDesk", "password": "ServiceDesk" }, headers: { "Content-Type": "application/json" } }; client.post("http://localhost:3000/api/v1/login", args, function (data, response) { console.log(data); console.log("Getting Auth tokens: "+data.status); xuserid = data.data.userId; xauthtoken = data.data.authToken; const forwardSession = forwardSessionToLiveAgent();
}
const forwardSessionToLiveAgent = async () =>{ var args = { data: { "rid": roomId, //"token": visitorToken, "department": "mYL3qbjJgNg7wcihf" }, headers: { "Content-Type": "application/json", "X-Auth-Token":xauthtoken, "X-User-Id":xuserid } }; client.post("http://localhost:3000/api/v1/livechat/room.forward", args, function (data, response) { console.log(data); console.log("Forwarding session to live agent: "+data.success); }); }
const runbot = async () => { const conn = await driver.connect( { host: HOST, useSsl: SSL}) myuserid = await driver.login({username: USER, password: PASS}); const roomsJoined = await driver.joinRooms(ROOMS); console.log('joined rooms');
}
// callback for incoming messages filter and processing const processMessages = async(err, message, messageOptions) => { messageUserId = message.u._id; if (!err) { console.log('Getting messages : '+message.u._id +' -'+ message.msg+' -'+ message.rid); // filter our own message if (message.u._id === myuserid) return; // can filter further based on message.rid const roomname = await driver.getRoomName(message.rid); console.log('User - ' + message.u._id + ' roomname - ' + roomname); if (message.msg.startsWith(BOTNAME)) { //Transfer session if(message.msg.length > 20){ const transferSession = startSessionTransfer(); return; } console.log('Responding to user'); endUserName = message.u.username; const response = message.u.username + ', how can ' + BOTNAME + ' help you with ' + message.msg.substr(BOTNAME.length + 1); const sentmsg = driver.sendDirectToUser(response, message.u.username)
} }
runbot()