首页 > 解决方案 > 使用java在twilio中获取whatsapp消息的编号和正文

问题描述

我有一个我正在尝试解决的问题。我收到来自 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) { 

没有得到我需要的 from 和 body。

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

我无法得到 from 和 body。为什么?。

注意:我得到的唯一错误是 Twilio 说我无法连接。

标签: javaspringspring-mvctwilio

解决方案


这样解决了

    @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());

推荐阅读