使用Java获取twilio中whatsapp消息的编号和正文

时间:2018-11-15 13:10:56

标签: java spring spring-mvc twilio

我有一个要解决的问题。我有来自WhatsApp的消息,我正在尝试使用Twilio SDK for Java进行回复。

这是我的代码:

package com.boilerplate.controllers;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;

@Controller
@Repository
@Transactional
@RequestMapping("/boilerplate")
public class BoilerplateController {
    public static final String ACCOUNT_SID = "";
    public static final String AUTH_TOKEN = "";
    @Autowired 
    private SessionFactory sessionFactory;

    @ResponseBody
    @RequestMapping(value = { "/wizard" }, method = RequestMethod.POST)
    public String wizardSave(@RequestParam String from,@RequestParam String body) { 

//      String q1_field = "0";
//      String q2_field = "0";
//      String q3_field = "0";
//      String q4_field = "0";
    String undone = "UNDONE";
//      String status = "";

        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Message message = Message.creator(
                 new com.twilio.type.PhoneNumber(from),
                 new com.twilio.type.PhoneNumber("whatsapp:+14155238886"),
                "Hello there!")
            .create();
        return undone;


}
}

由于某种原因,此代码

@ResponseBody
    @RequestMapping(value = { "/wizard" }, method = RequestMethod.POST)
    public String wizardSave(@RequestParam String from,@RequestParam String body) { 

没有让我得到我需要的东西和身体。

我正在使用spring并且使用了@RequestParam注释

我无法从身体中得到。为什么?

注意:我唯一遇到的错误是来自Twilio,说我无法连接。

1 个答案:

答案 0 :(得分:0)

以这种方式解决

    @ResponseBody
    @RequestMapping(value = { "/wizard" }, method = RequestMethod.POST)
    public String wizardSave(@RequestParam("Body") String message, 
    @RequestParam("From") String from) {    

并发送

  Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
                    Message messagem = Message.creator(
                             new com.twilio.type.PhoneNumber(from),
                             new com.twilio.type.PhoneNumber("whatsapp:+14155238886"),
                            "Sent From one")
                        .create();
   System.out.println(messagem.getSid());