From 64979008ac8da5b6d54c20f36c2b3c68b6f2d3ac Mon Sep 17 00:00:00 2001 From: Leonardo Cabeza Date: Sat, 7 Oct 2017 12:08:37 -0500 Subject: [PATCH] (Arregla) Caracteres especiales en twitter (#130) --- config/messages.js | 2 +- package.json | 3 ++- src/index.js | 4 ++++ src/utils/message.js | 7 +++++-- src/utils/tweets.js | 20 +++++++++++++++++--- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/config/messages.js b/config/messages.js index 20fd70b..b5b1b0b 100644 --- a/config/messages.js +++ b/config/messages.js @@ -36,7 +36,7 @@ const messages = { newTweet: '#{hashtagMessage} \u{1F426}\n'.concat( '#{tweetText}\n', '----\n', - '[Puedes ver el Tweet aquí](#{tweetUrl})'), + 'Puedes ver el Tweet aquí: #{tweetUrl}'), githubRelease: '*#{name}* acaba de alcanzar la versión *#{version}*\n\n'.concat( '[Puedes ver los cambios aquí](#{url})'), githubOpenVeLink: 'El enlace de github para comunidades de Telegram es: #{link}', diff --git a/package.json b/package.json index 1c50853..9db8616 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Official telegram bot for the ng-Venezuela community", "main": "src/index.js", "scripts": { - "dev": " debugMode=1 node_modules/nodemon/bin/nodemon.js src/index.js", + "dev": " debugMode=1 node src/index.js", + "dev:watch": " debugMode=1 node_modules/nodemon/bin/nodemon.js src/index.js", "lint": "node_modules/eslint/bin/eslint.js **/*.js", "test": "node_modules/jest/bin/jest.js --no-cache --no-watchman || true", "test:watch": "node_modules/jest/bin/jest.js --no-cache --watch" diff --git a/src/index.js b/src/index.js index a937dda..ece2ab4 100644 --- a/src/index.js +++ b/src/index.js @@ -140,6 +140,10 @@ redisClient true ) ); + bot + .on('webhook_error', (error) => { + throw new Error(`Webhook error: ${error}`); + }); }) .on('error', (error) => { throw new Error(`Redis error: ${error}`); diff --git a/src/utils/message.js b/src/utils/message.js index 3b769fc..0cb28af 100644 --- a/src/utils/message.js +++ b/src/utils/message.js @@ -22,10 +22,13 @@ const forwardMessage = (bot, chatId, fromChatId, messageId, options = {}) => { * @param {string} messageText * @param {boolean} replyMode * @param {number} messageId + * @param {boolean} htmlMode */ -const sendMessage = (bot, chatId, messageText, replyMode = false, messageId = null) => { +const sendMessage = ( + bot, chatId, messageText, replyMode = false, messageId = null, htmlMode = false +) => { try { - const defaultOptions = { parse_mode: 'Markdown' }; + const defaultOptions = { parse_mode: htmlMode ? 'HTML' : 'Markdown' }; if (replyMode && messageId) { Object.assign(defaultOptions, { reply_to_message_id: messageId }); } diff --git a/src/utils/tweets.js b/src/utils/tweets.js index 176102e..b9cbea5 100644 --- a/src/utils/tweets.js +++ b/src/utils/tweets.js @@ -3,6 +3,18 @@ const sendMessage = require('./../utils/message').sendMessage; const newTweetMessage = require('./../../config/messages').newTweet; const hashtagMessage = require('./../../config/config').integrations.twitter.hashtagMessage; +/** + * Remove some html entities + * @param {string} text + * @see https://core.telegram.org/bots/api#html-style + */ +const sanitizeTweet = text => + text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); + /** * Construct and send the message to notify * a new tweet in the group @@ -12,14 +24,16 @@ const hashtagMessage = require('./../../config/config').integrations.twitter.has */ const sendNewTweet = (bot, tweet) => { const tweetUrl = `https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`; - sendMessage( bot, config.community.telegram.groupId, newTweetMessage - .replace('#{tweetText}', tweet.text) + .replace('#{tweetText}', sanitizeTweet(tweet.text)) .replace('#{tweetUrl}', tweetUrl) - .replace('#{hashtagMessage}', hashtagMessage) + .replace('#{hashtagMessage}', hashtagMessage), + false, + null, + true ); };