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
+65
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
}
+63
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
}