add fan page notify, add twitch route, todo: twitch webhook reg

This commit is contained in:
Jay
2018-06-27 00:59:53 +08:00
parent f409e6ff61
commit 1f3057df69
15 changed files with 377 additions and 51 deletions
+13
View File
@@ -0,0 +1,13 @@
const pg = require('pg')
const config = require('../config')
const pool = new pg.Pool({
user: config.database.user,
password: config.database.pass || null,
host: config.database.host,
port: config.database.port,
max: 100,
database: config.database.dbname
})
module.exports = pool
+33 -6
View File
@@ -1,6 +1,18 @@
const request = require('request')
const cheerio = require('cheerio')
/**
* @typedef lastPost
* @prop {string} txt post body
* @prop {string} id post id
* @prop {string} link post link
* @prop {string} time timestamp
*/
/**
* get facebook fan page last post
* @param {string} pageid facebook fan page id
* @return {Promise<lastPost>}
*/
const getLastPost = async (pageid = '') => {
if (typeof pageid !== 'string' || pageid.trim().length === 0) return null
pageid = pageid.trim()
@@ -38,10 +50,23 @@ const getLastPost = async (pageid = '') => {
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 p = t('div.userContent')
let txt = p.first().text()
let id = p.first().attr('id')
if (!id) {
if (/[\?|&]id\=(\d+)/.test(link)) { // eslint-disable-line
let m = link.match(/[\?|&]story_fbid\=(\d+)/) // eslint-disable-line
if (m !== null && m.length > 1) {
id = m[1]
}
} else if (/\/posts\/(\d+)/.test(link)) {
let m = link.match(/\/posts\/(\d+)/)
if (m !== null && m.length > 1) {
id = m[1]
}
}
}
if (!time || !link || !txt || !id) return null
let tmp = {
txt,
id,
@@ -49,9 +74,11 @@ const getLastPost = async (pageid = '') => {
time
}
posts.push(tmp)
el = null
t = null
})
if (posts.length === 0) return
$ = null
if (posts.length === 0) return null
posts.sort((a, b) => {
return b.time - a.time
})
+17 -14
View File
@@ -1,5 +1,6 @@
const axios = require('axios')
const config = require('../../config')
const DB = require('../database')
const client = axios.create({
baseURL: 'https://api.line.me/v2/bot',
@@ -39,22 +40,24 @@ const textMessage = async (evt) => {
if (typeof text !== 'string') return
text = text.trim()
if (text.length === 0) return
let db = await DB.connect()
let opts = {
method: 'post',
url: replyURL,
data: {
replyToken,
messages: [
{
type: 'text',
text: 'test message'
}
]
}
}
// let opts = {
// method: 'post',
// url: replyURL,
// data: {
// replyToken,
// messages: [
// {
// type: 'text',
// text: 'test message'
// }
// ]
// }
// }
await client(opts)
// await client(opts)
db.release()
}
module.exports = {
+22 -2
View File
@@ -1,4 +1,5 @@
const config = require('../../config')
const rawBody = require('raw-body')
const crypto = require('crypto')
const verifyLine = async (c, n) => {
@@ -10,6 +11,25 @@ const verifyLine = async (c, n) => {
return n()
}
module.exports = {
verifyLine
const getRaw = async (c, n) => {
let raw = await rawBody(c.req, {
length: c.request.length,
limit: '5mb',
encoding: c.request.charset
})
c.request.raw = raw
let txt = raw instanceof Buffer ? raw.toString() : raw
if (c.request.type === 'application/json') {
try {
c.request.body = JSON.parse(txt)
} catch (err) {
c.request.body = txt
}
}
return n()
}
module.exports = {
verifyLine,
getRaw
}