facebook page notify v1.0

This commit is contained in:
Jay 2018-06-27 23:38:56 +08:00
parent 99e5a455cb
commit e2eeef9d82
4 changed files with 190 additions and 154 deletions

View File

@ -10,49 +10,14 @@ new cron.CronJob({ // eslint-disable-line
onTick: async () => {
console.log('run cron')
let db = await DB.connect()
let text = `select line."notify" as notify, fb."id" as id, fb."groupid" as group, fb."pageid" as page, fb."tmpl" as tmpl, fb."lastpost" as post
from "public"."facebook_page" fb
left join "public"."line_group" line
on line."id" = fb."groupid"
where
line."notify" = true`
let text = `select "id" as page, "lastpost" as post from "public"."facebook_page"`
let res = await db.query({
text
})
console.log('rows length :::: ', res.rowCount)
if (res.rows.legnth === 0) return
await new Promise(resolve => {
let count = res.rowCount
res.rows.forEach(t => {
fbParser.getLastPost(t.page)
.then((data) => {
let n = Math.floor(Date.now() / 1000)
if (t.post === data.id || data.time < (n - 10 * 60)) {
if (!--count) resolve(null)
return
}
t.post = data.id
let msg = t.tmpl || ''
if (typeof msg !== 'string' || msg.trim().length === 0) {
msg = `${data.txt}\n${data.link}`
} else {
msg = msg.replace(/{link}/, data.link).replace(/{txt}/, data.txt).replace(/\\n/, '\n')
}
if (t.notify) {
pushMessage(t.group, msg).then(() => {}).catch(() => {})
}
let text = `update "public"."facebook_page" set "lastpost" = $1, "mtime" = now() where "id" = $2`
let values = [data.id, t.id]
db.query({
text,
values
}).then(() => {}).catch(() => {})
if (!--count) resolve(null)
})
.catch(() => {
if (!--count) resolve(null)
})
})
res.rows.forEach(t => {
runCheckPage(t)
})
db.release()
@ -62,6 +27,67 @@ new cron.CronJob({ // eslint-disable-line
timeZone: 'Asia/Taipei'
})
const runCheckPage = async function (t) {
let db = await DB.connect()
fbParser.getLastPost(t.page)
.then(async (data) => {
let n = Math.floor(Date.now() / 1000)
if (t.post === data.id || data.time < (n - 10 * 60)) {
return
}
t.post = data.id
try {
let text = `update "public"."facebook_page" set "lastpost" = $1, "mtime" = now() where "id" = $2`
let values = [data.id, t.page]
await db.query({
text,
values
})
} catch (err) {
console.log(err)
}
let rt = null
try {
let text = `select rt."facebook" as page, rt."tmpl" as tmpl, rt."line" as group
from "public"."line_fb_rt" rt
left join "public"."line_group" line
on line."id" = rt."line"
where
line."notify" = true
and rt."facebook" = $1`
let values = [t.page]
rt = await db.query({
text,
values
})
} catch (err) {
console.log(err)
}
if (rt === null || rt.rowCount === 0) {
return
}
for (let i in rt.rows) {
let tmp = rt.rows[i]
let msg = tmp.tmpl || ''
if (typeof msg !== 'string' || msg.trim().length === 0) {
msg = `${data.txt}\n${data.link}`
} else {
msg = msg.replace(/{link}/, data.link).replace(/{txt}/, data.txt).replace(/\\n/, '\n')
}
pushMessage(tmp.group, msg).then(() => {}).catch(() => {})
}
})
.then(() => {
db.release()
})
.catch(() => {
db.release()
})
}
// register twitch streaming webhook
new cron.CronJob({ //eslint-disable-line
cronTime: '00 00 0,6,12,18 * * *',

View File

@ -16,11 +16,11 @@ const cheerio = require('cheerio')
const getLastPost = async (pageid = '') => {
if (typeof pageid !== 'string' || pageid.trim().length === 0) return null
pageid = pageid.trim()
console.log('access facebook fan page :::: ' + pageid)
let page = await new Promise((resolve) => {
request({
baseUrl: 'https://facebook.com',
url: `/${pageid}/posts`,
url: `/${encodeURIComponent(pageid)}/posts`,
method: 'get',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0'

View File

@ -36,7 +36,7 @@ const addGroup = async (txt, source = {}, db) => {
/**
* add facebook page notify to group
* @param {string} txt command body format => pageid name tmpl
* @param {string} txt command body format => pageid tmpl
* @param {object} source
* @param {object} db
*/
@ -47,8 +47,7 @@ const addPage = async (txt, source = {}, db) => {
let arr = txt.split(' ')
if (arr.length < 3) return null
let page = arr[0]
let name = arr[1]
let tmpl = arr.slice(2).join(' ')
let tmpl = arr.slice(1).join(' ')
let text = `select "id","owner" from "public"."line_group" where "id" = $1`
let values = [groupId]
let result = await db.query({
@ -63,43 +62,53 @@ const addPage = async (txt, source = {}, db) => {
let reply = 'not owner'
return { reply }
}
// check pageid in group
text = `select fb."id" from "public"."facebook_page" fb
left join "public"."line_group" line
on fb."groupid" = line."id"
where
fb."pageid" = $1`
// check page exists
text = `select "id" from "public"."facebook_page" where "id"=$1`
values = [page]
result = await db.query({
let fb = await db.query({
text,
values
})
if (result.rowCount > 0) {
let reply = 'page exists'
return { reply }
// if no page data insert page data and insert rt
if (fb.rowCount === 0) {
text = `insert into "public"."facebook_page" ("id", "lastpost", "ctime", "mtime") values
($1, '', now(), now())`
values = [page]
await db.query({
text,
values
})
text = `insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values ($1, $2, $3)`
values = [groupId, page, tmpl]
await db.query({
text,
values
})
} else {
// check rt exists
text = `select rt.* from "public"."line_fb_rt" rt
left join "public"."line_group" line
on line."id" = rt."line"
left join "public"."facebook_page" fb
on fb."id" = rt."facebook"
where
fb."id" = $1
and line."id" = $2`
values = [page, groupId]
let rt = await db.query({
text,
values
})
if (rt.rowCount === 0) {
text = `insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values ($1, $2, $3)`
values = [groupId, page, tmpl]
await db.query({
text,
values
})
}
}
// check page name in group
text = `select fb."id" from "public"."facebook_page" fb
left join "public"."line_group" line
on fb."groupid" = line."id"
where
fb."name" = $1`
values = [name]
result = await db.query({
text,
values
})
if (result.rowCount > 0) {
let reply = 'page name exists'
return { reply }
}
text = `insert into "public"."facebook_page" ("groupid", "pageid", "name", "tmpl", "ctime", "mtime") values
($1, $2, $3, $4, now(), now())`
values = [groupId, page, name, tmpl]
await db.query({
text,
values
})
let reply = 'add page success'
return { reply }
}

View File

@ -2,32 +2,35 @@
-- PostgreSQL database dump
--
-- Dumped from database version 9.6.8
-- Dumped by pg_dump version 9.6.8
-- Dumped from database version 10.2 (Debian 10.2-1.pgdg90+1)
-- Dumped by pg_dump version 10.2 (Debian 10.2-1.pgdg90+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;
ALTER TABLE IF EXISTS ONLY public.twitch_channel DROP CONSTRAINT IF EXISTS twitch_channel_line_group_id_fk;
ALTER TABLE IF EXISTS ONLY public.facebook_page DROP CONSTRAINT IF EXISTS facebook_page_line_group_id_fk;
SET search_path = public, pg_catalog;
ALTER TABLE IF EXISTS ONLY public.line_twitch_rt DROP CONSTRAINT IF EXISTS line_twitch_rt_twitch_channel_id_fk;
ALTER TABLE IF EXISTS ONLY public.line_twitch_rt DROP CONSTRAINT IF EXISTS line_twitch_rt_line_group_id_fk;
ALTER TABLE IF EXISTS ONLY public.line_fb_rt DROP CONSTRAINT IF EXISTS line_fb_rt_line_group_id_fk;
ALTER TABLE IF EXISTS ONLY public.line_fb_rt DROP CONSTRAINT IF EXISTS line_fb_rt_facebook_page_id_fk;
DROP INDEX IF EXISTS public.line_group_name_uindex;
ALTER TABLE IF EXISTS ONLY public.twitch_channel DROP CONSTRAINT IF EXISTS twitch_channel_pkey;
ALTER TABLE IF EXISTS ONLY public.twitch_channel DROP CONSTRAINT IF EXISTS twitch_channel_id_pk;
ALTER TABLE IF EXISTS ONLY public.line_twitch_rt DROP CONSTRAINT IF EXISTS line_twitch_rt_line_twitch_pk;
ALTER TABLE IF EXISTS ONLY public.line_group DROP CONSTRAINT IF EXISTS line_group_pkey;
ALTER TABLE IF EXISTS ONLY public.facebook_page DROP CONSTRAINT IF EXISTS facebook_page_pkey;
ALTER TABLE IF EXISTS public.twitch_channel ALTER COLUMN id DROP DEFAULT;
ALTER TABLE IF EXISTS public.facebook_page ALTER COLUMN id DROP DEFAULT;
ALTER TABLE IF EXISTS ONLY public.line_fb_rt DROP CONSTRAINT IF EXISTS line_fb_rt_facebook_line_pk;
ALTER TABLE IF EXISTS ONLY public.facebook_page DROP CONSTRAINT IF EXISTS facebook_page_id_pk;
DROP TABLE IF EXISTS public.version_ctrl;
DROP SEQUENCE IF EXISTS public.twitch_channel_id_seq;
DROP TABLE IF EXISTS public.twitch_channel;
DROP TABLE IF EXISTS public.line_twitch_rt;
DROP TABLE IF EXISTS public.line_group;
DROP SEQUENCE IF EXISTS public.facebook_page_id_seq;
DROP TABLE IF EXISTS public.line_fb_rt;
DROP TABLE IF EXISTS public.facebook_page;
DROP EXTENSION IF EXISTS plpgsql;
DROP SCHEMA IF EXISTS public;
@ -59,6 +62,8 @@ CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET search_path = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
@ -67,42 +72,30 @@ SET default_with_oids = false;
-- Name: facebook_page; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.facebook_page (
id integer NOT NULL,
groupid character varying(100) NOT NULL,
pageid character varying(200) NOT NULL,
name character varying(100) NOT NULL,
CREATE TABLE facebook_page (
id character varying(200) NOT NULL,
lastpost character varying(100) DEFAULT ''::character varying NOT NULL,
tmpl character varying(200) DEFAULT ''::character varying NOT NULL,
ctime timestamp with time zone DEFAULT now() NOT NULL,
mtime timestamp with time zone DEFAULT now() NOT NULL
);
--
-- Name: facebook_page_id_seq; Type: SEQUENCE; Schema: public; Owner: -
-- Name: line_fb_rt; Type: TABLE; Schema: public; Owner: -
--
CREATE SEQUENCE public.facebook_page_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: facebook_page_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.facebook_page_id_seq OWNED BY public.facebook_page.id;
CREATE TABLE line_fb_rt (
line character varying(100) NOT NULL,
facebook character varying(200) NOT NULL,
tmpl character varying(300) DEFAULT ''::character varying NOT NULL
);
--
-- Name: line_group; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.line_group (
CREATE TABLE line_group (
id character varying(100) NOT NULL,
name character varying(200) NOT NULL,
notify boolean DEFAULT false NOT NULL,
@ -113,44 +106,34 @@ CREATE TABLE public.line_group (
--
-- Name: twitch_channel; Type: TABLE; Schema: public; Owner: -
-- Name: line_twitch_rt; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.twitch_channel (
id integer NOT NULL,
twitchid character varying(100) NOT NULL,
name character varying(200) NOT NULL,
type character varying(100) DEFAULT ''::character varying NOT NULL,
ctime timestamp with time zone DEFAULT now() NOT NULL,
mtime timestamp with time zone DEFAULT now() NOT NULL,
groupid character varying(100) NOT NULL
CREATE TABLE line_twitch_rt (
line character varying(100) NOT NULL,
twitch character varying(100) NOT NULL,
tmpl character varying(300) DEFAULT ''::character varying NOT NULL
);
--
-- Name: twitch_channel_id_seq; Type: SEQUENCE; Schema: public; Owner: -
-- Name: twitch_channel; Type: TABLE; Schema: public; Owner: -
--
CREATE SEQUENCE public.twitch_channel_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: twitch_channel_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.twitch_channel_id_seq OWNED BY public.twitch_channel.id;
CREATE TABLE twitch_channel (
id character varying(100) NOT NULL,
name character varying(200) NOT NULL,
type character varying(100) DEFAULT ''::character varying NOT NULL,
ctime timestamp with time zone DEFAULT now() NOT NULL,
mtime timestamp with time zone DEFAULT now() NOT NULL
);
--
-- Name: version_ctrl; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.version_ctrl (
CREATE TABLE version_ctrl (
version integer NOT NULL,
ctime timestamp with time zone DEFAULT now() NOT NULL,
querystr text DEFAULT ''::character varying NOT NULL
@ -158,64 +141,82 @@ CREATE TABLE public.version_ctrl (
--
-- Name: facebook_page id; Type: DEFAULT; Schema: public; Owner: -
-- Name: facebook_page facebook_page_id_pk; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.facebook_page ALTER COLUMN id SET DEFAULT nextval('public.facebook_page_id_seq'::regclass);
ALTER TABLE ONLY facebook_page
ADD CONSTRAINT facebook_page_id_pk PRIMARY KEY (id);
--
-- Name: twitch_channel id; Type: DEFAULT; Schema: public; Owner: -
-- Name: line_fb_rt line_fb_rt_facebook_line_pk; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.twitch_channel ALTER COLUMN id SET DEFAULT nextval('public.twitch_channel_id_seq'::regclass);
--
-- Name: facebook_page facebook_page_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.facebook_page
ADD CONSTRAINT facebook_page_pkey PRIMARY KEY (id);
ALTER TABLE ONLY line_fb_rt
ADD CONSTRAINT line_fb_rt_facebook_line_pk PRIMARY KEY (facebook, line);
--
-- Name: line_group line_group_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.line_group
ALTER TABLE ONLY line_group
ADD CONSTRAINT line_group_pkey PRIMARY KEY (id);
--
-- Name: twitch_channel twitch_channel_pkey; Type: CONSTRAINT; Schema: public; Owner: -
-- Name: line_twitch_rt line_twitch_rt_line_twitch_pk; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.twitch_channel
ADD CONSTRAINT twitch_channel_pkey PRIMARY KEY (id);
ALTER TABLE ONLY line_twitch_rt
ADD CONSTRAINT line_twitch_rt_line_twitch_pk PRIMARY KEY (line, twitch);
--
-- Name: twitch_channel twitch_channel_id_pk; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY twitch_channel
ADD CONSTRAINT twitch_channel_id_pk PRIMARY KEY (id);
--
-- Name: line_group_name_uindex; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX line_group_name_uindex ON public.line_group USING btree (name);
CREATE UNIQUE INDEX line_group_name_uindex ON line_group USING btree (name);
--
-- Name: facebook_page facebook_page_line_group_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
-- Name: line_fb_rt line_fb_rt_facebook_page_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.facebook_page
ADD CONSTRAINT facebook_page_line_group_id_fk FOREIGN KEY (groupid) REFERENCES public.line_group(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE ONLY line_fb_rt
ADD CONSTRAINT line_fb_rt_facebook_page_id_fk FOREIGN KEY (facebook) REFERENCES facebook_page(id) ON DELETE CASCADE;
--
-- Name: twitch_channel twitch_channel_line_group_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
-- Name: line_fb_rt line_fb_rt_line_group_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.twitch_channel
ADD CONSTRAINT twitch_channel_line_group_id_fk FOREIGN KEY (groupid) REFERENCES public.line_group(id);
ALTER TABLE ONLY line_fb_rt
ADD CONSTRAINT line_fb_rt_line_group_id_fk FOREIGN KEY (line) REFERENCES line_group(id) ON DELETE CASCADE;
--
-- Name: line_twitch_rt line_twitch_rt_line_group_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY line_twitch_rt
ADD CONSTRAINT line_twitch_rt_line_group_id_fk FOREIGN KEY (line) REFERENCES line_group(id) ON DELETE CASCADE;
--
-- Name: line_twitch_rt line_twitch_rt_twitch_channel_id_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY line_twitch_rt
ADD CONSTRAINT line_twitch_rt_twitch_channel_id_fk FOREIGN KEY (twitch) REFERENCES twitch_channel(id) ON DELETE CASCADE;
--