Instantly share code, notes, and snippets.

@devrelv /get_public_url.js Secret
Last active Mar 23, 2018

Embed
What would you like to do?
ngrok with viber
var http = require("http");
var options = {
hostname: '127.0.0.1',
port: 4040,
path: '/api/tunnels',
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
};
module.exports.getPublicUrl = function() {
return new Promise((resolve, reject) => {
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(config) {
config = JSON.parse(config);
const httpsTunnel = config.tunnels.filter(t => t.proto === 'https').pop();
resolve(httpsTunnel.public_url);
});
});
req.on('error', function(e) {
reject(e.message);
});
req.end();
});
}
const ngrok = require('./get_public_url');
const ViberBot = require('./viber-bot').Bot;
// Creating the bot with access token, name and avatar
const bot = new ViberBot(logger, {
authToken: "YOUR_AUTH_TOKEN",
name: "YOUR_BOT_NAME",
avatar: "YOUR_BOT_AVATAR_URL"
});
const http = require('http');
const port = process.env.PORT || 8080;
return ngrok.getPublicUrl().then(publicUrl => {
console.log('Set the new webhook to"', publicUrl);
http.createServer(bot.middleware()).listen(port, () => bot.setWebhook(publicUrl));
}).catch(error => {
console.log('Can not connect to ngrok server. Is it running?');
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment