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
71 changes: 71 additions & 0 deletions packages/rocketchat-livechat/imports/server/rest/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import LivechatVisitors from '../../../server/models/LivechatVisitors';

function checkAuthentication(request) {
const receivedToken = request.headers['x-rocketchat-livechat-token'];
const secretToken = RocketChat.settings.get('Livechat_secret_token');
return receivedToken && receivedToken === secretToken;
}

RocketChat.API.v1.addRoute('livechat/status/:visitorToken', {
get() {
if (!checkAuthentication(this.request)) {
return RocketChat.API.v1.unauthorized();
}

const info = Meteor.call('livechat:getInitialData', this.urlParams.visitorToken);
Copy link
Member

Choose a reason for hiding this comment

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

Which data you want to get here? This method is returning a lot of data not related to visitor's status

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rodrigok we change this to pick only the fields below from initialData result:

  • visitor
  • room
  • departments
  • online
  • agentData

return RocketChat.API.v1.success(info);
}
});

RocketChat.API.v1.addRoute('livechat/messages', {

Choose a reason for hiding this comment

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

You are using RocketChat.authz.hasPermission, so you have to set { authRequired: true } to the route parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @renatobecker , I fixed this.

post() {
if (!checkAuthentication(this.request)) {
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');
}
const visitorToken = this.bodyParams.visitor.token;
Copy link
Member

Choose a reason for hiding this comment

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

Should check this.bodyParams.messages as an array and if not empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rodrigok , we added some validations to messages param according to suggestions


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
});
}
});
1 change: 1 addition & 0 deletions packages/rocketchat-livechat/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ 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';