Skip to content

Commit

Permalink
(Arregla) Caracteres especiales en twitter (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
leocabeza authored Oct 7, 2017
1 parent 8546ceb commit 6497900
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
7 changes: 5 additions & 2 deletions src/utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
20 changes: 17 additions & 3 deletions src/utils/tweets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');

/**
* Construct and send the message to notify
* a new tweet in the group
Expand All @@ -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
);
};

Expand Down

0 comments on commit 6497900

Please sign in to comment.