Adding telegram bot
This commit is contained in:
parent
59dcf4b926
commit
148acaadd1
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ node_modules/
|
||||
db.json
|
||||
.env
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
43
src/index.ts
43
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 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