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 webhook request on message #9870

Merged

Conversation

hmagarotto
Copy link
Contributor

@RocketChat/core

This PR added a new event to notifiy using livechat webhook (CRM url).

The new events are:

  • Send webhook request when agent sends message
  • Send webhook request when visitor sends message

The payloads used here are the same for the other webhook events (https://rocket.chat/docs/administrator-guides/livechat/#integrations)
The "messages" attribute will contains only the last message.

Screenshot with these new webhook events options:

screenshot_rc_crm

@CLAassistant
Copy link

CLAassistant commented Feb 23, 2018

CLA assistant check
All committers have signed the CLA.

@@ -49,6 +56,36 @@ RocketChat.callbacks.add('livechat.saveInfo', (room) => {
return sendToCRM('LivechatEdit', room);
}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-save-info');

RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
// skips this callback if the message was edited
if (message.editedAt) {
Copy link
Member

Choose a reason for hiding this comment

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

should message editings be sent as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that it could be sent with an indicator to show that message was edited. So, if necessary, we can ignore edited message when the original message was already forward to a channel without edition/update feature.
What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I agree. the test on the other could be the same to see if the message was edited 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sampaiodiego I think if we will send edited messages we need to send messages ID too. This is to allow the external system make a relationship between original and edited message.
Right?

Follow two examples of payload with "editedAt" and "_id" fields in message item.

A payload example of an original message:

{"_id":"T87nT6cjn8N2BG5X4","label":"HHHH2","code":55,"createdAt":"2018-03-01T19:52:37.522Z","lastMessageAt":"2018-03-09T18:14:55.015Z","visitor":{"_id":"n3Q4Lb29zW9pHpYy4","token":"aaaaaaaaa123ddd456ddd","name":"HHHH2","username":"guest-54","email":[{"address":"[email protected]"}],"phone":null},"agent":{"_id":"a7srSSKZ463Mc5Skw","username":"henrique.magarotto","name":"henrique","email":"[email protected]"},"type":"Message","messages":[{"_id":"o3FaFsuMdmn5J7QCz","username":"henrique.magarotto","msg":"Test message","ts":"2018-03-09T18:18:19.654Z","agentId":"a7srSSKZ463Mc5Skw"}]}

A payload example of an edited message:

{"_id":"T87nT6cjn8N2BG5X4","label":"HHHH2","code":55,"createdAt":"2018-03-01T19:52:37.522Z","lastMessageAt":"2018-03-09T18:18:19.654Z","visitor":{"_id":"n3Q4Lb29zW9pHpYy4","token":"aaaaaaaaa123ddd456ddd","name":"HHHH2","username":"guest-54","email":[{"address":"[email protected]"}],"phone":null},"agent":{"_id":"a7srSSKZ463Mc5Skw","username":"henrique.magarotto","name":"henrique","email":"[email protected]"},"type":"Message","messages":[{"_id":"o3FaFsuMdmn5J7QCz","username":"henrique.magarotto","msg":"Test message!","ts":"2018-03-09T18:18:19.654Z","editedAt":"2018-03-09T18:18:51.485Z","agentId":"a7srSSKZ463Mc5Skw"}]}

Copy link
Member

Choose a reason for hiding this comment

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

right. I think _id is already included.. if we send edited messages, editedAt will be sent as well 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sampaiodiego, _id and editedAt fields of messages was not sent in request JSON as you can see here: https://rocket.chat/docs/administrator-guides/livechat/#integrations
With the changes, this fields will start being sent.

@sampaiodiego
Copy link
Member

it is becoming even more clear that CRM integration should have been done on top of "outgoing webhooks", this way we could only create events to allow integrations listen to.

I think we'll move this way in the near future.

}

if (messages) {
messages.forEach((message) => {
if (message.t) {
return;
}
const msg = {
Copy link
Contributor Author

@hmagarotto hmagarotto Mar 9, 2018

Choose a reason for hiding this comment

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

@sampaiodiego here I added _id and editedAt fields. This is the payload sent on webhook request. Now, these new fields will be sent in all events.

// only call webhook if it is a livechat room
if (!(typeof room.t !== 'undefined' && room.t === 'l' && room.v && room.v.token)) {
return message;
}
Copy link
Member

Choose a reason for hiding this comment

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

This will pass if room.t === undefined, I'd suggest this:

if (room.t !== 'l' || room.v == null || room.v.token == null)) {

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, fix was done according to suggestion.
This code is a copy from sendToFacebook.js and sendMessageBySMS.js, maybe these callbacks need the same changes.


sendToCRM('Message', room, [message]);
return message;
}, RocketChat.callbacks.priority.MEDIUM, 'sendMessageToFacebook');
Copy link
Member

Choose a reason for hiding this comment

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

The callback ID/name is wrong sendMessageToFacebook

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, fixed callback ID to livechat-send-crm-message

@rodrigok rodrigok added this to the 0.63.0 milestone Mar 15, 2018
@rodrigok rodrigok merged commit c62364d into RocketChat:develop Mar 15, 2018
@hmagarotto hmagarotto deleted the livechat-webhook-send-on-message branch March 29, 2018 00:01
@rodrigok rodrigok mentioned this pull request Apr 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants