How to test Messenger Bots offline? Local webhooks - facebook

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?

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.

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:

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

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

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

Related

How can I test Customer Chat Plugin (beta) in localhost?

I'm trying use Customer Chat Plugin in my website (is not a Wordpress), but to test if the plugins is working I need deploy it many times.
Any way to test in my local machine? using localhost address?
You can use ngrok. It will expose your website running in localhost to internet and will provide you https url that you can use to test the feature. Remember to add ngork provided url to whitelisted domain in your facebook page settings before you test it.
didn't test it, but you could setup hosts file to serve your page on fake domain mapped to your local ip

Using Advance Rest API for IPN in localhost

i have read that in in able to test IPN in localhost magento site ,i can use Advance Rest API in google chrome ,now i downloaded already the app and test some values, but it showed The response was Empty..
how to test it correctly? am i doing the right thing there in the picture above? thank you,
You cannot test IPN on your localhost, as IPN is all about PayPal's server initiating a server-side POST to a URL you define.
As a result, your IPN script must be accessible by the outside world (or you can use a tunnel such as ngrok.me/localtunnel.me)
You can test on localhost using ngrok :- https://ngrok.com/
Simply run ngrok locally then paste test url that ngrok gives you (something like http://1bc7d09d.ngrok.com/)
It provides a tunnel to your localhost.
More information visit this URL :-Paypal Sandbox Test Tool IPN Simulator in Localhost

Can i use localhost as a URL Callback in a messenger webhook

Good evening, just saw that Facebook released his messenger bot toolkit and i immediately jumped right into it to learn more about it and maybe try to do my own.
My problem is that i don't have a https website running and it requires a https valid url. I tried to use my local web-server that has a certificate but it doesn't work.
My question is if this is possible to be done using a localhost url at all.
Thank you in advance
Actually this is possible with localhost. Use ngrok. It allows you to open localhost to the public web, over http or https. This should only be used for testing however.
If you want to test webhooks on your local environment, I would try ultrahook.com, you can get an API Key for free and the tool creates a tunnel from a public URL to your computer. This is from their FAQs page:
You download and run the UltraHook client on your computer. It
connects to UltraHook servers in the cloud and creates a tunnel from a
public endpoint on our servers to your computer. Any HTTP POST
requests sent to the public end point will be sent through the tunnel
an delivered to a private endpoint accessible from your computer.
I have used it to test webhooks from different providers (like payment gateways). In your computer, you can run something like:
ultrahook <subdomain> http://localhost:8000/webhook/
and then configure the webhook URL in your external service to something like <subdomain>.ultrahook.com
My question is if this is possible to be done using a localhost url at all.
No, of course it isn’t – because what such a “callback” actually means, is that Facebook makes a request to your server – and that is hardly possible with localhost.
A valid SSL certificate for your website is easy to get for free these days, via LetsEncrypt. And even if that is not available on your server, there’s still StartSSL, that provide basic certificates for free. All you need is a server you can install them on, or upload them to, or whatever mechanism your hoster provides for it. (And if they don’t provide any, then it might be time to switch.)

Ionic app REST calls to webapi hosted on localhost works using ionic serve, but not when I use "ionic run"

Using Ionic serve -- My Ionic app returns data from my REST call to my webapi running on localhost.
But when I issue Ionic run , my REST API call fails. The error.data is blank, so I can't tell what the error is.
I am also able to hit the api of the rest call directly in my browser and see that it returns the data.
Would anybody please give me some pointers as to how to debug this, or why my http calls are failing in run mode?
ionic serve run on browser and ionic run with devices[ios or android]
change localhost to ip address(in your system)
ex- 198.1.1.0:4000(eg.4000 is port number of local server)
and type(CLI)
ionic serve --address 198.1.1.0
check on Mobile browser type 198.1.1.0:4000
Note
If you are using a live server for your REST application,you will run into CORS issues.
I would suggest, you use Ionic's proxy server to do all your request. You will not run in any OPTION request or null request response etc etc.
Check This Official Blog and,
This will also be very helpful Ionic Proxy Example
localhost server is only accessible from your local environment (laptop). Assuming you are running on an actual device, it cannot access that web api in a normal way.
You can use following steps:
Open hotspot from your device
Connect your laptop to the network
Open terminal and type in ipconfig
Call your REST with the specified IP address.
You can find detailed reference here.
How can I access my localhost from my Android device?
I found the solution to my problem in this blogpost. It explained the network host configuration used by GenyMotion emulator : http://bbowden.tumblr.com/post/58650831283/accessing-a-localhost-server-from-the-genymotion
Using this I was able to get the ip address that I should use when invoking my service, that was hosted on my local IIS server.

How to test Facebook Real-time updates

In order to publish real-time updates to my app, Facebook needs needs to perform a post request to my server.
Problem is, my server is my home computer and not publicly addressable from the internet. Bringing a server live to implement this sounds like it could be a pain... can't attach debugger, fiddler etc....
So what's the best way to test the Http Endpoint? Integration tests that simulate the Facebook server? Fiddling with firewalls/NAT to try and get Facebook talking to my home computer?
Any ideas?
You can use ngrok - https://ngrok.com/ - free (pay-what-you-can) service that does exactly what you need. Localtunnel service is down and the developers also recommend ngrok.
In the past, I've used LocalTunnel to do this. It's a nice wrapper around an SSH tunnel and it effectively assigns you a subdomain at localtunnel.com pointing to a port on your localhost.
So basically, when you run it it will spit back an externally accessible sub domain name like xyz.localtunnel.com who's port 80 will point a port you specify on your local box.
You can find it at: http://progrium.com/localtunnel/
It's really great for testing various pubsubhubbub subscription feeds (like Facebook's).
OK! I think NAT should be the best bet and I don't see a reason for it not to work. You should try it out.
It was actually pretty easy - Logged into my home router, set up port forwarding on port 80 to the local IP of my computer, put an exception in windows firewall for port 80. and then navigate to my public IP address in the browser.
Implement the receiver samples at: https://github.com/facebook/real-time/tree/master/samples
The only answer is to get a webserver that is publicly accessible for real-time updates to be able to call back to.
There's lots of free webhosts that allow server-side scripting. And there's lots of paid for webhosts out there too. Stackoverflow is really not the place to get leads on where/when/why/howmuch for web hosting.
No you can't use ngrok only to simulate facebook realtime update since you must make a call to facebook servers with your ngrok adress to validate it (tell me if you find out how to do this :p ).
I use an openshift server to receive facebook realtime and then post evry json data received from facebook to my ngrok adress. So the process is
set up an openshift server to receive facebook notifications
Facebook sends notifications to your openshift
your openshift sends datas (as received) to your ngrok adress
And if you must receive facebook notifications on a local website (like www.website.dev/fb-notifications/) then create a script in your localhost folder which receives openshift posts (let's call it tunelscript.php). the process will be
set up an openshift server to receive facebook notifications
Facebook sends notifications to your openshift
your openshift sends datas (as received) to your tunel script via your ngrok adress (perso.ngrok.com/tunelscript.php)
Relay datas from your tunelscript to your local website (tunelscript.php => www.website.dev/fb-notifications/)
That's Tuneling B-)