How-to-Webhook
How to create a Webhook
Idea
This document will help you to create a Webhook in POWR.
Where should you create your Webhook?
First of all, it depends on the situation where you should put your webhook, but what I mentioned is only in one folder. You can see all webhooks in the folder: ~../powr/app/controllers/api/...
In this folder above you may find most of the webhooks.
When you created your webhook folder, inside you should create a .rb file. The name also depends, but in my situation it is simple. I just looked up all APIs and found out that I could just write webhooks_controller.rb. But you should clarify, with seniors or PMs. If they didn’t say anything about it, just name your file as I did.
Note: The class name of your webhooks should be like this:
The API that I am working on is Twilio, so my class name looks like this:
Methods in the Webhook
Methods in the Webhook depend on what reasons you are going to use it. For example in my ticket, I should use the POST method, so I wrote the “create” method, to implement the POST request method.
For example my method in the webhook:
I put a comment here that other my colleagues will understand for which path ‘listen’ this webhook.
Lines [8-12] shows how to convert incoming requests. I faced a lot of problems with stringIO, because, as I understood, it has other methods than just string, that's why I must convert it.
Be careful!
Webhook routings
Also an important thing to do. You should find the _config/api.rb _and write down your webhook route, so that it will be available in production.
For example, check the file: config/routes/api.rb
In this file you can find all api routes that are connected to the Webhooks or other API staff.
You should write the path to your webhook. Read other examples, I don’t know in which cases you are using webhooks. SO, IT DEPENDS.
How to check if a Webhook is working or not?
You have several options to check it:
Using fetch API. For example you can open a developer console in your browser (if it can run the fetch command) and write POST/GET method to trigger your webhook. fetch(“your_ngrok_url/path_to_your_webhook”, {
method: ‘POST’, // or ‘GET’
_ headers: {_
_ ‘Content-Type’:’application/json’, // or other if you have_
_},_
_body: “your_body_massage”,_
_._
_._
_._
_});_
You can modify this for your needs. [More](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).
- If you are facing the SMS problem you can go to textnow.com and send messages to your Twilio account number, be sure that your Twilio account messaging service number is connected to your webhook, which is your ngrok url. Don’t forget to print something in the console so that you will know which code is implemented.
- Try to test with post methods.
What is Ngrok and how do you eat it?
Ngrok is just a tool that helps you to create a tunnel to your localhost, so that your localhost can be available for the web. Long story short, your local web-app can be visible not only for you (locally).
How to install Ngrok and how to use it?
You should sign up first, and the whole instruction will be available for you.
**Note: **if you are getting an error that says a bad gateway or something like that, you should check that you started the server on another terminal, or just google it. In my experience most helpful was [this answer](https://stackoverflow.com/questions/40598428/ngrok-errors-502-bad-gateway#answer-62280361).
When you installed the Ngrok, you should paste the IP address that Ngrok created for you to your browser URL. Don’t forget to add this url to the service which you are using for webhook. I recommend you to use the https protocol, to be more safe. _Everytime you run ngrok, it updates the url, don’t forget to add a new url to your services for testing._
Your webhook’s request address will look something like this:
(Path to your webhook should be the same as your wrote in the api routings) _[https://random_generated_url:port/path_to_your_webhook](https://random_generated_url:port/path_to_your_webhook)_
Example: