diff --git a/app/controllers/whatsapp_controller.rb b/app/controllers/whatsapp_controller.rb new file mode 100644 index 000000000..cf45d31d4 --- /dev/null +++ b/app/controllers/whatsapp_controller.rb @@ -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"}]}]}} diff --git a/app/helpers/whatsapp_helper.rb b/app/helpers/whatsapp_helper.rb new file mode 100644 index 000000000..eb6926926 --- /dev/null +++ b/app/helpers/whatsapp_helper.rb @@ -0,0 +1,2 @@ +module WhatsappHelper +end diff --git a/app/services/whatsapp.rb b/app/services/whatsapp.rb new file mode 100644 index 000000000..2ac1dda94 --- /dev/null +++ b/app/services/whatsapp.rb @@ -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 diff --git a/config/options.yml b/config/options.yml index 174d80b57..5c94768d6 100644 --- a/config/options.yml +++ b/config/options.yml @@ -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] %> diff --git a/config/routes.rb b/config/routes.rb index b5feefe22..71c9dc6c8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/test/controllers/whatsapp_controller_test.rb b/test/controllers/whatsapp_controller_test.rb new file mode 100644 index 000000000..83f91a1d8 --- /dev/null +++ b/test/controllers/whatsapp_controller_test.rb @@ -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