Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
haxorcode committed Nov 21, 2018
0 parents commit 1c8fc00
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
access_token=
public_address=
secret=
trello_key=
trello_token=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db
node_modules
42 changes: 42 additions & 0 deletions components/express_middleware/register_with_studio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var request = require('request');
var debug = require('debug')('botkit:register_with_studio');
module.exports = function(webserver, controller) {
var registered_this_session = false;
controller.registerDeployWithStudio = function(host) {
if (!registered_this_session && controller.config.studio_token) {
var instance = {
url: host,
version: controller.version(),
ts: new Date(),
}
request({
method: 'post',
uri: (controller.config.studio_command_uri || 'https://studio.botkit.ai') + '/api/v1/bots/phonehome?access_token=' + controller.config.studio_token,
form: instance,
},function(err, res, body) {
registered_this_session = true;
if (err) {
debug('Error registering instance with Botkit Studio', err);
} else {
var json =null;
try {
json = JSON.parse(body);
} catch(err) {
debug('Error registering instance with Botkit Studio', err);
}
if (json) {
if (json.error) {
debug('Error registering instance with Botkit Studio', json.error);
}
}
}
});
}
}
if (webserver && controller.config.studio_token) {
webserver.use(function(req, res, next) {
controller.registerDeployWithStudio(req.get('host'));
next();
});
}
}
38 changes: 38 additions & 0 deletions components/express_webserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var express = require('express');
var bodyParser = require('body-parser');
var querystring = require('querystring');
var debug = require('debug')('botkit:webserver');

module.exports = function(controller, bot) {


var webserver = express();
webserver.use(bodyParser.json());
webserver.use(bodyParser.urlencoded({ extended: true }));

// import express middlewares that are present in /components/express_middleware
var normalizedPath = require("path").join(__dirname, "express_middleware");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./express_middleware/" + file)(webserver, controller);
});

webserver.use(express.static('public'));


webserver.listen(process.env.PORT || 3000, null, function() {

console.log('Express webserver configured and listening at http://localhost:' + process.env.PORT || 3000);

});

// import all the pre-defined routes that are present in /components/routes
var normalizedPath = require("path").join(__dirname, "routes");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./routes/" + file)(webserver, controller);
});

controller.webserver = webserver;

return webserver;

}
16 changes: 16 additions & 0 deletions components/mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose');

mongoose.connect(
process.env.mongo_connection,
{ useNewUrlParser: true }
)

mongoose.connection.on('connected', () => {
console.log(`Connected to MongoDB`);
});

mongoose.connection.on('error', err => {
if(err) throw err;
});

module.exports = mongoose;
29 changes: 29 additions & 0 deletions components/plugin_glitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var request = require('request');
module.exports = function(controller) {

function keepalive() {

request({
url: 'http://' + process.env.PROJECT_DOMAIN + '.glitch.me',
}, function(err) {

setTimeout(function() {
keepalive();
}, 55000);

});

}

// if this is running on Glitch
if (process.env.PROJECT_DOMAIN) {

// Register with studio using the provided domain name
controller.registerDeployWithStudio(process.env.PROJECT_DOMAIN + '.glitch.me');

// make a web call to self every 55 seconds
// in order to avoid the process being put to sleep.
keepalive();

}
}
10 changes: 10 additions & 0 deletions components/routes/incoming_webhooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var debug = require('debug')('botkit:incoming_webhooks');

module.exports = function(webserver, controller) {
webserver.post('/ciscospark/receive', function(req, res) {
res.status(200);
res.send('ok');
var bot = controller.spawn({});
controller.handleWebhookPayload(req, res, bot);
});
}
55 changes: 55 additions & 0 deletions components/subscribe_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var request = require('request');
var debug = require('debug')('botkit:subscribe_events');

module.exports = function(controller) {

debug('Subscribing to Cisco webhook events...');

var webhook_name = controller.config.webhook_name || 'Botkit Firehose';

var list = controller.api.webhooks.list().then(function(list) {
var hook_id = null;

for (var i = 0; i < list.items.length; i++) {
if (list.items[i].name == webhook_name) {
hook_id = list.items[i].id;
}
}

var hook_url = 'https://' + controller.config.public_address + '/ciscospark/receive';

debug('Cisco Spark: incoming webhook url is ', hook_url);

if (hook_id) {
controller.api.webhooks.update({
id: hook_id,
resource: 'all',
targetUrl: hook_url,
event: 'all',
secret: controller.config.secret,
name: webhook_name,
}).then(function(res) {
debug('Cisco Spark: SUCCESSFULLY UPDATED CISCO SPARK WEBHOOKS');
}).catch(function(err) {
debug('FAILED TO REGISTER WEBHOOK', err);
throw new Error(err);
});

} else {
controller.api.webhooks.create({
resource: 'all',
targetUrl: hook_url,
event: 'all',
secret: controller.config.secret,
name: webhook_name,
}).then(function(res) {

debug('Cisco Spark: SUCCESSFULLY REGISTERED CISCO SPARK WEBHOOKS');
}).catch(function(err) {
debug('FAILED TO REGISTER WEBHOOK', err);
throw new Error(err);
});

}
});
};
6 changes: 6 additions & 0 deletions components/trello/trello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let Trello = require("trello");

module.exports = function(key, token){
let trello = new Trello(key, token);
return trello;
}
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "webex-chatbot-add-trello-card",
"version": "0.0.1",
"description": "This is a chatbot designed to be able to create new trello cards all through a chatbot in cisco webex.",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"body-parser": "^1.15.2",
"botkit": "^0.6.20",
"botkit-studio-metrics": "^0.0.1",
"debug": "^2.6.0",
"express": "^4.14.0",
"mongoose": "^5.3.9",
"node-env-file": "^0.1.8",
"request": "^2.79.0",
"trello": "^0.9.0",
"wordfilter": "^0.2.6"
},
"devDependencies": {},
"repository": {
"type": "git",
"url": "https://github.com/sagitarious12/webex-chatbot-add-trello-card"
},
"homepage": "https://github.com/sagitarious12/webex-chatbot-add-trello-card",
"keywords": [
"bots",
"chatbots",
"Cisco Spark",
"Webex",
"Trello",
"Cards"
],
"author": "rileyworthen.123@gmail.com",
"license": "ISC"
}
95 changes: 95 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Webex Chatbot Trello Add Card

this package was created with the intent of teaching how to make integrations into Cisco Webex Chat system.

### What does this do?
This specific integration will allow any person to add a card onto a trello board. This bot works by 'listening' for specific keywords and then starting a conversation based on that keyword. Then it will start a conversation in order to gather the required information to be able to create a card on your trello account. When you have completed the small conversation that you have with this chatbot, you will now have a new card on one of your lists in trello.

## Installation

### First
Go to [Botkit studio](https://studio.botkit.ai/login) in order to get an access token to use [BotKit](https://botkit.ai/docs/). Once you get an access token, that needs to be placed in the .env file as the access_token.

![Building a bot with Botkit][Build-A-Bot-Botkit]
![Get the access token from Botkit][botkit-access-key]

### Second
Download the [Cisco Webex Desktop platform](https://www.webex.com/downloads.html) and install and run it. Once you get to the enter your work email address page just type in any email address and continue with creating a password.

![Login page on Webex Platform][webex-opening-screen]

Next thing that you need to do is go to the [Cisco Webex Developer Page](https://developer.webex.com/docs/bots) and log in with the email address that you just created on their desktop platform to register a new bot. After you have finished creating a new bot, make sure that you note the email address that you added for the bot because it will be used later on.

![Webex add bot button][Cisco-Developer-Create-Bot]
![Cisco Developer Create a Bot Form with the email][cisco-developer-create-bot-form-with-email]

### Third
Go to [Trello](https://trello.com/app-key) and make sure that you are logged in and this link will take you to get a key for your application. Also where it says "you can manually generate a Token" click on the link and get a token for this application. Those two fields need to be saved under the trello_key and the trello_token fields of the .env file.

![Trello Generate API key][trello-developer-api-key-with-token-button]

### Fourth
Install [Heroku](https://devcenter.heroku.com/articles/heroku-cli) on your computer. Heroku is a platform that allows you to upload node.js and Express projects to host them as easily as using Git. After installation, go onto your command prompt and navigate to your repository location. Then type "heroku create". It will then generate a public location for your application to be hosted so that Cisco Webex can access it. Place the address that was just created into the public_address field of the .env file.

### Fifth
Fill in the last field in the .env file. the secret field is any password that you want.

### Sixth
Go into your [Heroku Dashboard](https://dashboard.heroku.com/apps) and click on the app that you just created. Go into the settings of the application and click on "Reveal Config Vars" This will be where you need to add each of the fields from the .env file into these locations. Add new fields for each .env line.

![Heroku Config variables][heroku-config]

### Seventh
Go back into the command prompt and type "git push heroku master" this will now upload the app and build it and run it.

### Eighth and Last
Open up the Cisco Webex Platform again. Once it is open, click on the plus button on the top left to the right of the search bar and click on "contact a person". in the search box that appears, you will need to type the email address that you noted down in step one. This will start a new chat with the bot!

![Webex Plus button add a new chat.][webex-add-button]

## Usage

The only thing that this project is currently capable of doing is creating a new card on trello, in order to start the process of creating a card, you need to just type:

```/taskbot```

This will initiate the process of creating a new card on trello and the chatbot will do the rest of the teaching!

## Example Code

this example shows how we can create a conversation with the bot and when they type `/taskbot`, this will then start with the question 'Please enter in a name for the new card' then it will wait for a response. Once you enter in a response, it will continue into the next 'Thread' or question and ask 'Please enter in a description for the new card' then again it will wait for a response. Then it will tell the conversation to go into the next 'Thread' but this time there is something that gets called before the 'completed' thread gets initialized, it will do a `beforeThread` function, where it will use `npm trello` to add a card onto the list that is provided for the `list` variable.

```javascript
convo.addQuestion({text: "Please enter in a name for the new card"}, function(res, convo){
convo.gotoThread('card-description');
}, {key: 'card-name'}, 'default');

convo.addQuestion({text: "Please enter in a description for the new card"}, function(res, convo){
convo.gotoThread('completed');
}, {key: 'card-description'}, 'card-description');

convo.beforeThread('completed', function(convo, next){
let name = convo.extractResponse('card-name');
let description = convo.extractResponse('card-description');
let list = 'ENTER A LIST ID HERE';
let create = trello.addCard(name, description, list);
create.then((res) => {
next();
});
});

convo.addMessage("Congrats you have created a card on trello!", 'completed');
```

## Pull Requests

If you wish to contribute to this package, please clone the repository and create a pull request. Thank you so much!

[botkit-access-key]: https://worthenwebcom.files.wordpress.com/2018/11/botkit-access-key.png
[Build-A-Bot-Botkit]: https://worthenwebcom.files.wordpress.com/2018/11/build-a-bot-botkit.png
[Cisco-Developer-Create-Bot]: https://worthenwebcom.files.wordpress.com/2018/11/cisco-developer-create-bot.png
[cisco-developer-create-bot-form-with-email]: https://worthenwebcom.files.wordpress.com/2018/11/cisco-developer-create-bot-form-with-email.png
[webex-add-button]: https://worthenwebcom.files.wordpress.com/2018/11/webex-add-button.png
[webex-opening-screen]: https://worthenwebcom.files.wordpress.com/2018/11/webex-opening-screen.png
[heroku-config]: https://worthenwebcom.files.wordpress.com/2018/11/heroku-config-vars.png
[trello-developer-api-key-with-token-button]: https://worthenwebcom.files.wordpress.com/2018/11/trello-developer-api-key-with-token-button.png
20 changes: 20 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let env = require('node-env-file');
env(__dirname + '/.env');
let Botkit = require('botkit');
let debug = require('debug')('botkit:main');

let controller = Botkit.sparkbot({
public_address: process.env.public_address,
access_token: process.env.access_token,
secret: process.env.secret,
webhook_name: 'Enter A Webhook Name'
});

let webserver = require(__dirname + '/components/express_webserver.js')(controller);

require(__dirname + '/components/subscribe_events.js')(controller);

let normalizedPath = require("path").join(__dirname, "skills");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./skills/" + file)(controller);
});
8 changes: 8 additions & 0 deletions skills/sample_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(controller) {
controller.on('bot_space_join', function(bot, message) {
bot.reply(message, 'I am a bot, here to do your bidding.');
});
controller.on('user_space_join', function(bot, message) {
bot.reply(message, 'Hello, ' + message.raw_message.data.personDisplayName);
});
};
Loading

0 comments on commit 1c8fc00

Please sign in to comment.