Skip to content

Commit

Permalink
Merge pull request #56 from leocabeza/master
Browse files Browse the repository at this point in the history
Implementando el issue #36 y actualizando rama de master
  • Loading branch information
leocabeza authored May 9, 2017
2 parents 91d4db7 + db5acec commit 3389e70
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"homepage": "https://github.com/ngVenezuela/wengy-ven#readme",
"dependencies": {
"feedparser": "2.0.0",
"node-fetch": "^1.6.3",
"node-telegram-bot-api": "0.25.0"
},
"devDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const messages = require('../config/messages');
const morningEvent = require('./morning-event');
const blogEvent = require('./blog-event');
const generateRandom = require('./time-utility').generateRandom;
const fetch = require('node-fetch');

const token = config.telegramToken;
const groupId = config.groupId;
Expand Down Expand Up @@ -97,3 +98,47 @@ function newChatParticipant(msg) {
return messages.welcomeMsg.replace('#{name}', nameToBeShown);
}
}

bot.on('text', (msg) => {
if ( !msg.hasOwnProperty('entities') ) {
return;
}

if ( msg.entities[0].type !== 'pre' ) {
return;
};

if ( msg.text.length >= 200 ) {
return;
}

const chatId = msg.chat.id;
const {firstName = '', lastName = '', username = ''} = msg.from;
const fullName = firstName === '' && lastName === '' ? '' : `${firstName} ${lastName} `;
const user = username === '' ? '' : `(@${username})`;
const filename = `${new Date().toISOString()}.js`;
const gist = msg.text;

const body = {
'description': 'gist creado por ' + fullName + user +
' para https://t.me/ngvenezuela con https://github.com/ngVenezuela/wengy-ven',
'public': true,
'files': {
[filename]: {
'content': gist,
},
},
};

fetch('https://api.github.com/gists', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( body ),
})
.then((response) => response.json())
.then(({html_url}) => {
bot.sendMessage(chatId, html_url);
}).catch(() => {});
});
3 changes: 2 additions & 1 deletion src/time-utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ module.exports = {
vzlanHour,
vzlanMinute,
vzlanWeekday,
generateRandom
generateRandom,
};

/**
* Generate random Integer with values with range: min-max
* @param {Integer} min - Min value
* @param {Integer} max - Max value
* @return {Integer} Random Integer value
*/
function generateRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
Expand Down
4 changes: 2 additions & 2 deletions tests/time-utility.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { generateRandom } = require('../src/time-utility.js');
const {generateRandom} = require('../src/time-utility.js');

describe('Time Utility', () => {
it('generates a random number between a min and max range', () => {
const random = generateRandom(1, 50);

expect( random >= 1 && random <= 50 ).toBe(true);
});
})
});

0 comments on commit 3389e70

Please sign in to comment.