parent
d584e0e093
commit
725ca76f8e
@ -0,0 +1,15 @@ |
||||
<template name="externalSearch"> |
||||
<div class="content"> |
||||
<div class="title"> |
||||
<h2>{{_ "Knowledge_Base"}}</h2> |
||||
</div> |
||||
<div class="external-messages"> |
||||
{{#each messages}} |
||||
<div class="external-message"> |
||||
<a class="pick-message"><i class="icon-edit"></i></a> |
||||
{{msg}} |
||||
</div> |
||||
{{/each}} |
||||
</div> |
||||
</div> |
||||
</template> |
||||
@ -0,0 +1,23 @@ |
||||
Template.externalSearch.helpers({ |
||||
messages() { |
||||
console.log('messages helper'); |
||||
return RocketChat.models.LivechatExternalMessage.findByRoomId(this.rid, { ts: 1 }); |
||||
} |
||||
}); |
||||
|
||||
Template.externalSearch.events({ |
||||
'click a.pick-message'(event, instance) { |
||||
event.preventDefault(); |
||||
|
||||
$('#chat-window-' + instance.roomId + ' .input-message').val(this.msg).focus(); |
||||
} |
||||
}); |
||||
|
||||
Template.externalSearch.onCreated(function() { |
||||
this.roomId = null; |
||||
// console.log('externalSearch.this ->',this);
|
||||
this.autorun(() => { |
||||
this.roomId = Template.currentData().rid; |
||||
this.subscribe('livechat:externalMessages', Template.currentData().rid); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,62 @@ |
||||
/* globals HTTP, SystemLogger */ |
||||
|
||||
var knowledgeEnabled = false; |
||||
var apiaiKey = ''; |
||||
var apiaiLanguage = 'en'; |
||||
RocketChat.settings.get('Livechat_Knowledge_Enabled', function(key, value) { |
||||
knowledgeEnabled = value; |
||||
}); |
||||
RocketChat.settings.get('Livechat_Knowledge_Apiai_Key', function(key, value) { |
||||
apiaiKey = value; |
||||
}); |
||||
RocketChat.settings.get('Livechat_Knowledge_Apiai_Language', function(key, value) { |
||||
apiaiLanguage = value; |
||||
}); |
||||
|
||||
RocketChat.callbacks.add('afterSaveMessage', function(message, room) { |
||||
// skips this callback if the message was edited
|
||||
if (message.editedAt) { |
||||
return message; |
||||
} |
||||
|
||||
if (!knowledgeEnabled) { |
||||
return message; |
||||
} |
||||
|
||||
if (!(typeof room.t !== 'undefined' && room.t === 'l' && room.v && room.v.token)) { |
||||
return message; |
||||
} |
||||
|
||||
// if the message hasn't a token, it was not sent by the visitor, so ignore it
|
||||
if (!message.token) { |
||||
return message; |
||||
} |
||||
|
||||
Meteor.defer(() => { |
||||
try { |
||||
const response = HTTP.post('https://api.api.ai/api/query?v=20150910', { |
||||
data: { |
||||
query: message.msg, |
||||
lang: apiaiLanguage |
||||
}, |
||||
headers: { |
||||
'Content-Type': 'application/json; charset=utf-8', |
||||
'Authorization': 'Bearer ' + apiaiKey |
||||
} |
||||
}); |
||||
|
||||
if (response.data && response.data.status.code === 200 && !_.isEmpty(response.data.result.fulfillment.speech)) { |
||||
RocketChat.models.LivechatExternalMessage.insert({ |
||||
rid: message.rid, |
||||
msg: response.data.result.fulfillment.speech, |
||||
orig: message._id, |
||||
ts: new Date() |
||||
}); |
||||
} |
||||
} catch (e) { |
||||
SystemLogger.error('Error using Api.ai ->', e); |
||||
} |
||||
}); |
||||
|
||||
return message; |
||||
}, RocketChat.callbacks.priority.LOW); |
||||
@ -0,0 +1,15 @@ |
||||
class LivechatExternalMessage extends RocketChat.models._Base { |
||||
constructor() { |
||||
super(); |
||||
this._initModel('livechat_external_message'); |
||||
} |
||||
|
||||
// FIND
|
||||
findByRoomId(roomId, sort = { ts: -1 }) { |
||||
const query = { rid: roomId }; |
||||
|
||||
return this.find(query, { sort: sort }); |
||||
} |
||||
} |
||||
|
||||
RocketChat.models.LivechatExternalMessage = new LivechatExternalMessage(); |
||||
@ -0,0 +1,3 @@ |
||||
Meteor.publish('livechat:externalMessages', function(roomId) { |
||||
return RocketChat.models.LivechatExternalMessage.findByRoomId(roomId); |
||||
}); |
||||
Loading…
Reference in new issue