Skip to content

Commit

Permalink
Add post notify message & fix db attribute handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RPing committed Oct 27, 2017
1 parent 486aaff commit 543f747
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
35 changes: 35 additions & 0 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const site = require('./lib/site-utils')
const checkTime = new Date()
checkTime.setDate(checkTime.getDate() - process.env.CHECK_DAYS) // current time - CHECK_DAYS

const postNotifyMsg =
'If you have any question, post an issue in\nhttps://github.com/RPing/Ver.bot-notify/issues\n' +
'Or if you find some security issue, contact me\ng1222888@gmail.com\n' +
'To support AWS Lambda and EC2 running, please donate\nhttps://goo.gl/9czXSn (paypal link)'

function needToNotify(results) {
const hasNewVer = results.info.length > 0
const hasSubscriber = results.subscribers.length > 0
Expand Down Expand Up @@ -41,12 +46,42 @@ function notify(projectPlatform, projectName, releasePage, results, cb) {
)
}

function postNotify(subscribers, cb) {
const fnList = []
subscribers.forEach((subscriber) => {
fnList.push((inner_cb) => {
site.siteUtil(subscriber.platform)
.send(subscriber.id, postNotifyMsg, (err) => {
if (err) {
console.error('---- error when post-notify ----')
console.error(subscriber.platform, subscriber.id)
console.error(err)
return inner_cb(err)
}
inner_cb(null)
})
})
})

async.parallel(
async.reflectAll(fnList),
function parallelCallback(err, parallel_results) {
const hasSomeError = parallel_results.some(result => result.error !== undefined)

cb(hasSomeError)
}
)
}

function checkAndNotify(results, projectInfo, projectName, projectPlatform, cb) {
if (needToNotify(results)) {
async.series([
function (inner_cb) {
const releasePage = site.siteUtil(projectPlatform).getReleasesPage(projectInfo)
notify(projectPlatform, projectName, releasePage, results, inner_cb)
},
function (inner_cb) {
postNotify(results.subscribers, inner_cb)
}
], function seriesCallback(err) {
if (err)
Expand Down
10 changes: 3 additions & 7 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ exports.getAllSubscriber = function (project_name, cb) {
ExpressionAttributeValues: {
':x': project_name
},
ProjectionExpression: 'subscriber_and_platform'
ProjectionExpression: 'subscriber_id, subscriber_platform'
}

function onQuery(err, data) {
Expand All @@ -68,13 +68,9 @@ exports.getAllSubscriber = function (project_name, cb) {

const list = []
data.Items.forEach((item) => {
const a = item.subscriber_and_platform.split('-')
const platform = subscriberPlatform(parseInt(a[1]))
const id = a[0]

list.push({
platform: platform,
id: id
platform: subscriberPlatform(item.subscriber_platform),
id: item.subscriber_id
})
})

Expand Down
12 changes: 12 additions & 0 deletions lib/sites/subscriber/skype.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class skype {
_singleton = this
}

send(chat_id, msg, cb) {
const appId = process.env.SKYPE_APP_ID
const appSecret = process.env.SKYPE_APP_SECRET

this._requestToken(appId, appSecret, (err, token) => {
if (err)
return cb(err)

this._sendMsg(token, chat_id, msg, cb)
})
}

notify(chat_id, project_name, release_page, version_info, cb) {
const appId = process.env.SKYPE_APP_ID
const appSecret = process.env.SKYPE_APP_SECRET
Expand Down
26 changes: 26 additions & 0 deletions lib/sites/subscriber/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ class slack {
_singleton = this
}

send(chat_id, msg, cb) {
const bot_token = process.env.SLACK_BOT_TOKEN
const options = {
url: 'https://slack.com/api/chat.postMessage',
method: 'POST',
timeout: 3000,
form: {
token: bot_token,
text: msg,
channel: chat_id,
},
}

request(options, (err, res) => {
if (err)
return cb(err)

if (typeof res === 'undefined')
return cb(new Error(`Timeout Error. res object is ${res}`))
else if (res.statusCode !== 200)
return cb(new Error(`res code is ${res.statusCode}`))

cb(null)
})
}

notify(chat_id, project_name, release_page, version_info, cb) {
const bot_token = process.env.SLACK_BOT_TOKEN
const options = {
Expand Down
26 changes: 26 additions & 0 deletions lib/sites/subscriber/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ class telegram {
_singleton = this
}

send(chat_id, msg, cb) {
const bot_token = process.env.TELEGRAM_BOT_TOKEN
const options = {
url: `https://api.telegram.org/bot${bot_token}/sendMessage`,
method: 'POST',
timeout: 3000,
json: {
chat_id: chat_id,
text: msg,
parse_mode: 'Markdown',
},
}

request(options, (err, res) => {
if (err)
return cb(err)

if (typeof res === 'undefined')
return cb(new Error(`Timeout Error. res object is ${res}`))
else if (res.statusCode !== 200)
return cb(new Error(`res code is ${res.statusCode}`))

cb(null)
})
}

notify(chat_id, project_name, release_page, version_info, cb) {
const bot_token = process.env.TELEGRAM_BOT_TOKEN
const options = {
Expand Down

0 comments on commit 543f747

Please sign in to comment.