add youtube new video notify
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const axios = require('axios')
|
||||
const config = require('../../config')
|
||||
const qs = require('querystring')
|
||||
|
||||
const queryYoutubeName = async (id = '') => {
|
||||
if (typeof id !== 'string' || id.trim().length === 0) return null
|
||||
@@ -16,11 +17,14 @@ const queryYoutubeName = async (id = '') => {
|
||||
method: 'get',
|
||||
params
|
||||
})
|
||||
if (!result || !('data' in result) || !('items' in result.data) || Array.isArray(result.data.items) || result.data.items.length === 0) return null
|
||||
|
||||
if (!result || !('data' in result) || !('items' in result.data) || !Array.isArray(result.data.items) || result.data.items.length === 0) return null
|
||||
let data = result.data.items[0]
|
||||
console.log('youtube data ::: ', JSON.stringify(data, null, 2))
|
||||
if (!('snippet' in data) || !('title' in data.snippet)) return null
|
||||
return data.snippet.title
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -32,6 +36,7 @@ const subYoutube = async (id = '') => {
|
||||
|
||||
let data = {
|
||||
'hub.mode': 'subscribe',
|
||||
'hub.verify': 'async',
|
||||
'hub.topic': `https://www.youtube.com/xml/feeds/videos.xml?channel_id=${id}`,
|
||||
'hub.callback': `${config.url.replace(/\/$/, '')}/google/youtube/webhook?id=${id}`,
|
||||
'hub.lease_seconds': 86400
|
||||
@@ -39,11 +44,16 @@ const subYoutube = async (id = '') => {
|
||||
|
||||
try {
|
||||
await axios({
|
||||
data,
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: qs.stringify(data),
|
||||
url,
|
||||
method: 'post'
|
||||
})
|
||||
} catch (err) {}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
@@ -404,7 +404,9 @@ const addYoutube = async (txt = '', source = {}, db) => {
|
||||
return { reply }
|
||||
}
|
||||
|
||||
console.log('youtube id ::: ', id)
|
||||
let youtubeName = await api.google.queryYoutubeName(id)
|
||||
console.log('youtubr name :::: ', youtubeName)
|
||||
if (youtubeName === null) return null
|
||||
|
||||
text = `select "id" from "public"."youtube_channel" where "id" = $1`
|
||||
@@ -436,7 +438,9 @@ const addYoutube = async (txt = '', source = {}, db) => {
|
||||
|
||||
try {
|
||||
await api.google.subYoutube(id)
|
||||
} catch (err) {}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
|
||||
return {
|
||||
reply: 'add youtube channel success'
|
||||
|
||||
@@ -27,7 +27,7 @@ const runAct = async (cmd, txt = '', source = {}) => {
|
||||
if (!cmd || typeof cmd !== 'string' || cmd.length === 0) return null
|
||||
if (!(cmd in cmds)) return null
|
||||
let result = await cmds[cmd](txt, source)
|
||||
return result
|
||||
return result.reply
|
||||
}
|
||||
|
||||
module.exports = runAct
|
||||
|
||||
@@ -17,6 +17,7 @@ const textMessage = async (evt) => {
|
||||
let result = await commands(text, source)
|
||||
if (result === null) return
|
||||
if (typeof result === 'object' && 'reply' in result) {
|
||||
console.log('reply message :::: ', result.reply)
|
||||
await api.line.replyMessage(replyToken, result.reply)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user