add line push/reply , add fb parser post

This commit is contained in:
Jay 2018-06-26 18:04:19 +08:00
parent 075b68012e
commit f409e6ff61
6 changed files with 161 additions and 3 deletions

11
background.js Normal file
View File

@ -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'
})

View File

@ -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
}

View File

@ -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
}

View File

@ -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"

View File

@ -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

View File

@ -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
})