Adding telegram bot
This commit is contained in:
parent
59dcf4b926
commit
148acaadd1
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,4 +2,5 @@ lib/
|
||||
node_modules/
|
||||
db.json
|
||||
.env
|
||||
logs/
|
||||
logs/
|
||||
chatid
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
45
src/index.ts
45
src/index.ts
@ -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
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user