Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions app/controllers/whatsapp_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class WhatsappController < ApplicationController
protect_from_forgery except: :create
skip_before_action :authenticate_user!

def create
# render plain: params["hub.challenge"], status: :ok

# if params[:entry][0][:id] == "332066249987383"
# binding.pry
# end

render plain: "OK", status: :ok
end

private

def callback
params.dig(:entry, 0, :changes, 0)
end
end


# curl -i -X POST \
# https://graph.facebook.com/v19.0/320958887765426/messages \
# -H 'Authorization: Bearer abc123' \
# -H 'Content-Type: application/json' \
# -d '{ "messaging_product": "whatsapp", "to": "17737898255", "type": "template", "template": { "name": "hello_world", "language": { "code": "en_US" } } }'



# {"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBJBODFGN0ExNDc2NUUzODBFNTEA", "status"=>"read", "timestamp"=>"1715986294", "recipient_id"=>"17737898255"}]}, "field"=>"messages"}]}], "whatsapp"=>{"object"=>"whatsapp_business_account", "entry"=>[{"id"=>"332066249987383", "changes"=>[{"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBJBODFGN0ExNDc2NUUzODBFNTEA", "status"=>"read", "timestamp"=>"1715986294", "recipient_id"=>"17737898255"}]}, "field"=>"messages"}]}]}}





# {"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBJBODFGN0ExNDc2NUUzODBFNTEA", "status"=>"delivered", "timestamp"=>"1715986294", "recipient_id"=>"17737898255", "conversation"=>{"id"=>"2bb8fba1d906609da4c5cbdac61f5bf1", "origin"=>{"type"=>"utility"}}, "pricing"=>{"billable"=>true, "pricing_model"=>"CBP", "category"=>"utility"}}]}, "field"=>"messages"}]}], "whatsapp"=>{"object"=>"whatsapp_business_account", "entry"=>[{"id"=>"332066249987383", "changes"=>[{"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBJBODFGN0ExNDc2NUUzODBFNTEA", "status"=>"delivered", "timestamp"=>"1715986294", "recipient_id"=>"17737898255", "conversation"=>{"id"=>"2bb8fba1d906609da4c5cbdac61f5bf1", "origin"=>{"type"=>"utility"}}, "pricing"=>{"billable"=>true, "pricing_model"=>"CBP", "category"=>"utility"}}]}, "field"=>"messages"}]}]}}





# {"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBJBODFGN0ExNDc2NUUzODBFNTEA", "status"=>"sent", "timestamp"=>"1715986294", "recipient_id"=>"17737898255", "conversation"=>{"id"=>"2bb8fba1d906609da4c5cbdac61f5bf1", "expiration_timestamp"=>"1716070440", "origin"=>{"type"=>"utility"}}, "pricing"=>{"billable"=>true, "pricing_model"=>"CBP", "category"=>"utility"}}]}, "field"=>"messages"}]}], "whatsapp"=>{"object"=>"whatsapp_business_account", "entry"=>[{"id"=>"332066249987383", "changes"=>[{"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBJBODFGN0ExNDc2NUUzODBFNTEA", "status"=>"sent", "timestamp"=>"1715986294", "recipient_id"=>"17737898255", "conversation"=>{"id"=>"2bb8fba1d906609da4c5cbdac61f5bf1", "expiration_timestamp"=>"1716070440", "origin"=>{"type"=>"utility"}}, "pricing"=>{"billable"=>true, "pricing_model"=>"CBP", "category"=>"utility"}}]}, "field"=>"messages"}]}]}}





# {"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBIzMzIyMkQ1MUJCRTRFMTcwOTEA", "status"=>"read", "timestamp"=>"1715985847", "recipient_id"=>"17737898255"}]}, "field"=>"messages"}]}], "whatsapp"=>{"object"=>"whatsapp_business_account", "entry"=>[{"id"=>"332066249987383", "changes"=>[{"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"},

# "statuses"=>[{"id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgARGBIzMzIyMkQ1MUJCRTRFMTcwOTEA", "status"=>"read", "timestamp"=>"1715985847", "recipient_id"=>"17737898255"}]}, "field"=>"messages"}]}]}}





# # # reply


# {"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"}, "contacts"=>[{"profile"=>{"name"=>"Keith Schacht"}, "wa_id"=>"17737898255"}], "messages"=>[{"from"=>"17737898255", "id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgASGBQzQUFBNTU5NEVCRTFBNzlCRkY4RAA=", "timestamp"=>"1715986308", "text"=>{"body"=>"Yes"}, "type"=>"text"}]}, "field"=>"messages"}]}], "whatsapp"=>{"object"=>"whatsapp_business_account", "entry"=>[{"id"=>"332066249987383", "changes"=>[{"value"=>{"messaging_product"=>"whatsapp", "metadata"=>{"display_phone_number"=>"17372885811", "phone_number_id"=>"320958887765426"}, "contacts"=>[{"profile"=>{"name"=>"Keith Schacht"}, "wa_id"=>"17737898255"}], "messages"=>[{"from"=>"17737898255", "id"=>"wamid.HBgLMTc3Mzc4OTgyNTUVAgASGBQzQUFBNTU5NEVCRTFBNzlCRkY4RAA=", "timestamp"=>"1715986308", "text"=>{"body"=>"Yes"}, "type"=>"text"}]}, "field"=>"messages"}]}]}}
2 changes: 2 additions & 0 deletions app/helpers/whatsapp_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module WhatsappHelper
end
53 changes: 53 additions & 0 deletions app/services/whatsapp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Whatsapp
# Followed this guide: https://medium.com/@rishicentury/how-to-use-whatsapp-cloud-api-6c4b4a22fc34

def self.send(str, number: "17737898255")
raise "Need to specify what you want to send" if str.blank?

response = if str.is_a?(Symbol)
send_template(str, number)
else
send_text(str, number)
end

raise "Unexpected response: #{response.status} - #{response.body}" if response.status != 200
end

def self.send_text(text, number: "17737898255")
raise "Need to specify text to send" if text.blank?

response = Faraday.post("https://graph.facebook.com/v19.0/320958887765426/messages") do |req|
req.headers["Authorization"] = "Bearer #{Setting.whatsapp_key}"
req.headers["Content-Type"] = "application/json"
req.body = JSON.generate({
messaging_product: "whatsapp",
recipient_type: "individual",
to: number,
type: "text",
text: {
body: text
}
})
end
end

def self.send_template(name = :hello_world, number: "17737898255")
raise "Need to specify a template name" if name.blank?

response = Faraday.post("https://graph.facebook.com/v19.0/320958887765426/messages") do |req|
req.headers["Authorization"] = "Bearer #{Setting.whatsapp_key}"
req.headers["Content-Type"] = "application/json"
req.body = JSON.generate({
messaging_product: "whatsapp",
to: number,
type: "template",
template: {
name: name,
language: {
code: "en_US"
}
}
})
end
end
end
1 change: 1 addition & 0 deletions config/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ shared:
http_header_auth_email: <%= ENV["HTTP_HEADER_AUTH_EMAIL"] || "X-WEBAUTH-EMAIL" %>
http_header_auth_name: <%= ENV["HTTP_HEADER_AUTH_NAME"] || "X-WEBAUTH-NAME" %>
http_header_auth_uid: <%= ENV["HTTP_HEADER_AUTH_UID"] || "X-WEBAUTH-USER" %>
whatsapp_key: <%= ENV["WHATSAPP_KEY"] || Rails.application.credentials[:whatsapp_key] %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
end
end

post "whatsapp/create" => "whatsapp#create"

get "/login", to: "authentications#new"
post "/login", to: "authentications#create"
get "/register", to: "users#new"
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/whatsapp_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "test_helper"

class WhatsappControllerTest < ActionDispatch::IntegrationTest
test "should get create" do
get whatsapp_create_url
assert_response :success
end
end