2011-06-03 78 views
2

我正在尝试为Amazon SNS设置Webhook。 SNS将发送一个JSON对象给webhook。基于KRL文档,我可以使用event:param('name')来获取事件参数。这适用于表单编码数据,但是JSON呢?KRL webhooks接收JSON

我发出了呼吁postbin.org,这是postbin报道什么:

body { 
    "Message": "You have ...", 
    "MessageId": "958....", 
    "Signature": "vo3v5f....", 
    ... 
} 

这里是我想什么KRL写:

rule sns_webhook { 
    select when webhook sometopic Type "SubscriptionConfirmation" 
    pre { 
    topic_arn = event:param("TopicARN"); 
    signature = event:param("Signature"); 
    message = event:param("Message"); 
    subscribe_url = event:param("SubscribeURL"); 
    } 
    if valid_signature(signature) then { 
    confirm_subscription(subscribe_url); 
    } 
} 

这会为HTTP可能工作形式编码的数据,但与JSON我期望以下将需要:

rule sns_json { 
    select when webhook sometopic 
    pre { 
    body = event:param('body').decode(); 
    msg_type = body.pick("Type"); 
    signature = body.pick("Signature"); 
    ... 
    } 
    if msg_type eq "SubscriptionConfirmation" && valid(signature) then 
    { 
    confirm_subscription(...); 
    } 
} 

我是否需要使用秒这里介绍的ond方法?请问event:param('body')从SNS消息中获取JSON数据?

回答

2

你的第二个代码块非常接近。这是,重写并使用正确的事件:如果你第一次有这种规则参数()

rule sns_json { 
    select when webhook sometopic 
    pre { 
    body = event:param('request_body').decode(); 
    msg_type = body.pick("Type"); 
    signature = body.pick("Signature"); 
    ... 
    } 
    if msg_type eq "SubscriptionConfirmation" && valid(signature) then 
    { 
    confirm_subscription(...); 
    } 
} 

,我会记得在fired片尾曲块添加last

除了可以多次解码主体,还可以将已解码消息的显式事件作为事件参数引发,并包含消息的类型,以便您可以编写明确处理不同类型的规则。