Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
teresan committed May 26, 2020
0 parents commit c2768bf
Show file tree
Hide file tree
Showing 8 changed files with 1,126 additions and 0 deletions.
Empty file added .gitignore
Empty file.
30 changes: 30 additions & 0 deletions functions/findExpert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
exports.handler = function(context, event, callback) {
const topic = event.topic;
const channelSid = event.channelSid;
const expertList = [];

const axios = require('axios');
axios.get('https://iris-bobcat-6430.twil.io/experts/list', {
service: 'IS9115e5981568c4e980c1711e0b691dde'
})
.then(response => {
const data = JSON.parse(response.data);

response.forEach(expert => {
if (expert.topics.find(topic) !== undefined) {

context.getTwilioClient().chat.services(context.TWILIO_EXPERT_CHAT_SID)
.channels(channelSid)
.members
.create({identity: expert.name})
.then(member => console.log(member.sid))
.catch(error => callback(error));

}
})
})
.catch(error => callback(error));

console.log(expertList);
callback(null, expertList);
};
40 changes: 40 additions & 0 deletions functions/listTopics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
exports.handler = function(context, event, callback) {

const keywords = event.keywords;

let originalTopics = [];
if (keywords.search(',') >= 0) {
originalTopics = keywords.split(',').filter(e => e !== '');
} else if (keywords.search(' ') >= 0) {
originalTopics = keywords.split(' ').filter(e => e !== '');
} else {
originalTopics.push(keywords.trim());
}

console.log(originalTopics);

processAllTopics(context.getTwilioClient(), originalTopics)
.then(processedTopics => {
console.log('processedTopics:' + processedTopics)
callback(null, processedTopics);
});

};

const getTaskName = (client, topic) => {
let taskName = client.autopilot.assistants(context.TWILIO_AUTOPILOT_ASSISTANT_SID)
.queries
.create({ language: 'en-US', query: topic.trim() })
.then(query => {
return query.results.task;
});
return Promise.resolve(taskName);
};

function processTopic(client, topic) {
return Promise.resolve(getTaskName(client, topic));
}

function processAllTopics(client, originalTopics) {
return Promise.all(originalTopics.map(topic => processTopic(client, topic)));
}
27 changes: 27 additions & 0 deletions functions/saveExpert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
exports.handler = function(context, event, callback) {

//Build expert json
var payload = {
name: event.name,
number: event.number,
topics: event.topics
};

console.log(payload);

//Submit to expert hub API
const axios = require('axios');

axios.post('https://iris-bobcat-6430.twil.io/experts/add', {
service: 'IS9115e5981568c4e980c1711e0b691dde',
expert: JSON.stringify(payload)
})
.then(response => {
callback(event.topics);
})
.catch(error => {
callback(error);
});

};

Loading

0 comments on commit c2768bf

Please sign in to comment.