diff --git a/app/controllers/api/v1/webhooks/meya_controller.rb b/app/controllers/api/v1/webhooks/meya_controller.rb new file mode 100644 index 00000000..70ec4ca0 --- /dev/null +++ b/app/controllers/api/v1/webhooks/meya_controller.rb @@ -0,0 +1,91 @@ +class Api::V1::Webhooks::MeyaController < Api::V1::ApplicationController + before_action :set_bot + # before_action :set_integration + # before_action :authorize + # before_action :throttle + + # POST api.domain.tld/v1/b/1/webhooks/meya + # @raise [UnfullfiledRequestError] + def create + @message = Api::Message.new(message_params).tap(&:sanitize!) + @conversation = Api::Conversation.new(conversation_params) + @creator = Api::Creator.new(creator_params) + + if @message.valid? && @conversation.valid? && @creator.valid? + CreateRequestJob.perform_async(@bot.id, { request_id: request.request_id, payload: meya_params.to_json, fulfilled: true }) + TrackMessageJob.perform_async(@message.attributes, @conversation.attributes, @creator.attributes, @bot.id) + else + p @message.errors + p @conversation.errors + p @creator.errors + CreateRequestJob.perform_async(@bot.id, { request_id: request.request_id, payload: meya_params.to_json }) + + error = UnfullfiledRequestError.new + logger.error(error) + Appsignal.add_exception(error) + end + + head :created + end + + + private + + # @return [Hash] + def message_params + { + distinct_id: SecureRandom.uuid, + platform: 'web', + provider: 'meya', + mtype: meya_params[:type], + sent_at: meya_params[:timestamp], + properties: { + text: meya_params[:text] + } + } + end + + # @return [Hash] + def conversation_params + { + distinct_id: meya_params[:user_id] + meya_params[:bot_id] + } + end + + # @return [Hash] + def creator_params + if meya_params[:sender] == 'bot' + { + type: 'Bot', + distinct_id: meya_params[:bot_id] + } + elsif meya_params[:sender] == 'user' + { + type: 'Interlocutor', + distinct_id: meya_params[:user_id] + } + else + Hash.new + end + end + + def meya_params + params.require(:meya).permit(:user_id, :sender, :timestamp, :text, :type, :bot_id) + end + + # @raise [ActiveRecord::RecordNotFound] + def set_bot + @bot = Bot.friendly.find(params[:bot_id]) + end +end + +# curl -H "Content-Type: application/json" -X POST -d '{"user_id":"1234567890","text":"hi"}' -u zfH44a7brkHMDbbgOA4QKCzPYuy51yzq: https://meya.ai/webhook/receive/BdZj2saIUCs + +# {"user_id": "1234567890", "sender": "bot", "timestamp": 1484839610.680796, "text": "Howdy! \ud83d\ude04", "type": "text", "bot_id": "BdZj2saIUCs"} + +# {"user_id": "1234567890", "sender": "user", "timestamp": 1484839610.286196, "text": "hi", "type": "text", "bot_id": "BdZj2saIUCs"} + +# https://6511ea7a.ngrok.io/v1/b/06134b33/webhooks/meya + + +# X-Meya-Signature diff --git a/config/routes.rb b/config/routes.rb index 3ec2482e..c0cef692 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,7 @@ namespace :webhooks do post :smooch, controller: :smooch, action: :create, as: :smooch + post :meya, controller: :meya, action: :create, as: :meya end end