Adding telegram bot

This commit is contained in:
Fabian Stamm 2018-10-24 10:46:49 +02:00
parent 59dcf4b926
commit 148acaadd1
4 changed files with 77 additions and 323 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ node_modules/
db.json
.env
logs/
chatid

View File

@ -29,6 +29,7 @@
"node-fetch": "^2.2.0",
"nodemailer": "^4.6.7",
"rss-parser": "^3.4.2",
"ssl-root-cas": "^1.2.5"
"ssl-root-cas": "^1.2.5",
"telegraf": "^3.24.1"
}
}

View File

@ -62,22 +62,59 @@ async function check() {
return db.get(calculateHash(item)).value() !== true;
})
for (let item of items) {
await sendFeed(feed, item).catch(err => Logging.error(err))
await sendFeedMail(feed, item).catch(err => Logging.error(err))
await sendFeedTelegraf(feed, item).catch(err => Logging.error(err));
}
} catch (error) {
Logging.error(error);
}
}
import {AllHtmlEntities} from "html-entities";
import Telegraf from "telegraf";
import * as fs from "fs";
const bot = new Telegraf(process.env.TG_TOKEN)
const chatidFilename = "./chatid";
let chatid: number;
try {
if (fs.existsSync(chatidFilename)) {
chatid = Number(fs.readFileSync(chatidFilename).toString());
}
} catch (err) {
Logging.error(err);
}
bot.start(ctx => {
let code = ctx.message.text.split(" ")[1];
if (code === process.env.TG_CODE) {
if (chatid) {
bot.telegram.sendMessage(chatid, "This chat is now disabled!");
}
chatid = ctx.chat.id;
fs.writeFileSync(chatidFilename, chatid.toString());
ctx.reply("News enabled!");
} else {
ctx.reply("Invalid code!");
}
})
bot.startPolling();
async function sendFeedTelegraf(feed: Feed, item: FeedItem) {
if (chatid) {
await bot.telegram.sendMessage(chatid, entities.decode(item.title) + "\n\n" + entities.decode(item.contentSnippet));
}
}
import { AllHtmlEntities } from "html-entities";
import { fstat } from "fs";
const entities = new AllHtmlEntities();
async function sendFeed(feed: Feed, item: FeedItem) {
async function sendFeedMail(feed: Feed, item: FeedItem) {
Logging.log("Sending Mail!")
let mailOptions = {
from: process.env.MAIL_SENDER,
to: process.env.MAIL_RECEIVER,
subject: '[OSPLUS_UPDATE] ' + entities.decode(item.title),
text: item.contentSnippet,
text: entities.decode(item.contentSnippet),
html: item.content
};

349
yarn.lock

File diff suppressed because it is too large Load Diff