-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Changes from 2 commits
16e96ed
e57a6f9
322b09d
eb2627c
30c0e89
1ee167d
57db7a3
5e0444b
976b092
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
return RocketChat.API.v1.success(info); | ||
} | ||
}); | ||
|
||
RocketChat.API.v1.addRoute('livechat/messages', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are using There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Webhook payloads: https://rocket.chat/docs/administrator-guides/livechat/ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 The "curl" below is working and will create a new guest as shown in the screenshot.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to try, here is the whole script: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rodrigok , we added some validations to |
||
|
||
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 | ||
}); | ||
} | ||
}); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: