How to test Messenger Bots offline? Local webhooks
Asked Answered
L

5

12

I'm starting with Messenger Platform API. I want to make simple Messenger Chatbot. Here is tutorial I follow.

As you see, to start I need to set up webhook (step 2). So basically: web server that provides some REST API and posts some data back. The problem is: facebook requires me to provide some real, existing app URL that works currently in internet. So, do I need to upload my code to server each time I change something? Can I somehow test it locally (on my localhost)? How can I test behavior of my bot?

Livvi answered 18/4, 2016 at 15:40 Comment(3)
How would Facebook be able to ping your server that is offline?Apprehension
There is similar question, so you can find answer here: #36674579Aerograph
@Apprehension It's not necessary for facebook to connect it by itself. I was thinking about some kind of emulator or fake, localhosted instance that would be available just for testing.Livvi
S
30

There's a few services you can use to expose your webserver running on localhost to the public. Two options are localtunnel and ngrok.

An example (from localtunnel's quickstart) of how you might do this, given that your webserver is running at http://localhost:8000/ would look like this:

Install localtunnel from npm:

npm install -g localtunnel

Request a tunnel to your local server:

lt --port 8000

This will generate a link, of the form https://foo.localtunnel.me you can use to access your localhost from the web. One nice feature is that you automatically get https so Facebook won't give you a 'SSL certificate problem' error. Localtunnel or ngrok are also nice for sharing work running locally with others.

Note that the localtunnel url is only valid as long as the connection lives, so you will have to update the url Facebook has if the connection ends.

Saddlebacked answered 19/4, 2016 at 2:42 Comment(5)
I'm trying to use localtunnel, but it says "Invalid hostname" after trying to open given urlLivvi
do I need to configure something in windows? Maybe firewall?Livvi
OK, I've solved problem. Some IIS Express issue. Thanks for help ;)Livvi
@Ludwik11 No problem, glad to help!Saddlebacked
Here's a post related to an IIS Express issue: https://mcmap.net/q/99859/-exposing-localhost-to-the-internet-via-tunneling-using-ngrok-http-error-400-bad-request-invalid-hostnameEnvironment
O
16

I created a library that tries to solve this exact problem! With fb-local-chat-bot you can easily test your bot offline. The library also makes testing much more simple. If you're interested, definitely check it out

https://github.com/spchuang/fb-local-chat-bot

Demo:

Othella answered 19/8, 2016 at 8:23 Comment(0)
D
3

you may use ngrok to test the messenger bot in localhost. You may download it at: https://ngrok.com/download

on executing ngrok, it will generate secure link that can be used as a end point in webhook.

You can also check the detailed status of each request and response of ngrok through

http://localhost:4040/inspect/http

Later, once you are done, you may deploy your code to secure server.

You can find more info as to how to build a basic chat bot on the link below:

How to build a basic chat bot on facebook messenger

Designation answered 27/10, 2016 at 7:32 Comment(0)
M
1

You can deploy your backend services on heroku free of cost. It provide public DNS with RSA.

Marler answered 30/10, 2016 at 2:46 Comment(1)
His main concern is not related to hosting but to upload the code whenever he makes any change to it. This consumes a lot of time while you make small changes which needs to be tested.Viscount
C
1

If you can't use ngrok for some reason (like routing webhook to multiples dev machines). please try this emulator i have created for developing / debugging webhooks locally. this provides emulation of send api and a messenger ui

I have created an emulator for send api and messenger which i use for debugging web hooks locally

https://github.com/SonOfSardaar/facebook-send-api-emulator

I also came across this (i have not tried this one yet but looks good)

https://learn.microsoft.com/en-us/bot-framework/debug-bots-emulator

Cale answered 14/11, 2017 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.