Skip to content

Instantly share code, notes, and snippets.

@devrelv
Last active May 25, 2021 03:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save devrelv/44f5c430d54b31e4ede866f226537700 to your computer and use it in GitHub Desktop.
Save devrelv/44f5c430d54b31e4ede866f226537700 to your computer and use it in GitHub Desktop.
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