Skip to content

Instantly share code, notes, and snippets.

@dadinugroho
Created May 2, 2021 04:45
Show Gist options
  • Save dadinugroho/fb02e6d89ceeea5ea6d4da6c068737cf to your computer and use it in GitHub Desktop.
Save dadinugroho/fb02e6d89ceeea5ea6d4da6c068737cf to your computer and use it in GitHub Desktop.
Running ngrok from nodejs and send the address through TelegramBot
# Repalce 1234567890:tHiSiStHeToKeNr3C31v3DfRoMb0Tf@Th3r with your token given by @BotFather
TELEGRAM_BOT_API=1234567890:tHiSiStHeToKeNr3C31v3DfRoMb0Tf@Th3r
/**
* ngmess
*
* Run ngrok and send the public addresses via TelegramBot
*
* @version: 0.0.1
* @author : dadinugroho
*
* How to:
* 1. Download ngrok and follow instruction from the ngrok website.
* 2. Make sure you can run ngrok successfuly.
* 3. Run this index.js or you can run using pm2 so that everytime the computer is running this script will automatically run.
*/
const { exec } = require('child_process');
// This is to handle issue node-telegram-bot-api deprecated Automatic enabling of cancellation of promises is deprecated.
process.env.NTBA_FIX_319 = 1;
const TelegramBot = require('node-telegram-bot-api');
const request = require('request');
const options = {
url: 'http://localhost:4040/api/tunnels',
method: 'GET'
};
require('dotenv').config();
// Starting the local ngrok installation from nodejs.
const ngrokProc = exec('./ngrok start --all', (error, stdout, stderr) => {
if (error) {
console.table(error);
}
console.log(`Child Process STDOUT: ${stdout}`);
console.log(`Child Process STDERR: ${stderr}`);
});
ngrokProc.on('exit', function (code) {
console.log('Child process exited with exit code ' + code);
});
// Setup the TelegramBot
const tBot = new TelegramBot(process.env.TELEGRAM_BOT_API, { polling: true });
tBot.on('message', (msg) => {
if ('cibot' === msg.text.toString()) {
request(options, (err, res, body) => {
if (err) {
tBot.sendMessage(msg.chat.id, err);
} else {
const result = JSON.parse(body);
result.tunnels.forEach(row => tBot.sendMessage(msg.chat.id, `${row.uri} => ${row.public_url}`));
}
});
}
});
{
"name": "ngmess",
"version": "0.0.1",
"description": "Running ngrok using nodejs and send the address via TelegramBot",
"main": "index.js",
"author": "dadinugroho",
"license": "ISC",
"dependencies": {
"dotenv": "^8.2.0",
"node-telegram-bot-api": "^0.53.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment