Skip to content

Commit

Permalink
support web hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 7, 2018
1 parent 34c5cd2 commit 7c3505a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ if (!TOKEN) {
process.exit(1)
}


const HOSTNAME = process.env.HOSTNAME
if (!HOSTNAME) {
console.error('Missing HOSTNAME env var.')
process.exit(1)
}

const parseCmd = (msg) => {
if ('string' !== typeof msg.text) return null
Expand All @@ -37,7 +41,18 @@ const error = `\
*Oh snap! An error occured.*
Report this to my creator @derhuerst to help making this bot better.`

const api = new Api(TOKEN, {polling: true})
let api
if (process.env.NODE_ENV === 'production') {
console.info('using web hooks at ' + HOSTNAME)
api = new Api(TOKEN, {polling: false})
api.setWebHook(HOSTNAME, {
certificate: `/etc/letsencrypt/live/${HOSTNAME}/fullchain.pem`
})
} else {
console.info('using polling')
api = new Api(TOKEN, {polling: true})
}

api.on('message', async (msg) => {
log(msg)
const user = msg.from ? msg.from.id : msg.chat.id
Expand Down

0 comments on commit 7c3505a

Please sign in to comment.