如何在java中处理twilio SMS状态回调

时间:2017-01-18 09:55:39

标签: java spring twilio

我有以下服务方法进行短信回调处理

@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseBody
public void processSms(@RequestParam(value = "MessageStatus", required = false) String messageStatus,
                       @RequestParam(value = "ApiVersion", required = false) String apiVersion,
                       @RequestParam(value = "SmsSid", required = false) String smsSid,
                       @RequestParam(value = "SmsStatus", required = false) String smsStatus,
                       @RequestParam(value = "To", required = false) String to,
                       @RequestParam(value = "From", required = false) String from,
                       @RequestParam(value = "MessageSid", required = false) String messageSid,
                       @RequestParam(value = "AccountSid", required = false) String accountSid){

    TwilioCallBackResponse response = new TwilioCallBackResponse();
    response.messageStatus = messageStatus;
    response.apiVersion = apiVersion;
    response.smsSid = smsSid;
    response.smsStatus = smsStatus;
    response.to = to;
    response.from = from;
    response.messageSid = messageSid;
    response.accountSid = accountSid;

    LOG.info("Incomming twilio callback: " + response.messageStatus);

    smsService.processSmsCallback(response);
}

我可以成功地从twilio获取并记录响应。 问题是在twilio结束时会记录响应错误。我应该指定内容类型还是回复一些响应主体?有什么想法吗?

这是来自twilio log

enter image description here

和错误11200也会记录HTTP检索失败

2 个答案:

答案 0 :(得分:4)

状态回调不控制应用程序流,因此在此实例中不需要返回TwiML;但是,建议您respond to status callbacks使用204 No Content或200 OK with Content-Type:text / xml并在正文中使用空<Response/>。无法正确响应将导致调试器中出现警告。

答案 1 :(得分:2)

好的,根据吉姆的回答,这对我有用,而且在twilio方面没有更多的警告

    @RequestMapping(value = "/", method = RequestMethod.POST, produces = "text/xml")
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
public String processSms(@RequestParam(value = "MessageStatus", required = false) String messageStatus,
                       @RequestParam(value = "ApiVersion", required = false) String apiVersion,
                       @RequestParam(value = "SmsSid", required = false) String smsSid,
                       @RequestParam(value = "SmsStatus", required = false) String smsStatus,
                       @RequestParam(value = "To", required = false) String to,
                       @RequestParam(value = "From", required = false) String from,
                       @RequestParam(value = "MessageSid", required = false) String messageSid,
                       @RequestParam(value = "AccountSid", required = false) String accountSid){

    TwilioCallBackResponse response = new TwilioCallBackResponse();
    response.messageStatus = messageStatus;
    response.apiVersion = apiVersion;
    response.smsSid = smsSid;
    response.smsStatus = smsStatus;
    response.to = to;
    response.from = from;
    response.messageSid = messageSid;
    response.accountSid = accountSid;

    LOG.info("Incomming twilio callback: " + JsonUtils.printJson(response));

    smsService.processSmsCallback(response);

    return "<Response/>";
}