Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Livechat messages rest APIs #10054

Merged
merged 9 commits into from
Mar 27, 2018
64 changes: 64 additions & 0 deletions packages/rocketchat-livechat/imports/server/rest/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import LivechatVisitors from '../../../server/models/LivechatVisitors';

RocketChat.API.v1.addRoute('livechat/messages', { authRequired: true }, {
post() {
if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) {
return RocketChat.API.v1.unauthorized();
}

if (!this.bodyParams.visitor) {
return RocketChat.API.v1.failure('Body param "visitor" is required');
}
if (!this.bodyParams.visitor.token) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.bodyParams.visitor.token doesn't work. You should use camelCased in the field names, such as this.bodyParams.visitorToken.

Copy link
Contributor Author

@hmagarotto hmagarotto Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@renatobecker the payload used in message post was based in the payload send to webhook.

This is the expected POST for this endpoint:

curl -v -H "Content-type: application/json" -H "${H_USERID}" -H "${H_AUTHTOKEN}" "http://localhost:3000/api/v1/livechat/messages" -d'{
  "visitor": {
    "token":"04aa15e2-31ed-11e8-ac97-93976ab3c67b",
    "name": "Guest Name",
    "email": "guest@mail.com",
    "phone": {
        "number": "+5511999999999"
    }
  },
  "messages": [
    {
       "msg": "Hi!!"
    }
  ]
}'

Webhook payloads: https://rocket.chat/docs/administrator-guides/livechat/

Copy link

@renatobecker-zz renatobecker-zz Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried your code? We are talking about the name of a query parameter and the format you are using is not valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we tried it. We are using a body parameter (JSON object) not a query parameter. And this format is to access field token inside visitor inside request body (application/json). This is why "visitor.token"

The "curl" below is working and will create a new guest as shown in the screenshot.

curl -s -H "Content-type: application/json" -H "${H_USERID}" -H "${H_AUTHTOKEN}" "http://localhost:3000/api/v1/livechat/messages" -d'{
  "visitor": {
    "token":"04aa15e2-31ed-11e8-ac97-93976ab3c67b",
    "name": "Guest Name",
    "email": "guest@mail.com",
    "phone": {
        "number": "+5511999999999"
    }
  },
  "messages": [
    {
       "msg": "Hi!!"
    }
  ]
}'

screenshot-new-guest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to try, here is the whole script:
https://gist.github.com/hmagarotto/6251f86e1f00035cc249c8ef05238a90

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sorry. I was testing this in another way.

return RocketChat.API.v1.failure('Body param "visitor.token" is required');
}
if (!this.bodyParams.messages) {
return RocketChat.API.v1.failure('Body param "messages" is required');
}
if (!(this.bodyParams.messages instanceof Array)) {
return RocketChat.API.v1.failure('Body param "messages" is not an array');
}
if (this.bodyParams.messages.length === 0) {
return RocketChat.API.v1.failure('Body param "messages" is empty');
}

const visitorToken = this.bodyParams.visitor.token;

let visitor = LivechatVisitors.getVisitorByToken(visitorToken);
let rid;
if (visitor) {
const rooms = RocketChat.models.Rooms.findOpenByVisitorToken(visitorToken).fetch();
if (rooms && rooms.length > 0) {
rid = rooms[0]._id;
} else {
rid = Random.id();
}
} else {
rid = Random.id();
const visitorId = RocketChat.Livechat.registerGuest(this.bodyParams.visitor);
visitor = LivechatVisitors.findOneById(visitorId);
}

const sentMessages = this.bodyParams.messages.map((message) => {
const sendMessage = {
guest: visitor,
message: {
_id: Random.id(),
rid,
token: visitorToken,
msg: message.msg
}
};
const sentMessage = RocketChat.Livechat.sendMessage(sendMessage);
return {
username: sentMessage.u.username,
msg: sentMessage.msg,
ts: sentMessage.ts
};
});

return RocketChat.API.v1.success({
messages: sentMessages
});
}
});
2 changes: 1 addition & 1 deletion packages/rocketchat-livechat/imports/server/rest/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RocketChat.API.v1.addRoute('livechat/users/:type', { authRequired: true }, {
const users = RocketChat.authz.getUsersInRole(role);

return RocketChat.API.v1.success({
users: users.fetch().map(user => ({ _id: user._id, username: user.username }))
users: users.fetch().map(user => _.pick(user, '_id', 'username', 'name', 'status', 'statusLivechat'))
});
} catch (e) {
return RocketChat.API.v1.failure(e.error);
Expand Down
32 changes: 32 additions & 0 deletions packages/rocketchat-livechat/imports/server/rest/visitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import LivechatVisitors from '../../../server/models/LivechatVisitors';

RocketChat.API.v1.addRoute('livechat/visitor/:visitorToken', { authRequired: true }, {
get() {
if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) {
return RocketChat.API.v1.unauthorized();
}

const visitor = LivechatVisitors.getVisitorByToken(this.urlParams.visitorToken);
return RocketChat.API.v1.success(visitor);
}
});

RocketChat.API.v1.addRoute('livechat/visitor/:visitorToken/room', { authRequired: true }, {
get() {
if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) {
return RocketChat.API.v1.unauthorized();
}

const rooms = RocketChat.models.Rooms.findOpenByVisitorToken(this.urlParams.visitorToken, {
fields: {
name: 1,
t: 1,
cl: 1,
u: 1,
usernames: 1,
servedBy: 1
}
}).fetch();
return RocketChat.API.v1.success({ rooms });
}
});
2 changes: 2 additions & 0 deletions packages/rocketchat-livechat/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import '../imports/server/rest/departments.js';
import '../imports/server/rest/facebook.js';
import '../imports/server/rest/sms.js';
import '../imports/server/rest/users.js';
import '../imports/server/rest/messages.js';
import '../imports/server/rest/visitors.js';