Last active
February 17, 2016 02:58
-
-
Save abhisekp/e55d495050c9d1805639 to your computer and use it in GitHub Desktop.
GitterBot
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
console.clear(); | |
/* GLOBALS */ | |
const GitterBot = { | |
config: { | |
roomId: '546fd572db8155e6700d6eaf', // FreeCodeCamp/FreeCodeCamp // <-- insert the room id | |
token: '', // <-- insert your user token (Get it from https://developer.gitter.im/apps) | |
} | |
}; | |
const messages = { | |
FCCwelcome: genFCCWelcomeMessage(``), // <-- insert comma/space separated list of usernames with(out) @ symbol | |
message: `` // <-- insert "@username says something in 3rd person" to show as status message | |
} | |
/* ONLY EDIT ABOVE THIS LINE */ | |
function genFCCWelcomeMessage(authors = '') { | |
let line1 = '', | |
line2 = '', | |
line3 = ''; | |
function prependAt(author) { | |
const mention = author && /^[^@]/.test(author) ? '@' + author : author; | |
return mention; | |
} | |
if (typeof authors === 'string') { | |
authors = authors.split(/[ ,]/).filter(Boolean); | |
} | |
// const mentions = [for(var author of authors) prependAt(author)].join(' '); | |
const mentions = authors.map(prependAt).join(' '); | |
// insert mentions in middle | |
const middleInsertion = mentions.length <= 40; | |
// if there are mentions and middle insertion is false, then repeatOffset = 0 | |
const repeatOffset = mentions && middleInsertion ? mentions.length * 1.27 : 0; | |
if (!middleInsertion) { | |
line1 += `${mentions}\n`; | |
} | |
line1 += `:snowflake: ${' '.repeat(100/2-repeatOffset)}${middleInsertion ? mentions : ''}${' '.repeat(100/2-repeatOffset)} :snowflake:`; | |
line2 += `###:snowman: $$ \\color{#4A2B0F}{\\text{FreeCodeCamp}}\\ \\color{black}{welcomes\\ you} $$ :snowman:`; | |
line3 += `:blossom: ${' '.repeat(28)} :blossom: ${' '.repeat(28)} :blossom: ${' '.repeat(28)} :blossom:`; | |
const line1Chars = line1.split(/[;& \t]+/).filter(Boolean); | |
// console.info('Chars', line1Chars); | |
console.info('line1 chars', line1Chars.length); | |
let message = '----\n'; | |
message += `${line1} \n ${line2} \n ${line3}`; | |
message = message.replace(/[ \t]+/g, ' '); | |
return message; | |
} | |
GitterBot.sendMessage = function sendMessage(opts = {}, cb) { | |
var settings = { | |
async: true, | |
crossDomain: false, | |
url: `https://gitter.im/api_staging/v1/rooms/${GitterBot.config.roomId}/chatMessages`, | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json', | |
authorization: `Bearer ${GitterBot.config.token}`, | |
}, | |
processData: false, | |
data: JSON.stringify({ | |
text: messages.message || messages.FCCwelcome, | |
burstStart: false, | |
status: true | |
}) | |
}; | |
Object.assign(settings, opts); | |
console.debug('Settings:', settings); | |
$.ajax(settings).done(function(response) { | |
console.log(response); | |
cb && cb(response); | |
}); | |
} | |
GitterBot.sendMessage(); | |
// console.log(getFCCWelcomeMessage('')); |
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
console.clear(); | |
/* GLOBALS */ | |
const GitterBot = { | |
config: { | |
roomId: '546fd572db8155e6700d6eaf', // FreeCodeCamp/FreeCodeCamp // <-- insert the room id | |
token: '', // <-- insert your user token | |
blacklists: { | |
rooms: ['FreeCodeCamp/FreeCodeCamp'], | |
authors: ['abhisekp'] | |
}, | |
whitelists: { | |
rooms: [], | |
authors: [] | |
}, | |
authors: [{ | |
username: 'abhisekp', | |
token: '' | |
}], | |
rooms: [{ | |
uri: 'FreeCodeCamp/FreeCodeCamp', | |
id: '546fd572db8155e6700d6eaf' | |
}] | |
} | |
}; | |
const messages = { | |
FCCwelcome: genFCCWelcomeMessage(''), // <-- insert comma/space separated list of usernames with(out) @ symbol | |
message: '' | |
} | |
function genFCCWelcomeMessage(authors = '') { | |
let line1 = '', | |
line2 = '', | |
line3 = ''; | |
function prependAt(author) { | |
const mention = author && /^[^@]/.test(author) ? '@' + author : author; | |
return mention; | |
} | |
if (typeof authors === 'string') { | |
authors = authors.split(/[ ,]/).filter(Boolean); | |
} | |
// const mentions = [for(var author of authors) prependAt(author)].join(' '); | |
const mentions = authors.map(prependAt).join(' '); | |
// insert mentions in middle | |
const middleInsertion = mentions.length <= 40; | |
// if there are mentions and middle insertion is false, then repeatOffset = 0 | |
const repeatOffset = mentions && middleInsertion ? mentions.length * 1.27 : 0; | |
if (!middleInsertion) { | |
line1 += `${mentions}\n`; | |
} | |
line1 += `:snowflake: ${' '.repeat(100/2-repeatOffset)}${middleInsertion ? mentions : ''}${' '.repeat(100/2-repeatOffset)} :snowflake:`; | |
line2 += `###:snowman: $$ \\color{#4A2B0F}{\\text{FreeCodeCamp}}\\ \\color{black}{welcomes\\ you} $$ :snowman:`; | |
line3 += `:blossom: ${' '.repeat(28)} :blossom: ${' '.repeat(28)} :blossom: ${' '.repeat(28)} :blossom:`; | |
const line1Chars = line1.split(/[;& \t]+/).filter(Boolean); | |
// console.info('Chars', line1Chars); | |
console.info('line1 chars', line1Chars.length); | |
let message = '----\n'; | |
message += `${line1} \n ${line2} \n ${line3}`; | |
message = message.replace(/[ \t]+/g, ' '); | |
return message; | |
} | |
//window.addEventListener('DOMContentLoaded', init, false); | |
function init() { | |
console.info('Initiating'); | |
const iframe = document.getElementById('content-frame').contentDocument; | |
const chatContainer = iframe.getElementById('chat-container'); | |
const chats = chatContainer.getElementsByTagName('.chat-container').firstChild; | |
const chatContainerObs = new MutationObserver(GitterBot.observeChat); | |
chatContainerObs.observe(chats); | |
console.info('Observing new chat messages'); | |
console.info('Initiated'); | |
} | |
GitterBot.observeChat = function observeChat(target, config) { | |
const chats = chatContainer.getElementsByTagName('div')[0]; | |
// console.debug('All Chats', chats); | |
const allChatItems = chatContainer.querySelectorAll('.chat-item'); | |
/* get latest item */ | |
const latestChatItem = allChatItems[Array.from(allChatItems).length]; | |
console.debug(latestChatItem); | |
const chatDetails = Object.assign({}, getChatDetails()); | |
console.debug('Chat Details:', chatDetails); | |
/* process if chat sent within one second (protects against scrolling effects) */ | |
if (Date.now() - chatDetails.time < 1000) { | |
processChat(chatDetails); | |
} | |
} | |
GitterBot.processChat = function processChat(chatDetails) { | |
/* exit if no text */ | |
if (!chatDetails || !chatDetails.text) { | |
return; | |
} | |
} | |
GitterBot.getChatDetails = function getChatDetails() { | |
const chatDetails = {}; | |
const time = new Date(latestChatItem.querySelector('.chat-item__time').childNode.title); | |
console.debug('Chat Time:', time); | |
chatDetails.time = time; | |
chatDetails.author = {}; | |
const author = latestChatItem.querySelector('.chat-item__from').textContent; | |
console.debug('Author:', author); | |
chatDetails.author.name = author; | |
const authorImage = latestChatItem.querySelector('.avatar__image').src; | |
console.debug('Author Image:', authorImage); | |
chatDetails.author.image = authorImage; | |
const chatText = latestChatItem.querySelector('.chat-item__text').innerHTML; | |
console.debug('Chat Text:', chatText); | |
chatDetails.text = chatText; | |
return chatDetails; | |
} | |
GitterBot.sendMessage = function sendMessage(opts = {}, cb) { | |
var settings = { | |
async: true, | |
crossDomain: false, | |
url: `https://gitter.im/api_staging/v1/rooms/${GitterBot.config.roomId}/chatMessages`, | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json', | |
authorization: `Bearer ${GitterBot.config.token}`, | |
}, | |
processData: false, | |
data: JSON.stringify({ | |
text: messages.message || messages.FCCwelcome, | |
burstStart: false, | |
status: true | |
}) | |
}; | |
Object.assign(settings, opts); | |
console.debug('Settings:', settings); | |
$.ajax(settings).done(function(response) { | |
console.log(response); | |
cb && cb(response); | |
}); | |
} | |
GitterBot.sendMessage(); | |
// console.log(getFCCWelcomeMessage('')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment