Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.
Open
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
91 changes: 91 additions & 0 deletions app/controllers/api/v1/webhooks/meya_controller.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down