Test Your Bots Locally with Ngrok

ItamarMItamarM RSS Feed

Wouldn’t it be great if development could be simplified further by allowing you to test requests directly and without the need to constantly redeploy your work to a host platform or webserver?

As you may know Viber uses webhook callbacks for event interaction between two systems connected over the Internet (in this case, Viber and your incredible chat bot). This is great when both systems are deployed on the public internet, but what about when you’re developing locally? How is Viber supposed to talk to the NodeJS app running on localhost?

Since the bot service is running on your local machine, you’ll need to install something to forward Viber’s request to your local development environment.

Enter ngrok. This free tool lets you set up a secure tunnel to your localhost, which is a fancy way of saying it opens access to your local app from the internet.

Today, I’d like to show you how to use it to facilitate local chat bot development.

ngrok webhooks

Step one: Download ngrok

ngrok is a Go program, distributed as a single executable file for all major desktop platforms. Hence it doesn’t require you to install additional frameworks or any other dependencies. Download the version for your development system of choice and simply unzip the file somewhere on your computer.

Step two: Expose your chat bot service to the internet

Once you have ngrok installed, you can expose the app running on your local machine to the Internet. Just tell ngrok what port your web server is listening on.

This port should be your service’s port. For example on our is it up bot tutorial we used port 8080. You should use the port you defined for your bot.

In a Terminal window, navigate to the directory where you unzipped ngrok. Going forward you may want to add ngrok to your system path to allow you access to the ngrok command from any directory. For now we will run the ngrok utility directly from your default location.

Now start ngrok, telling it which port we want to expose to the public Internet. In the Terminal type ./ngrok http 8080 on Linux and Mac, or just ngrok http 8080 on Windows. After starting up, you should see something like the following:

ngrok tunnel

You’ll see some outputs include “Forwarding” URLs for http and https. These are the URLs where your application is accessible on the internet.

For this example we’ll use the secure https URL, which will look something like this: https://d2097a67.ngrok.io (where the “d2097a67” will be some other random identifier.)

Step three: Configure Viber to work with your local machine

Now set your new webhook to Viber. This can be done by placing a call to the set_webhook API method with your new ngrok url.

For your convenience we made this NodeJS module which extracts the ngrok url so you can set it easily.

Next steps

We’ve only just scratched the surface of what’s possible with ngrok. You might want to sign up for an account at ngrok.com, where you can reserve a custom subdomain and configure HTTP basic authentication for access to your tunnel. Check out their docs for more uses and information.

Need help or found a bug? Contact us.