第三方服务集成

时间:2014-08-15 14:10:51

标签: ruby-on-rails heroku integration mandrill

我正在使用我的应用中的电子邮件功能回复,而我正在使用mandrillapp.com。它现在如何运作:

1. User replies from their email to something@something.com
2. Mandril receives the mail and sends the POST request to preconfigured endpoint of my service
3. I process that post request (create internal app message)

关于第3步,我基本上有代码应该创建包含在begin/rescue中的内部应用消息。因此,任何潜在的错误都会报告给我,这是他们过去的错误。

然而,现在我遇到了一个声称已发送电子邮件的用户(步骤1),我联系了mandrill支持,他们说他们已经处理过并发送到我的终端(步骤2)。

因此,只让我看到在特定时间范围内步骤3中发生的事情。我的应用程序在heroku上,我的日志存储在AWS上我可以回到过去那个特定时间,我可以看到来自mandrill端的成功POST请求。

我能做些什么才能抓住这个案例向前发展?当然要修理它,因为我不知道现在发生了什么。

我正在考虑创建一个名为Mandrill hooks的模型,它将具有params列,这将是一个序列化哈希,它将保存从mandrill到我的端点的所有参数。

然后我可以查看我的帐户收到的webhook数量和mandrill发送的数量,希望我发现不同数量的数据,因为我将在数据库中有params数据,我将能够重现情况下。

还是有更明智的方法,因为这对我来说看起来很天真?

1 个答案:

答案 0 :(得分:1)

我认为可能有一种更明智的方法可以让你对所有事情有更多的控制权,这听起来与我最近完成的事情和I answered a similar question here非常相似。这有什么帮助,还是有一些你想要的更具体的帮助?

** 编辑:链接的帖子/代码

我正在运行我的Mandrill Webhook处理作为VS中的API项目,这就是我的运行方式:

    [HttpPost]
    public string Post()
    {
        /* Every Mandrill webhook uses the same general data format, regardless of the event type. 
         * The webhook request is a standard POST request with a single parameter (currently) - 'mandrill_events'. */

        string validJson = HttpContext.Current.Request.Form["mandrill_events"].Replace("mandrill_events=", ""); //"mandrill_events=" is not valid JSON. If you take that out you should be able to parse it. //http://stackoverflow.com/questions/24521326/deserializing-mandrillapp-webhook-response
        List<MandrillEvent> mandrillEventList = JsonConvert.DeserializeObject<List<MandrillEvent>>(validJson);

        foreach (MandrillEvent mandrillEvent in mandrillEventList)
        {
            if (mandrillEvent.msg.email != null)
            {
                DataLayer.ReportingData.EmailSave(mandrillEvent); //Saves MandrillEvent email to database and sets a messageId for datalayer
            }
        } 

        foreach (MandrillEvent mandrillEvent in mandrillEventList)
        {
            DataLayer.ReportingData.MandrillEventSave(mandrillEvent); //Saves MandrillEvent object to database
        }

        return "DONE";
    }

然后我接受了#34; mandrill_event&#34;的记录(和未记录的)JSON参数。并使用json2csharp.com生成C#属性。我创建了一个名为&#34; MandrillEvent.cs&#34;把它们放在:

public class SmtpEvent
    {
        public int ts { get; set; }
        public DateTime SmtpTs { get; set; }
        public string type { get; set; }
        public string diag { get; set; }
        public string source_ip { get; set; }
        public string destination_ip { get; set; }
        public int size { get; set; }
        public int smtpId { get; set; } //added for datalayer
    }

    public class Msg
    {
        public int ts { get; set; }
        public DateTime MsgTs { get; set; }
        public string _id { get; set; }
        public string state { get; set; }
        public string subject { get; set; }
        public string email { get; set; }
        public List<object> tags { get; set; }
        public List<object> opens { get; set; } //an array of containing an item for each time the message was opened. Each open includes the following keys: "ts", "ip", "location", "ua"
        public List<object> clicks { get; set; } //an array containing an item for each click recorded for the message. Each item contains the following: "ts", "url"
        public List<SmtpEvent> smtp_events { get; set; }
        public List<object> resends { get; set; } //not currently documented on http://help.mandrill.com/entries/58303976-Message-Event-Webhook-format
        public string _version { get; set; }
        public string diag { get; set; } //for bounced and soft-bounced messages, provides the specific SMTP response code and bounce description, if any, received from the remote server
        public int bgtools_code { get; set; } //Is it this? for bounced and soft-bounced messages, a short description of the bounce reason such as bad_mailbox or invalid_domain. (not currently documented but in JSON response)
        public string sender { get; set; }
        public object template { get; set; }
        public string bounce_description { get; set; }

        public Msg()
        {
            tags = new List<object>();
            opens = new List<object>();
            clicks = new List<object>();
            smtp_events = new List<SmtpEvent>();
            smtp_events.Add(new SmtpEvent());
            resends = new List<object>();
        }
    }

    public class MandrillEvent
    {
        public string @event { get; set; }
        public string _id { get; set; }
        public Msg msg { get; set; }
        public int ts { get; set; }
        public DateTime MandrillEventTs { get; set; }
        public int messageId { get; set; } //added for datalayer
        public List<string> SingleMandrillEventData { get; set; } //added for Reporting

        public MandrillEvent()
        {
            SingleMandrillEventData = new List<string>();
            msg = new Msg();
        }
    }

你现在有了#34; mandrill_events&#34; JSON对象作为一个正常运行的C#对象!!这有帮助还是需要更多的澄清/帮助?