-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
rodrigok
merged 9 commits into
RocketChat:develop
from
hmagarotto:livechat-messages-api
Mar 27, 2018
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16e96ed
Added api to enable external system sends visitor message
hmagarotto e57a6f9
Merge branch 'develop' into livechat-messages-api
rodrigok 322b09d
Added validations for "messages" body param
hmagarotto eb2627c
Picked only relevant fields for status result
hmagarotto 30c0e89
Merge branch 'livechat-messages-api' of github.com:hmagarotto/Rocket.…
hmagarotto 1ee167d
Removed status endpoint and changed authentication method
hmagarotto 57db7a3
Added name and status to user payload
hmagarotto 5e0444b
Added visitors endpoint
hmagarotto 976b092
Fixing endpoint to use require authentication
hmagarotto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
packages/rocketchat-livechat/imports/server/rest/messages.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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 | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/rocketchat-livechat/imports/server/rest/visitors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this.bodyParams.visitor.token
doesn't work. You should use camelCased in the field names, such asthis.bodyParams.visitorToken
.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.
@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 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.
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.
Yes, we tried it. We are using a body parameter (JSON object) not a query parameter. And this format is to access field
token
insidevisitor
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.
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.
If you want to try, here is the whole script:
https://gist.github.com/hmagarotto/6251f86e1f00035cc249c8ef05238a90
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.
OK, sorry. I was testing this in another way.