-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,194 additions
and
831 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,74 @@ | ||
const Composer = require('telegraf/composer') | ||
const bot = new Composer() | ||
const cheerio = require('cheerio'); | ||
const fetch = require('node-fetch'); | ||
let dayoff = null | ||
const Composer = require("telegraf/composer"); | ||
const bot = new Composer(); | ||
const cheerio = require("cheerio"); | ||
const fetch = require("node-fetch"); | ||
let dayoff = null; | ||
//十分鐘定時清除 | ||
setInterval(cleanDayoff, 1000 * 60 * 10); //10min | ||
function cleanDayoff() { | ||
dayoff = null; | ||
}; | ||
dayoff = null; | ||
} | ||
async function getDayoff() { | ||
return dayoff || await dayoffReq() | ||
return dayoff || (await dayoffReq()); | ||
} | ||
async function dayoffReq() { | ||
let body = await fetch('https://www.dgpa.gov.tw/typh/daily/nds.html').then(res => res.text()) | ||
var $ = cheerio.load(body), | ||
city, status, time, city_status, city_name, data = { | ||
"typhoon": [], | ||
"update_time": "" | ||
}; | ||
city = $('.Table_Body > tr > td:nth-child(1):not([colspan="3"])'); | ||
status = $(".Table_Body > tr > td:nth-child(2)"); | ||
for (var i = 0; i < city.length; i++) { | ||
city_name = $(city[i]).text() | ||
city_status = $(status[i]).text().replace(/。/g, "。\n").replace(/ /g, "").trim() | ||
if (city_status.length > 40) | ||
city_status = city_status.substring(0, 40) + '...' | ||
if (city_status.match(/上午|下午|停止上班|停止上課/)) | ||
city_name = `❗️${city_name}`; | ||
else | ||
city_name = `🔹${city_name}`; | ||
let body = await fetch("https://www.dgpa.gov.tw/typh/daily/nds.html").then( | ||
(res) => res.text() | ||
); | ||
var $ = cheerio.load(body), | ||
city, | ||
status, | ||
time, | ||
city_status, | ||
city_name, | ||
data = { | ||
typhoon: [], | ||
update_time: "", | ||
}; | ||
city = $('.Table_Body > tr > td:nth-child(1):not([colspan="3"])'); | ||
status = $(".Table_Body > tr > td:nth-child(2)"); | ||
for (var i = 0; i < city.length; i++) { | ||
city_name = $(city[i]).text(); | ||
city_status = $(status[i]) | ||
.text() | ||
.replace(/。/g, "。\n") | ||
.replace(/ /g, "") | ||
.trim(); | ||
if (city_status.length > 40) | ||
city_status = city_status.substring(0, 40) + "..."; | ||
if (city_status.match(/上午|下午|停止上班|停止上課/)) | ||
city_name = `❗️${city_name}`; | ||
else city_name = `🔹${city_name}`; | ||
|
||
data.typhoon.push({ | ||
"city_name": city_name, | ||
"city_status": city_status | ||
}) | ||
} | ||
//更新時間 | ||
time = $("div.f_right > h4:nth-child(1)").text().match(/[0-9]+/g); | ||
data.update_time = `${time[3]}:${time[4]}:${time[5]}` | ||
dayoff = data | ||
return dayoff | ||
data.typhoon.push({ | ||
city_name: city_name, | ||
city_status: city_status, | ||
}); | ||
} | ||
//更新時間 | ||
time = $("div.f_right > h4:nth-child(1)") | ||
.text() | ||
.match(/[0-9]+/g); | ||
data.update_time = `${time[3]}:${time[4]}:${time[5]}`; | ||
dayoff = data; | ||
return dayoff; | ||
} | ||
bot.command('dayoff', async ctx => { | ||
let data = await getDayoff(), | ||
resp = '' | ||
for ({ city_name, city_status } of data.typhoon) { | ||
resp += `${city_name} ${city_status}\n`; | ||
} | ||
//更新時間 | ||
time = `更新時間 ${data.update_time} `; | ||
//送訊息囉 | ||
resp += `--- | ||
bot.command("dayoff", async (ctx) => { | ||
let data = await getDayoff(), | ||
resp = ""; | ||
for ({ city_name, city_status } of data.typhoon) { | ||
resp += `${city_name} ${city_status}\n`; | ||
} | ||
//更新時間 | ||
time = `更新時間 ${data.update_time} `; | ||
//送訊息囉 | ||
resp += `--- | ||
\`詳細及最新情報以\` [行政院人事行政總處](goo.gl/GjmZnR) \`公告為主\` | ||
${time}`; | ||
|
||
ctx.reply(resp, { parse_mode: "markdown", reply_to_message_id: ctx.message.message_id }) | ||
}) | ||
module.exports = bot | ||
ctx.reply(resp, { | ||
parse_mode: "markdown", | ||
reply_to_message_id: ctx.message.message_id, | ||
}); | ||
}); | ||
module.exports = bot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
const JSONdb = require('simple-json-db'); | ||
const db = new JSONdb('./database.json'); | ||
module.exports = db | ||
const JSONdb = require("simple-json-db"); | ||
const db = new JSONdb("./database.json"); | ||
module.exports = db; |
Oops, something went wrong.