Skip to content
Snippets Groups Projects
Commit 5b93937f authored by Diego Sampaio's avatar Diego Sampaio
Browse files

using processWebhookMessage on V1 APIs

parent 841656b5
No related branches found
No related tags found
No related merge requests found
......@@ -54,74 +54,21 @@ RocketChat.API.v1.addRoute 'chat.messageExamples', authRequired: true,
# Send Channel Message
RocketChat.API.v1.addRoute 'chat.postMessage', authRequired: true,
post: ->
channel = @bodyParams.channel
channelType = channel[0]
channel = channel.substr(1)
switch channelType
when '#'
room = RocketChat.models.Rooms.findOne
$or: [
{_id: channel}
{name: channel}
]
if not room?
return RocketChat.API.v1.failure 'invalid-channel'
rid = room._id
if room.t is 'c'
Meteor.runAsUser @userId, ->
Meteor.call 'joinRoom', room._id
when '@'
roomUser = RocketChat.models.Users.findOne
$or: [
{_id: channel}
{username: channel}
]
if not roomUser?
return RocketChat.API.v1.failure 'invalid-channel'
rid = [@useId, roomUser._id].sort().join('')
room = RocketChat.models.Rooms.findOne(rid)
if not room
Meteor.runAsUser @userId, ->
Meteor.call 'createDirectMessage', roomUser.username
room = RocketChat.models.Rooms.findOne(rid)
else
return RocketChat.API.v1.failure 'invalid-channel-type'
message =
alias: @bodyParams.username or @bodyParams.alias
msg: _.trim(@bodyParams.text or @bodyParams.msg or '')
attachments: @bodyParams.attachments
parseUrls: false
bot:
try
@bodyParams.bot =
u: @userId
groupable: false
if @bodyParams.icon_url? or @bodyParams.avatar?
message.avatar = @bodyParams.icon_url or @bodyParams.avatar
else if @bodyParams.icon_emoji? or @bodyParams.emoji?
message.emoji = @bodyParams.icon_emoji or @bodyParams.emoji
if _.isArray message.attachments
for attachment in message.attachments
if attachment.msg
attachment.text = _.trim(attachment.msg)
delete attachment.msg
messageReturn = processWebhookMessage @bodyParams, @user
message = RocketChat.sendMessage @user, message, room, {}
return RocketChat.API.v1.success
ts: Date.now()
channel: channel
message: message
if not messageReturn?
return RocketChat.API.v1.failure 'unknown-error'
return RocketChat.API.v1.success
ts: Date.now()
channel: messageReturn.channel
message: messageReturn.message
catch e
return RocketChat.API.v1.failure e.error
# Set Channel Topic
RocketChat.API.v1.addRoute 'channels.setTopic', authRequired: true,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment