Skip to content

Commit

Permalink
Merge pull request #173 from leocabeza/ngve-twitter
Browse files Browse the repository at this point in the history
(Agrega) Integración con Twitter
  • Loading branch information
leocabeza authored May 5, 2019
2 parents f76ff6c + 88af21a commit 66ace42
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 53 deletions.
21 changes: 12 additions & 9 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
TELEGRAM_BOT_TOKEN=XXX
GITHUB_ACCESS_TOKEN=XXX
DIALOGFLOW_CLIENT_EMAIL=XXX
DIALOGFLOW_PRIVATE_KEY=XXX
DIALOGFLOW_PROJECT_ID=XXX
SENTRY_DSN=
NTBA_FIX_319=1
BOT_USERNAME=XXX
NODE_ENV=development
ADMIN_GROUP_ID=XXX
APP_PORT=8443
BOT_USERNAME=TestsBot
GITHUB_ACCESS_TOKEN=
DIALOGFLOW_CLIENT_EMAIL=
DIALOGFLOW_PRIVATE_KEY=
DIALOGFLOW_PROJECT_ID=
MAIN_GROUP_ID=
NODE_ENV=development
NTBA_FIX_319=1
SENTRY_DSN=
TELEGRAM_BOT_TOKEN=
TWITTER_CONSUMER_SECRET=
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## [Unreleased]

## 4.1.0 (2019-05-04)

### Agregado

- Habilitada de nuevo la funcionalidad de twitter

## 4.0.3 (2019-05-01)

### Actualizado
Expand Down
3 changes: 2 additions & 1 deletion now-staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"NODE_ENV": "staging",
"NTBA_FIX_319": "1",
"SENTRY_DSN": "@sentryio-dns-staging",
"TELEGRAM_BOT_TOKEN": "@telegram-bot-token-staging"
"TELEGRAM_BOT_TOKEN": "@telegram-bot-token-staging",
"TWITTER_CONSUMER_SECRET": "@twitter-consumer-secret"
},
"version": 1,
"public": true,
Expand Down
3 changes: 2 additions & 1 deletion now.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"NODE_ENV": "production",
"NTBA_FIX_319": "1",
"SENTRY_DSN": "@sentryio-dns",
"TELEGRAM_BOT_TOKEN": "@telegram-bot-token"
"TELEGRAM_BOT_TOKEN": "@telegram-bot-token",
"TWITTER_CONSUMER_SECRET": "@twitter-consumer-secret"
},
"version": 1,
"public": true,
Expand Down
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
"dotenv": "^6.2.0",
"express": "^4.16.4",
"ngrok": "^3.1.1",
"node-fetch": "^2.4.1",
"node-fetch": "^2.5.0",
"node-telegram-bot-api": "^0.30.0"
},
"devDependencies": {
"husky": "^1.3.1",
"lint-staged": "^8.1.5",
"lint-staged": "^8.1.6",
"prettier": "1.17.0"
},
"husky": {
Expand Down
2 changes: 1 addition & 1 deletion src/config/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const messages = {
newBlogPost: '*#{author}* ha agregado una nueva entrada al blog titulada: '.concat(
'*#{title}* y está disponible en: #{link}'
),
newTweet: '#{hashtagMessage} \u{1F426}\n'.concat(
newTweet: '#ngVenezuelaTweet \u{1F426}\n'.concat(
'#{tweetText}\n',
'----\n',
'Puedes ver el Tweet aquí: #{tweetUrl}'
Expand Down
20 changes: 14 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const Sentry = require('@sentry/node');
const handleListenCommands = require('./listen-commands');
const handleTimeEvents = require('./time-events');
const WebhookServer = require('./webhooks/server');
const { Telegram, Superfeedr } = require('./webhooks');
const { handleBlogEntry, handleGithubRelease } = require('parsers');
const { Telegram, Superfeedr, Twitter } = require('./webhooks');
const {
handleBlogEntry,
handleGithubRelease,
handleTweet,
} = require('parsers');

//NOTE: About the port -> https://core.telegram.org/bots/api#setwebhook
const {
Expand All @@ -19,14 +23,13 @@ const {

const telegramBot = new Telegram(TELEGRAM_BOT_TOKEN);
const superfeedr = new Superfeedr();
const twitter = new Twitter();
Sentry.init({ dsn: SENTRY_DSN });

initializeBot = async () => {
if (NODE_ENV === 'development') {
const url = await ngrok.connect(APP_PORT);
console.log(
`use this url in your webhooks along with the token: ${url}/${TELEGRAM_BOT_TOKEN}`
);
console.log(`use this url in your webhooks: ${url}/${TELEGRAM_BOT_TOKEN}`);
telegramBot.setWebHook(`${url}/${TELEGRAM_BOT_TOKEN}`);
} else {
telegramBot.setWebHook(`${APP_URL}/${TELEGRAM_BOT_TOKEN}`);
Expand All @@ -39,7 +42,8 @@ handleTimeEvents(telegramBot);

new WebhookServer(`/${TELEGRAM_BOT_TOKEN}`, APP_PORT)
.subscribe(telegramBot)
.subscribe(superfeedr);
.subscribe(superfeedr)
.subscribe(twitter);

superfeedr.on('newGithubRelease', msg => {
handleGithubRelease(telegramBot, msg);
Expand All @@ -49,6 +53,10 @@ superfeedr.on('newBlogEntry', msg => {
handleBlogEntry(telegramBot, msg);
});

twitter.on('newTweet', msg => {
handleTweet(telegramBot, msg);
});

process.on('SIGUSR2', () => {
if (NODE_ENV === 'development') {
telegramBot.closeWebHook();
Expand Down
7 changes: 5 additions & 2 deletions src/listen-commands/development-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ const sendGroupId = async (bot, msg) => {
const chatId = msg.chat.id;
const chatInfo = await bot.getChat(chatId);

if (['group', 'supergroup'].includes(chatInfo.type)) {
if (chatInfo.type === 'private') {
bot.sendMessage(
chatId,
`Tú variable \`mainGroupId/adminGroupId\` es: ${chatInfo.id}`
`Tú variable \`mainGroupId/adminGroupId\` es: ${chatInfo.id}`,
{
parse_mode: 'Markdown',
}
);
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const handleBlogEntry = require('./blog');
const handleGithubRelease = require('./github');
const handleTweet = require('./twitter');

module.exports = {
handleBlogEntry,
handleGithubRelease,
handleTweet,
};
31 changes: 31 additions & 0 deletions src/parsers/twitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { mainGroupId } = require('config/telegram');
const { newTweet } = require('config/messages');
const { sendMessage } = require('bot-api-overrides');

const isRt = tweet => tweet.retweeted || tweet.retweeted_status;

const isReply = tweet =>
tweet.in_reply_to_status_id || tweet.in_reply_to_user_id;

const handleTweet = (bot, tweets = []) => {
tweets.forEach(tweet => {
if (!isReply(tweet) && !isRt(tweet)) {
const tweetUrl = `https://twitter.com/${tweet.user.screen_name}/status/${
tweet.id_str
}`;

sendMessage(
bot,
mainGroupId,
newTweet
.replace('#{tweetText}', tweet.text)
.replace('#{tweetUrl}', tweetUrl),
{
parse_mode: 'Markdown',
}
);
}
});
};

module.exports = handleTweet;
2 changes: 2 additions & 0 deletions src/webhooks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const Telegram = require('./telegram');
const Superfeedr = require('./superfeedr');
const Twitter = require('./twitter');

module.exports = {
Superfeedr,
Telegram,
Twitter,
};
17 changes: 15 additions & 2 deletions src/webhooks/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ class BotServer {
// parse the updates to JSON
app.use(bodyParser.json());

// If a webhook has a verify url, we receive it here
app.get(path, (req, res) => {
this.webhooks.forEach(
webhook =>
typeof webhook.verifyMessage === 'function' &&
webhook.verifyMessage(req, res)
);
});

// We are receiving updates at the route below!
app.post(path, (req, res) => {
this.webhooks.forEach(
webhook =>
webhook.checkMessage(req.body) && webhook.proccessMessage(req.body)
webhook.checkMessage(req.body) && webhook.processMessage(req.body)
);

res.sendStatus(200).end();
Expand All @@ -45,7 +54,11 @@ class BotServer {
* @param {object} webhook
*/
static isWebHook(webhook) {
return webhook && webhook.checkMessage && webhook.proccessMessage;
return (
webhook &&
typeof webhook.checkMessage === 'function' &&
typeof webhook.processMessage === 'function'
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/webhooks/superfeedr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Superfeedr extends EventEmitter {
* Emit message to subscribers
* @param {object} msg - Message to emit
*/
proccessMessage(msg) {
processMessage(msg) {
if (msg.status.feed.startsWith('https://medium.com')) {
return this.emit('newBlogEntry');
}
Expand Down
5 changes: 3 additions & 2 deletions src/webhooks/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ class TelegramBot extends NodeTelegramBotApi {
msg.chosen_inline_result ||
msg.callback_query ||
msg.shipping_query ||
msg.pre_checkout_query
msg.pre_checkout_query ||
msg.poll
);
}

/**
* Give message to processUpdate parent method
* @param {object} msg - Message to process
*/
proccessMessage(msg) {
processMessage(msg) {
this.processUpdate(msg);
}
}
Expand Down
Loading

0 comments on commit 66ace42

Please sign in to comment.