Major Check in - Git and Recast Integrated, create repository and issue functional via UI

This commit is contained in:
2018-07-19 14:23:15 +05:30
parent a5996f1f2d
commit 76aca797d6
18 changed files with 2007 additions and 283 deletions

57
src/js/bot/bot.js Normal file
View File

@@ -0,0 +1,57 @@
/*
* bot.js
*
* In this file:
* - received message from a connected channel will be transformed with Recast.AI SDK
* - received message from test command will be processed by Recast.AI
* You can run this command for testing:
* curl -X "POST" "http://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8"
*
*
* The Recast.AI SDK will handle the message and call your reply bot function (ie. replyMessage function)
*/
const recastai = require('recastai').default
const config = require('./config.js');
// Instantiate Recast.AI SDK
const client = new recastai(config.recasttoken);
/*
* Callback for BotConnector messages
* Parameters:
* - message: Message received from BotConnector
*/
const replyMessage = message => {
// Get text from message received
const text = message.content
console.log('I receive: ', text)
return client.request.analyseText(text)
.then(nlp => {
let reply = 'I\'m sorry but I don\'t understand what you are talking about.'
const intent = nlp.intent()
if (intent) {
reply = `I understand that you talk about ${intent.slug}.`
}
message.addReply({ type: 'text', content: reply })
return message.reply().then(p => p.body)
})
}
/*
* Main bot function
* Parameters are:
* - body: Request body
* - response: Response of your server (can be a blank object if not needed: {})
*/
const reply = (request, response) => {
return client.connect.handleMessage(request, response, replyMessage)
}
module.exports = {
reply,
}

3
src/js/bot/config.js Normal file
View File

@@ -0,0 +1,3 @@
module.exports = {
recasttoken: "26021d055040a9d9f1ad48476efab4a0"
}

88
src/js/bot/recast-ops.js Normal file
View File

@@ -0,0 +1,88 @@
const config = require('./config.js');
const Microbot = require("./../microbot-ops.js");
const app = new Microbot();
const DomManipulator = require('./../dom-ops.js');
const domManipulator = new DomManipulator();
module.exports = class Recast {
constructor() {
this.recastToken = config.recasttoken;
this.requestUrl = "https://api.recast.ai/v2/request";
}
getAndCallProcessIntent(command, text) {
self = this;
var url = this.requestUrl + "?text=" + command;
var bodyRelevant = '';
var intent = '';
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Token " + this.recastToken
},
data: text
})
.then(function(response) {
response.json().then(function(body) {
bodyRelevant = body.results;
intent = bodyRelevant.intents[0]["slug"];
if(intent !== undefined) {
domManipulator.showWidget(intent);
domManipulator.populateRecastData(intent, bodyRelevant);
// self.processIntent(intent);
}
return intent;
});
})
.catch(function(error) {
console.error('Fetch Error =\n', error);
});
};
processIntent(intent) {
switch(intent) {
case "createrepo":
app.createRepository();
break;
case "updaterepo":
app.updateRepository();
break;
case "viewrepos":
app.viewRepositories();
break;
case "deleterepo":
app.deleteRepository();
break;
case "createissue":
app.createIssue();
break;
case "updateissue":
app.updateIssue();
break;
case "closeissue":
app.closeIssue();
break;
case "reopenissue":
app.reopenIssue();
break;
case "displayissue":
app.displayIssue();
break;
case "addissuecomment":
app.addIssueComment();
break;
case "displaylastcomment":
app.displayLastComment();
break;
case "addcollab":
app.addCollaborator();
break;
case "removecollab":
app.removeCollaborator();
break;
default:
console.log("default");
}
}
}

47
src/js/bot/server.js Normal file
View File

@@ -0,0 +1,47 @@
/*
* server.js
* This file is the core of your bot
*
* It creates a little server using express
* So, your bot can be triggered throught "/" route
*
* This file was made for locally testing your bot
* You can test it by running this command
* curl -X "POST" "http://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8"
* You might modify the server port ^^^^ depending on your configuration in config.js file
*/
const express = require('express')
const bodyParser = require('body-parser')
// Load configuration
require('./config')
const bot = require('./bot')
// Start Express server
const app = express()
app.set('port', process.env.PORT || 5000)
app.use(bodyParser.json())
// Handle / route
app.use('/', (request, response) => {
bot.reply(request, response)
.then(success => {
console.log(success)
if (!response.headersSent) { response.status(200) }
}).catch(error => {
console.log('Error in your bot:', error)
if (!response.headersSent) { response.sendStatus(400) }
})
})
if (!process.env.REQUEST_TOKEN) {
console.log('ERROR: process.env.REQUEST_TOKEN variable in src/config.js file is empty ! You must fill this field with the request_token of your bot before launching your bot locally')
process.exit(0)
} else {
// Run Express server, on right port
app.listen(app.get('port'), () => {
console.log('Our bot is running on port', app.get('port'))
})
}