Java ApacheCamel:使用Twilio发送文本消息

Java ApacheCamel:使用Twilio发送文本消息,java,apache-camel,twilio,Java,Apache Camel,Twilio,我正在尝试使用通过Apache Camel发送文本消息。因为我从未使用过TwilioAPI(既不是本机使用的,也不是使用ApacheCamel),所以我不确定参数是否正确。以下是我写的方法: /** * Sends a text message to the given recipient's number (parameter to) * * @param username: * Twilio username (email) * @param passwor

我正在尝试使用通过Apache Camel发送文本消息。因为我从未使用过TwilioAPI(既不是本机使用的,也不是使用ApacheCamel),所以我不确定参数是否正确。以下是我写的方法:

/**
 * Sends a text message to the given recipient's number (parameter to)
 * 
 * @param username:
 *            Twilio username (email)
 * @param password:
 *            Twilio password (in plain text)
 * @param accountSid:
 *            Twilio account sid (from the dashboard)
 * @param from:
 *            registered phone number (starting with country prefix +XX)
 * @param to:
 *            the recipient's phone number (starting with country prefix +XX)
 * @param message:
 *            the message to be sent (plain text)
 * @throws Exception
 */
public static void sendTextMessage(String username, String password, String accountSid, String from, String to,
        String message) throws Exception {
    String route = String.format("twilio:message/creator?username=%s&password=%s&accountSid=%s&from=%s&to=%s",
            username, password, accountSid, from, to);
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:message").to(route);
        }
    });
    context.start();
    ProducerTemplate producer = context.createProducerTemplate();
    producer.sendBody("direct:message", message);
    context.stop();
}
最重要的一行是创建路由,这是方法的第一行。当我根据JavaDoc使用参数运行此方法时,我得到以下错误:

Caused by: org.apache.camel.RuntimeCamelException: Missing properties for creator, need one or more from [pathAccountSid, mediaUrl, messagingServiceSid, body]
因此,我想添加参数
messagingServiceSid
,再次提供我的
accountSid

String route = String.format("twilio:message/creator?username=%s&password=%s&accountSid=%s&from=%s&to=%s&messagingServiceSid=%s",
            username, password, accountSid, from, to, accountSid);
现在,我收到以下错误消息:

Caused by: java.lang.IllegalArgumentException: No matching method for message/creator, with arguments [messagingServiceSid, from, to]
我做错了什么

编辑:这些是我的Maven依赖项:

<dependencies>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.20.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-twilio</artifactId>
        <version>2.20.1</version>
    </dependency>
</dependencies>

我必须说,要有效地与camel twilio合作,您需要对以下方面有很好的了解。在您的情况下,让我们熟悉这里的
MessageCreator
API:

话虽如此,首先,由于
用户名
(即
帐户SID
)和
密码
应该在camel twilio组件中共享,因此让我们在组件中设置它们:

TwilioComponent twilio = context.getComponent("twilio", TwilioComponent.class);
twilio.getConfiguration().setUsername(username);
twilio.getConfiguration().setPassword(password);
(请注意,大多数情况下,twilio用户名和accoundSid指的是同一个东西,因此您只能使用其中一个。)

设置好用户名/密码后,让我们使用
MessageCreator
。您可以使用的最简单的构造函数是
MessageCreator(PhoneNumber to,PhoneNumber from,String body)
,但是由于
to
from
必须是
PhoneNumber
实例,将它们作为驼峰消息头传递给端点比将它们作为端点参数嵌入端点URI更容易。(注意:任何CamelTwilio端点选项都可以在带有
CamelTwilio
前缀的消息头中提供。)

这将类似于以下内容:

    public void configure() throws Exception {
        from("direct:message")
            .setHeader("CamelTwilioTo", constant(new PhoneNumber(to)))
            .setHeader("CamelTwilioFrom", constant(new PhoneNumber(from)))
            .setHeader("CamelTwilioBody", constant(message))
            .to("twilio://message/creator");
    }
注意,此时,端点URI可以像
twilio://message/creator

现在你应该可以给Twilio发短信了

仅供参考,这里有一个使用弹簧靴的驼色斜纹布的工作示例:

当我使用帐户sid而不是用户名,使用身份验证令牌而不是密码时,它会起作用!非常感谢你!
    public void configure() throws Exception {
        from("direct:message")
            .setHeader("CamelTwilioTo", constant(new PhoneNumber(to)))
            .setHeader("CamelTwilioFrom", constant(new PhoneNumber(from)))
            .setHeader("CamelTwilioBody", constant(message))
            .to("twilio://message/creator");
    }