Apache Camel:使用Twilio发送短信

问题描述 投票:1回答:1

我正在尝试使用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();
}
java apache-camel twilio
1个回答
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

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