Skip to content

Commit

Permalink
Using tgFancy
Browse files Browse the repository at this point in the history
  • Loading branch information
kopiro committed Jan 19, 2020
1 parent de4f3eb commit 46072d2
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
103 changes: 103 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -20,7 +20,8 @@
"pm2": "^4.2.1",
"promise-sequential": "^1.1.1",
"puppeteer": "^2.0.0",
"striptags": "^3.1.1"
"striptags": "^3.1.1",
"tgfancy": "^0.13.0"
},
"devDependencies": {
"eslint": "^5.3.0",
Expand Down
44 changes: 21 additions & 23 deletions src/index.js
Expand Up @@ -4,15 +4,13 @@
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-restricted-syntax */
const path = require("path");
const TelegramBot = require("node-telegram-bot-api");
const fs = require("fs");
const crypto = require("crypto");
const striptags = require("striptags");
const sequential = require("promise-sequential");
const Sentry = require("@sentry/node");
const { tagsAllowed, newLine, seeMore, DATA_DIR } = require("./constants");

const ENV = process.env.NODE_ENV || "development";
const Tgfancy = require("tgfancy");
const { tagsAllowed, newLine, DATA_DIR } = require("./constants");

const config = require("../data/config");

Expand Down Expand Up @@ -57,31 +55,18 @@ function notifyChange(bot, chatIds, element) {
return Promise.all(
chatIds.map(async chatId => {
console.debug(`Sending message to ${chatId}`, { message, photo, url });
const fullMessage = [message, url].filter(e => e).join(newLine);

if (photo) {
const maxMessageLength =
1020 - newLine.length - seeMore.length - (url || "").length;
const fullMessage = [
message.length > maxMessageLength
? [message.substr(0, maxMessageLength), seeMore].join("")
: message,
url
]
.filter(e => e)
.join(newLine);

bot.sendPhoto(chatId, photo, {
caption: fullMessage,
parse_mode: "html"
disable_notification: true
});
return true;
}

const fullMessage = [message, url].filter(e => e).join(newLine);
bot.sendMessage(chatId, striptags(fullMessage, tagsAllowed), {
parse_mode: "html"
parse_mode: "html",
disable_web_page_preview: true
});
return true;
})
);
}
Expand All @@ -91,6 +76,16 @@ function getModuleExecWrapper(bot, moduleConfig) {
const formatter = moduleConfig.formatter || (e => e);

return async () => {
if (config.doNotDisturb) {
const { min, max } = config.doNotDisturb;
const now = new Date();
const hour = now.getUTCHours();
if (hour < min || hour > max) {
console.log(`Do not disturb is enabled (H${hour})`);
return;
}
}

const moduleData = readDataForModule(moduleConfig);

try {
Expand Down Expand Up @@ -146,8 +141,11 @@ function getModuleExecWrapper(bot, moduleConfig) {
function main() {
const { telegram, modules } = config;

const bot = new TelegramBot(telegram.token, {
polling: telegram.polling
const bot = new Tgfancy(telegram.token, {
polling: telegram.polling,
tgfancy: {
chatIdResolution: false
}
});

const botListeners = [];
Expand Down
4 changes: 2 additions & 2 deletions src/modules/facebook_page.js
@@ -1,10 +1,10 @@
const { FB } = require("fb");

exports.fetch = ({ pageId, accessToken }) =>
exports.fetch = ({ pageId, accessToken, limit = 1 }) =>
new Promise((resolve, reject) => {
FB.setAccessToken(accessToken);
FB.api(
`/${pageId}/feed?fields=full_picture,message,permalink_url&limit=10`,
`/${pageId}/feed?fields=full_picture,message,permalink_url&limit=${limit}`,
res => {
if (!res || res.error) {
reject(res.error);
Expand Down

0 comments on commit 46072d2

Please sign in to comment.