This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
/** | |
* Processes a users message and gives relevant output | |
*/ | |
const processRequest = (message, output) => { | |
if(helloWords.indexOf(message.toLowerCase()) != -1) { | |
return output(greeting); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pinger = require('ping-bot'); | |
pinger(`google.com`, (response) => { | |
console.log(response); | |
// or do whatever you like with the bot | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Botkit = require('botkit'); | |
//Botkit supports both Slack and Facebook Messenger Bots, | |
//so you need to initalize for a slackbot controller with Botkit.slackbot(); | |
const controller = Botkit.slackbot(); | |
//Start a new process with the Bot Token gotten from Slack | |
const process = controller.spawn({ | |
token: process.env.SLACK_TOKEN //change this based on the token you get | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pinger = require('./ping-bot'); | |
//listen for direct message and direct mention events | |
controller.on(['direct_message', 'direct_mention'], (bot, message) => { | |
pinger(message.text, (response) => { | |
//send message reply to users | |
bot.reply(message, response); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pinger = require('./ping-bot'); | |
const Botkit = require('botkit'); | |
//require beepboop's library | |
const BeepBoop = require('beepboop-botkit'); | |
//as usual, initialize a slackbot controller | |
const controller = Botkit.slackbot(); | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quantize(k, colors) { | |
if (k > colors.length) { | |
throw Error(`K (${k}) is greater than colors (${colors.length}).`) | |
} | |
const centers = getRandomKCenters(k, colors) | |
let centerDistances = [] | |
for(let i = 0; i < colors.length; i++) { | |
for(let j = 0; j < centers.length; j++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quantize(k, colors) { | |
if (k > colors.length) { | |
throw Error(`K (${k}) is greater than colors (${colors.length}).`) | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRandomKCenters(k, colors) { | |
return lodash.sampleSize(colors, k); | |
} | |
function quantize(k, colors) { | |
... | |
const centers = getRandomKCenters(k, colors) | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function distance(color1, color2) { | |
const [ r1, g1, b1 ] = color1 | |
const [ r2, g2, b2 ] = color2 | |
return Math.sqrt( | |
Math.pow(r1 - r2, 2) + Math.pow(g1 - g2, 2) + Math.pow(b1 - b2, 2) | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quantize(k, colors) { | |
... | |
const centerDistances = new Array(k) | |
for(let i = 0; i < colors.length; i++) { | |
for(let j = 0; j < centers.length; j++) { | |
centerDistances[j] = distance(centers[j], colors[i]) | |
} | |
const minimumDistance = Math.min(...centerDistances) | |
const closestCentersIndex = centerDistances.indexOf(minimumDistance) |
OlderNewer