【问题标题】:I want to respond to a twilio SMS with a simple text and at the same time forward the SMS to some emailId我想用简单的文本回复 twilio SMS,同时将 SMS 转发到某个 emailId
【发布时间】:2018-05-15 14:12:53
【问题描述】:

配置了一个调用以下代码的 webhook URL :::

String body = request.getParameter("Body");
    String fromNumber = request.getParameter("From");
    String message;
    Message sms = new Message.Builder().body(new Body(message)).build();
    // Create a TwiML response and add our friendly message.
    MessagingResponse twiml = new MessagingResponse.Builder().message(sms).build();
    response.setContentType("application/xml");
    try {
        response.getWriter().print(twiml.toXml());
        return twiml.toXml();
        final TwilioEmailContextDTO twilioEmailContextDTO = new TwilioEmailContextDTO();
        twilioEmailContextDTO.setBody(body);
        twilioEmailContextDTO.setFromNumber(fromNumber);
        forwardTwilioSMSToMail(twilioEmailContextDTO);
    } 

Q1:String fromNumber = request.getParameter("From"); 是否给我发件人号码。

Q2:我也收到The constructor Body(String) is undefined" 编译错误。

Q3:我使用传统的 Hybris 方式将 SMS 作为邮件转发(使用 emailService),我们有 twilio 方式吗?

用代码更新

{ 请看一下我使用的方法。使用 Spring,注解。

@RequestMapping(value = "/twilioReply", method = RequestMethod.POST)  
@ResponseBody  
public void TwilioReplies(HttpServletRequest request, HttpServletResponse response) throws IOException {  
    String body = request.getParameter("Body");   
    String fromNumber = request.getParameter("From");  
    String messageBody = this.configurationService.getConfiguration().getString(TWILIO_REPLY);  
    Body smsBody = new Body.Builder(messageBody).build();  
    Message message = new Message.Builder().body(smsBody).build();  
    // Create a TwiML response and add our message.  
    MessagingResponse twiml = new MessagingResponse.Builder().message(message).build();  
    response.setContentType("application/xml");  
    try {  
        response.getWriter().print(twiml.toXml());  
    } catch (TwiMLException e) {  
        LOG.error("Exception Occured while twiml Email :",e);  
    }
} 

【问题讨论】:

    标签: twilio twilio-api twilio-twiml twilio-functions


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    A1:From 参数是 Twilio 为发送消息的号码发送的参数。所以是的,request.getParameter("From") 应该是那个数字。

    A2:Body 也已构建,如this example in the documentation

    Body body = new Body.Builder("Store Location: 123 Easy St.").build();
    Message message = new Message.Builder().body(body).build();
    

    A3:Twilio 对您如何在自己的服务器上发送电子邮件没有意见。如果这对你有用,那就去吧!

    【讨论】:

    • 非常感谢 philnash。你的回答解决了我的问题。此外,我正在尝试通过调用我在 twilio 回复部分中提供的 webhook URL 来调试我的代码。我正在考虑使用像邮递员这样的 Rest 客户端工具来点击 url,您能否提供一个示例 XML,它将作为上述代码中的请求提供。
    • 很高兴有帮助,您能否将答案标记为正确,以便其他人可以看到它有帮助,好吗?此外,XML 应该有点像:<Response><Message>Store Location: 123 East St.</Message></Response>.
    • 非常感谢@philnash,还有一个查询(很抱歉在 cmets 部分下的泛滥问题);我们在电话号码下有账户 SID (PN******************) 和账户 SID(ACf******************)在控制台中,我们在发送 SMS 时应该使用哪个。 --->另外,您提供的xml看起来像我们收到回复后发送的响应(twiml),但我想看看请求xml的样子(当回复进来时);我们用来从问题中获取上述代码 sn-p 中的“body”和“from”的那个。
    • 以“PN”开头的SID就是电话号码SID,所以要使用的就是以“AC”开头的Account SID。来自 Twilio 的传入请求不是 XML,而是 application/x-www-form-urlencoded。参数都列在这里:twilio.com/docs/sms/twiml.
    • 嗨 philnash,在我的代码中处理回复时,我应该使用它来发送 twiml 1.) response.getWriter().print(twiml.toXml());或 2.) 返回 twiml.toXml();
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多