No action in params in github webhook payload - ruby-on-rails

I am sending to an organization webhook the pull_request payload as described here : https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
But while in my script i can get any parameter from the payload using
params[:foo][:bar]
i cannot get the first param which is the action using
params[:action]
it's not giving anything
i am reading here that it should be there under params[:action] : https://guides.rubyonrails.org/v3.2/action_controller_overview.html
I don't know what i am doing wrong the webhook is sending the pull_request even as expected but the param is just not there, when i check the dump is starts with the pr number not the action.

Related

How to access params body of a webhook post request

I'm trying to send myself an email with the details of a post request received from a webhook
p = params.permit!
selfMailer.send_myself_an_email(p).deliver_later
However this only send me an email with the content:
{"controller"=>"custom/send", "action"=>"create"}
I tried to call .inspect on the params but not much changed, this is what I got:
"#<ActionController::Parameters {\"controller\"=>\"custom/send\", \"action\"=>\"create\"} permitted: true>"
In the log I can see clearly the parameters passed, there are many, but above is all I get! so how can I access them?
wow! I haven't thought about it, it seems that I made my app to redirect www.example.com to example.com, so basically when the webhook request arrived to the www url it was redirected, that's why I wasn't seeing the post params!
I just adjusted the webhook url by removing the www and now it's working

Ruby on rails api post request issue

i am trying to post using postman to a rails api that i made, the actual request goes in and creates an entry, but nothing but the ID gets recorded. attached are the files for that.
You need to pass the post params and not just the id into the list.new call and make sure you're sending up the correctly namespaced values in the post request.
Step 1.
In create you need to do
#list = List.new(list_params)
Step 2.
Postman needs to be putting all the params into the list[] namespace
ie. list[title] rather than just title.

Twilio post to url with params[:text_response] and params[:phone_number]

I am using ruby 2.0.0 and Rails 4.0.
I am sending a text message out via my rails app to my end user.
When they respond, I want to redirect them to an api call:
www.myapp.com/api/verify_text_response
Within that API call, I want to see what the text message sent from the end user to my url is. Ideally, I would receive a param of "text_response" that I could then do what I wanted to with.
How can I redirect a reply from my end_user to the above url, and capture the phone number it came from as well as the message sent to me in my params? If this isn't possible, how can I use TwiML to do something similar?
End Goal
For what it's worth, this is what I'm trying to accomplish:
I send a text message out - "Would you like to subscribe?"
The end user responds "Yes" or "No".
I change the subscribed attribute on my subscription model to true or false.
I send another text message saying either "You are subscribed." or "You are not subscribed.".
Item's #3 and #4 are based on the user responding "Yes" or "No" in item #2.
The twilio guide here for Ruby is the most useful documentation out there. They recommend using the Twilio gem in your Rails application by adding the twilio-ruby gem to your Gemfile.
All you need to do to is add the following code to one of your controller's actions (the one that is routed to by www.myapp.com/api/verify_text_response:
def receive_text
# Not exactly sure this is the right parameter, but it's easy to test
sender_message = params[:Body]
response = if (sender_message == "Yes")
"You are subscribed."
else
"You are not subscribed."
end
twiml = Twilio::TwiML::Response.new do |r|
r.Message(response)
end
twiml.text
end
To make your Rails application accessible to Twilio, follow the directions found on this page:
Copy and paste the URL of your server into the "SMS" URL of a number
on the Numbers page of your Twilio Account. On the page for that
number, change the Method from "POST" to "GET".
I wasn't exactly sure from looking at the documentation which parameter holds the user's response (I thought it was probably params[:Body]), but the best way to figure out is simply by printing out the parameters that the controller receives when you send a text message to your Twilio number.

Using Webhooks with Rails

I am unfamiliar with Webhooks but I feel like they are the right thing for my app.
I have the following documentation for FluidSurveys webhook
I understand how I can make the webhook through a POST request to their API, but I don't know how can I tell where the webhook is actually going to send the response.
Can I pass any subscription url I want? e.g. https://www.myapp.com/test and is that where webhook will send the data? Also, after the webhook is created I'm not sure how to ensure my Rails app will receive the response that is initiated.
I assume a controller method that corresponds with the url I provide to the webhook.
If I'm correct on the controller handling the webhook, what would that look like?
Any guidance is appreciated.
Webhooks hook into your app via a callback URL you provide. This is just an action in one of your controllers that responds to POST requests and handles the webhook request. Every time something changes to the remote service, the remote service makes a request to the callback URL you provided, hence triggering the action code.
I'll exemplify with the survey created event. You start by defining a callback action for this event, where you handle the request coming from the webhook. As stated here the webhook responds with the following body:
survey_creator_name=&survey_name=MADE+A+NEW+SURVEY&survey_creator_email=timothy#example.com&survey_url=http%3A%2F%2Fexample.com%2Fsurveys%2Fbob%2Fmade-a-new-survey%2F``
Let's leave the headers for now, they don't contain important information. The available body parameters (survey_creator_name, survey_name etc.) will reflect all details regarding the new survey available on the remote service. So let's write a callback action that handles this request:
class HooksController
def survey_created_callback
# If the body contains the survey_name parameter...
if params[:survery_name].present?
# Create a new Survey object based on the received parameters...
survey = Survey.new(:name => params[:survey_name]
survey.url = params[:survey_url]
survey.creator_email = params[:survey_creator_email]
survey.save!
end
# The webhook doesn't require a response but let's make sure
# we don't send anything
render :nothing => true
end
end
Let's add the route for this (in config/routes.rb):
scope '/hooks', :controller => :hooks do
post :survey_created_callback
end
This will enable the POST /hooks/survey_created_callback route.
Now you'll need to subscribe this callback URL to the Webhooks API. First you'll want to know which hooks are available to you. You do this by placing a GET request at /api/v2/webhooks/. In the response you'll find the event name, survey and collector parameters.
Finally, you subscribe to one of the previously listed hooks by placing a request to the POST /api/v2/webhooks/subscribe/ URL with the following contents:
{
"subscription_url": "http://your-absolute-url.com/hooks/survey_created_callback",
"event": "[EVENT NAME FROM THE HOOKS LIST]",
"survey": "[SURVEY FROM THE HOOKS LIST]",
"collector": "[COLLECTOR FROM THE HOOKS LIST]"
}
The response to this will be a code 201 if the hook was created successfully, or code 409, if a webhook for the same callback URL already exists. Or something else, if this went bad :)
You can now test the hook, by creating a survey on the remote service and then watch it getting replicated in your Rails app.
Hope this helps...

Ruby on Rails 3: How to retrieve POST and GET params separately?

I know you can get parameters using params, but the thing is that it merges all parameters whether they were sent via GET or POST:
If you send a request via GET, no problem, the parameters can only be squeezed in the URL.
But if you send a POST request that has a URL like /blabla?foo=bar&foo2=bar2, is there a way to tell the difference between the variables sent via the URL and the variables sent through the actual POST method?
Yes, in your controller you can get the GET parameters using request.GET and the POST parameters with request.POST

Resources