evendis / mandrill-rails

Webhook processing and event decoration to make using Mandrill with Rails just that much easier
MIT License
288 stars 36 forks source link

handle_inbound not working #13

Closed pawel2105 closed 10 years ago

pawel2105 commented 10 years ago

As per the documentation I've created an inbox controller:

class InboxController < ApplicationController
  include Mandrill::Rails::WebHookProcessor

  def handle_inbound(event_payload)
    Rails.logger.info event_payload['msg']
    Rails.logger.info "-------------"
    Rails.logger.info event_payload.user_email
    Rails.logger.info "-------------"
    Rails.logger.info event_payload.recipients
    Rails.logger.info "-------------"
    Rails.logger.info event_payload.recipient_emails
  end
end

I've copied the routes for the inbox verbatim from the readme as well. When I send a test email from the mandrill app web interface to an email address I've set up with a hook sending a POST request to mydomain.com/inbox I can see a request hit:

 Processing by InboxController#create as */*

The params include: Parameters: {"mandrill_events" etc.

However I cannot see any of my logging info.

Additionally I've tried to define a create method in the same controller and have the following in the definition:

Mandrill::WebHook::EventDecorator[params[:mandrill_events]]

But then I get ArgumentError: odd number of arguments for Hash

tardate commented 10 years ago

Hi @pawel2105 , a couple of things:

pawel2105 commented 10 years ago

@tardate after a bit of playing around I managed to get it working from:

JSON.parse(params['mandrill_events']).each do |raw_event|
  event = Mandrill::WebHook::EventDecorator[raw_event]
  ...
end
jfrux commented 9 years ago

Why did he have to do:

JSON.parse(params['mandrill_events']).each do |raw_event|
  event = Mandrill::WebHook::EventDecorator[raw_event]
  ...
end

Mine is doing the same thing...? Shouldn't it be calling handle_ like it says in the documentation and comments?

tardate commented 9 years ago

@joshuairl Good question! No, should not need to do that. Just define the required handle_* methods and it "should just work". If you are seeing different, then appreciate it if you could post details..

jfrux commented 9 years ago
gem 'rails', '4.1.8'
gem 'mandrill-rails'

routes.rb

Rails.application.routes.draw do
  resource :webhook, :controller => 'webhook', :only => [:show,:create]
  #...
end

Simple webhook controller

class WebhookController < ActionController::Base
  include Mandrill::Rails::WebHookProcessor

  def handle_click(event_payload)
    puts "EMAIL LOG: CLICK EVENT"
  end

  def handle_spam(event_payload)
    puts "EMAIL LOG: SPAM EVENT"
  end

  def handle_send(event_payload)
    puts "EMAIL LOG: SEND EVENT"
  end

  def handle_deferral(event_payload)
    puts "EMAIL LOG: DEFERRAL EVENT"
  end

  def handle_hard_bounce(event_payload)
    puts "EMAIL LOG: HARD BOUNCE EVENT"
  end

  def handle_reject(event_payload)
    puts "EMAIL LOG: REJECT EVENT"
  end

  def handle_unsub(event_payload)
    puts "EMAIL LOG: UNSUB EVENT"
  end

  def handle_open(event_payload)
    puts "EMAIL LOG: OPEN EVENT"
  end

  def handle_soft_bounce(event_payload)
    puts "EMAIL LOG: SOFT BOUNCE EVENT"
  end

  def handle_storage(event_payload)
    puts event_payload.to_yaml
    #...
  end
end

Sending this with curl:

curl -v -A 'Mandrill-Webhook/1.0' -H 'X-Mandrill-Signature: [key]=' -X POST -d 'mandrill_events=[{"event":"hard_bounce","_id":"1dbc399ff23847f1a7bd5bcc047b3089","msg":{"ts":142780085,"_id":"1dbc399ff23847f1a7bd5bcc047b3089","state":"bounced","subject":"[subject]","email":"example@example.org","tags":["tag"],"smtp_events":[{"ts":1427800855,"type":"sent","diag":"250 2.0.0 OK 1427800855 s44si7119005yho.99 - gsmtp","source_ip":"198.2.128.3","destination_ip":"64.233.177.26","size":19486}],"resends":[],"_version":"n7ryzhjgtNQ7iu-681TS9g","diag":"X-Postfix; unknown user: \"[removed_email]","bgtools_code":10,"metadata":{"person":314911,"activity":17351,"attendee":410289},"sender":"[fromemail]","template":null,"bounce_description":"bad_mailbox"},"ts":1427801665}]' 'http://localhost:4000/webhook'

logs just showing:

Processing by WebhookController#create as */*
  Parameters: {"mandrill_events"=>"[{\"event\":\"hard_bounce\",\"_id\":\"1dbc399ff23847f1a7bd5bcc047b3089\",\"msg\":{\"ts\":1427800854,\"_id\":\"1dbc399ff23847f1a7bd5bcc047b3089\",\"state\":\"bounced\",\"subject\":\"[subject]\",\"email\":\"example@example.org\",\"tags\":[\"tag\"],\"smtp_events\":[{\"ts\":1427800855,\"type\":\"sent\",\"diag\":\"250 2.0.0 OK 1427800855 s44si7119005yho.99 - gsmtp\",\"source_ip\":\"198.2.128.3\",\"destination_ip\":\"64.233.177.26\",\"size\":19486}],\"resends\":[],\"_version\":\"n7ryzhjgtNQ7iu-681TS9g\",\"diag\":\"X-Postfix", "unknown user: \\\"example\\\"\\n\\n--333F7821C6.1427800810\\/example.org\",\"bgtools_code\":10,\"metadata\":{\"person\":314911,\"activity\":17351,\"attendee\":410289},\"sender\":\"[fromemail]\",\"template\":null,\"bounce_description\":\"bad_mailbox\"},\"ts\":1427801665}"=>"[FILTERED]"}
jfrux commented 9 years ago

sorry for my curl and log messages not making much sense but they contained data that i didn't want google indexing... haha but the entry was taken from mandrill system.

pchowdhry commented 9 years ago

I'm seeing this same issue, is there any resolution?

tardate commented 9 years ago

@pchowdhry Hi, I've yet to find any way to reproduce the issue reported here. I just created a bare bones demo app to test again: https://github.com/evendis/mandrill-rails-demo

@joshuairl I took your example and tried to reproduce the error in the demo app. But it just worked :confounded: Perhaps you can take a look at the demo app and see if it fails for you .. or can you find a way make it fail by changing it to match your setup exactly?

pchowdhry commented 9 years ago

Thanks @tardate I'll dig a little deeper. I think it may have something to do with the json parsing libraries, as it looks like it isn't getting the inbound event at all.

tardate commented 9 years ago

I'll appreciate that greatly, thanks @pchowdhry

tardate commented 9 years ago

I read thru the code again and the two main possibilities I can see are: