使用java在twilio中获取whatsapp消息的号码和正文

问题描述 投票:0回答:2

我有一个问题正在努力解决。我收到来自 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 说我无法连接。

java spring spring-mvc twilio
2个回答
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());

0
投票
   @PostMapping(value="/wizard",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String wizardSave(@RequestBody MultiValueMap<String, String> data) {
    String body= data.getFirst("Body");

    System.out.prinln("Body :"+body);

    return body;
}

希望这个解决方案有用。

© www.soinside.com 2019 - 2024. All rights reserved.