From f409e6ff61f1c2b976b4d4ff5c0d13b28763e47e Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 26 Jun 2018 18:04:19 +0800 Subject: [PATCH] add line push/reply , add fb parser post --- background.js | 11 ++++++ libs/facebook-pageparse/index.js | 65 ++++++++++++++++++++++++++++++++ libs/line-message/index.js | 63 +++++++++++++++++++++++++++++++ package.json | 6 ++- route/index.js | 1 + route/line/index.js | 18 ++++++++- 6 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 background.js create mode 100644 libs/facebook-pageparse/index.js create mode 100644 libs/line-message/index.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..96fbad1 --- /dev/null +++ b/background.js @@ -0,0 +1,11 @@ +const CronJob = require('cron') +const fbParser = require('./libs/facebook-pageparse') + +new CronJob({ // eslint-disable-line + cronTime: '00 */10 * * * *', + onTick: () => { + + }, + start: true, + timeZone: 'Asia/Taipei' +}) diff --git a/libs/facebook-pageparse/index.js b/libs/facebook-pageparse/index.js new file mode 100644 index 0000000..700d2be --- /dev/null +++ b/libs/facebook-pageparse/index.js @@ -0,0 +1,65 @@ +const request = require('request') +const cheerio = require('cheerio') + +const getLastPost = async (pageid = '') => { + if (typeof pageid !== 'string' || pageid.trim().length === 0) return null + pageid = pageid.trim() + + let page = await new Promise((resolve) => { + request({ + baseUrl: 'https://facebook.com', + url: `/${pageid}/posts`, + method: 'get', + headers: { + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0' + } + }, (err, res, body) => { + if (err) { + resolve(null) + return + } + if (body && typeof body !== 'string' && !(body instanceof String)) { + resolve(null) + return + } + resolve(body) + }) + }) + if (page === null) return null + let $ = cheerio.load(page, { + lowerCaseTags: true, + lowerCaseAttributeNames: true + }) + + let posts = [] + + $('div.userContentWrapper').each((i, el) => { + let t = cheerio.load(el) + let timeEl = t('abbr') + let time = timeEl.attr('data-utime') + let link = timeEl.parent().attr('href') + let p = t('div.userContent div.text_exposed_root') + let txt = p.text() + let id = p.attr('id') + if (!time || !link || !txt || !id) return + let tmp = { + txt, + id, + link, + time + } + posts.push(tmp) + }) + + if (posts.length === 0) return + posts.sort((a, b) => { + return b.time - a.time + }) + let post = posts[0] + post.link = `https://facebook.com/${post.link.replace(/^\//, '')}` + return post +} + +module.exports = { + getLastPost +} diff --git a/libs/line-message/index.js b/libs/line-message/index.js new file mode 100644 index 0000000..7d83b93 --- /dev/null +++ b/libs/line-message/index.js @@ -0,0 +1,63 @@ +const axios = require('axios') +const config = require('../../config') + +const client = axios.create({ + baseURL: 'https://api.line.me/v2/bot', + headers: { + Authorization: `Bearer ${config.line.access}` + } +}) + +const pushMessage = async (target, message = '') => { + if (typeof target !== 'string' || target.trim().length === 0) return + if (typeof message !== 'string' || message.trim().length === 0) return + + let data = { + to: target, + messages: [ + { + type: 'text', + text: message + } + ] + } + + let opts = { + method: 'post', + url: '/message/push', + data + } + + await client(opts) +} + +const textMessage = async (evt) => { + let replyURL = '/message/reply' + let {replyToken, source, message} = evt + if (!message || message.type !== 'text') return + let {text} = message + if (typeof text !== 'string') return + text = text.trim() + if (text.length === 0) return + + let opts = { + method: 'post', + url: replyURL, + data: { + replyToken, + messages: [ + { + type: 'text', + text: 'test message' + } + ] + } + } + + await client(opts) +} + +module.exports = { + textMessage, + pushMessage +} diff --git a/package.json b/package.json index 4f79385..6d5bdc7 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,15 @@ "author": "JayChen", "license": "ISC", "dependencies": { + "axios": "^0.18.0", + "cheerio": "^1.0.0-rc.2", + "cron": "^1.3.0", "dotenv": "^6.0.0", "koa": "^2.5.1", "koa-body": "^4.0.3", "koa-logger": "^3.2.0", - "koa-router": "^7.4.0" + "koa-router": "^7.4.0", + "request": "^2.87.0" }, "devDependencies": { "standard": "^11.0.1" diff --git a/route/index.js b/route/index.js index 75c6dc4..e91a959 100644 --- a/route/index.js +++ b/route/index.js @@ -2,5 +2,6 @@ const Router = require('koa-router') const r = new Router() r.use('/line', require('./line').routes()) +r.use('/fb', require('./facebook').routes()) module.exports = r diff --git a/route/line/index.js b/route/line/index.js index 7929354..06695d4 100644 --- a/route/line/index.js +++ b/route/line/index.js @@ -5,6 +5,9 @@ const rawBody = require('raw-body') const { verifyLine } = require('../../libs/middleware') +const { + textMessage +} = require('../../libs/line-message') const getRaw = async (c, n) => { let raw = await rawBody(c.req, { @@ -25,8 +28,19 @@ const getRaw = async (c, n) => { } r.post('/', getRaw, verifyLine, async (c, n) => { - console.log(c.request.body) - if (!('event' in c.request.body)) return c.throw(400, 'data struct error') + console.log(JSON.stringify(c.request.body, null, 2)) + if (!('events' in c.request.body)) return c.throw(400, 'data struct error') + let evt = c.request.body.events || [] + evt.forEach(t => { + let type = t.type + switch (type) { + case 'message': + textMessage(t).catch(err => { + console.log(err) + }) + break + } + }) c.body = 'success' c.status = 200 })