Skip to content

Instantly share code, notes, and snippets.

View perfectmak's full-sized avatar
🎯
Focusing

Perfect Makanju perfectmak

🎯
Focusing
View GitHub Profile
...
/**
* Processes a users message and gives relevant output
*/
const processRequest = (message, output) => {
if(helloWords.indexOf(message.toLowerCase()) != -1) {
return output(greeting);
}
const pinger = require('ping-bot');
pinger(`google.com`, (response) => {
console.log(response);
// or do whatever you like with the bot
});
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
});
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);
});
});
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();
/**
@perfectmak
perfectmak / image-color-quantization.js
Last active January 26, 2018 18:06
Simple Implementation of Color Quantization in Javascript (Node.js)
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++) {
function quantize(k, colors) {
if (k > colors.length) {
throw Error(`K (${k}) is greater than colors (${colors.length}).`)
}
...
}
function getRandomKCenters(k, colors) {
return lodash.sampleSize(colors, k);
}
function quantize(k, colors) {
...
const centers = getRandomKCenters(k, colors)
...
}
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)
)
}
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)