Apache Camel:使用Twilio发送短信

时间:2017-12-20 08:20:26

标签: java apache-camel twilio

我尝试使用camel-twilio component通过Apache Camel发送短信。由于我从未使用Twilio API(既不是原生也不是使用Apache Camel),我不确定我是否正确使用了参数。这是我写的方法:

/**
 * 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>

编辑2:这是该方法的修改版和工作版:

/**
 * Sends a text message to the given recipient's number (parameter to)
 * 
 * @param accountSid:
 *            Twilio account sid (from the dashboard)
 * @param authToken:
 *            Twilio auth token (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 accountSid, String authToken, String from, String to, String message)
        throws Exception {
    CamelContext context = new DefaultCamelContext();
    TwilioComponent twilio = context.getComponent("twilio", TwilioComponent.class);
    twilio.getConfiguration().setUsername(accountSid);
    twilio.getConfiguration().setPassword(authToken);
    context.addRoutes(new RouteBuilder() {
        @Override
        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");

        }
    });
    context.start();
    ProducerTemplate producer = context.createProducerTemplate();
    producer.sendBody("direct:message", message);
    context.stop();
}

1 个答案:

答案 0 :(得分:3)

我必须说,为了有效地使用camel-twilio,你需要对Twilio Java API有一个很好的理解。在您的情况下,让我们在这里熟悉MessageCreator API:
https://www.twilio.com/docs/libraries/reference/twilio-java/7.17.0/com/twilio/rest/api/v2010/account/MessageCreator.html

首先,由于username(即accountSid)和password应该是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),但由于tofrom必须是PhoneNumber个实例,因此将它们传递给端点更容易Camel消息头而不是将它们作为端点参数嵌入端点URI。 (注意:任何camel-twilio端点选项都可以在带有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。

仅供参考,有一个使用Spring Boot的camel-twilio的工作示例:
https://github.com/tadayosi/demo-camel-hawtio-springboot