Integrating the Flowdock Push API is a lot easier than I thought it would be. I decided to document how to get it set up.
Add these gems to your Gemfile:
gem 'flowdock'
gem 'dotenv-rails', :groups => [:development, :test]
Then run bundle
.
Next we need to configure our .env
file to have the correct token for the Flow we are trying to send messages to. To find a specific Flow’s token you:
.env
file.If you havn’t already, create a .env file in the main directory of your Rails app. IMPORTANT: make sure to put .env in your .gitignore file so you don’t leak API tokens onto your github.
Copy the flow token into your .env
file:
# .env file
FLOW_TOKEN=<paste your flow token here>
Restart the server so the token will be available to you. Now we need to set up a route that will handle the POST request for sending the message to the flow.
The form for sending a message is in the Dashboard of my app so this is the route I created:
# config/routes.rb
post '/dashboard/send_flow' => "dashboards#send_flow"
It is configured to hit my DashboardsController and execute the #send_flow action.
Here is what the #send_flow action looks like:
def send_flow
flow = Flowdock::Flow.new(:api_token => ENV['FLOW_TOKEN'],
:source => "LaunchPairs", :from => {:name => current_user.first_name, :address => current_user.email})
flow.push_to_chat(:content => params[:flow][:question], :external_user_name => current_user.first_name.gsub!(/\s/, ""))
flash[:success] = "Your question was sent! Check the flow for any answers"
redirect_to dashboard_path
end
First we are creating a new flow using the proper Flow Token which we set up earlier in the .env
file. The dotenv-rails gem loads that environment variable so it’s available to call in the action.
Next we call the push_to_chat
method on the flow and provide the content for the message. The params[:flow][:question] is coming from my form that hit’s this action. The final step, is to set up the form to hit this route.
How you build the form is up to you as long as it hits the correct controller and action. Here is what mine looks like:
<%= form_for 'flow', url: { controller: :dashboards, action: :send_flow } do |f| %>
<%= f.text_area :question %>
<%= f.submit "Ask Your Question", class: "button alert" %>
<% end -%>
I love teaching and writing new content but sometimes find it hard to justify.
Getting your email motivates me to spend more time creating awesome content and notifies you when new posts or screencasts come out.
I will never share your email or spam. Expect less than 5 emails a year and feel free to unsubscribe at any time.