简体   繁体   English

Apache Camel:使用Twilio发送文本消息

[英]Apache Camel: Send Text Message with Twilio

I'm trying to send a text message over Apache Camel using the camel-twilio component . 我正在尝试使用camel-twilio组件通过Apache Camel发送文本消息。 Since I never used the Twilio API (neither natively nor using Apache Camel), I'm not sure if I got the parameters right. 由于我从未使用过Twilio API(既未使用本机也未使用过Apache Camel),因此我不确定参数是否正确。 Here's the method I wrote: 这是我写的方法:

/**
 * 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();
}

The most important line is the creation of the route, the first of the method. 最重要的一行是方法的创建,即路线的创建。 When I'm running this method with parameters according to the JavaDoc, I get this error: 当我根据JavaDoc使用参数运行此方法时,出现以下错误:

Caused by: org.apache.camel.RuntimeCamelException: Missing properties for creator, need one or more from [pathAccountSid, mediaUrl, messagingServiceSid, body]

So I thought to add the parameter messagingServiceSid , providing my accountSid again: 所以我想添加参数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);

Now I get this error message: 现在,我收到此错误消息:

Caused by: java.lang.IllegalArgumentException: No matching method for message/creator, with arguments [messagingServiceSid, from, to]

What am I doing wrong? 我究竟做错了什么?

EDIT: These are my Maven dependencies: 编辑:这些是我的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>

EDIT 2: Here's the modified and working version of the method: 编辑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();
}

I have to say that to work efficiently with camel-twilio, you need to have a good understanding on Twilio Java API . 我必须说,要有效地使用camel-twilio,您需要对Twilio Java API有很好的了解。 In your case, let's get familiar with the MessageCreator API here: 在您的情况下,让我们在这里熟悉MessageCreator API:
https://www.twilio.com/docs/libraries/reference/twilio-java/7.17.0/com/twilio/rest/api/v2010/account/MessageCreator.html https://www.twilio.com/docs/libraries/reference/twilio-java/7.17.0/com/twilio/rest/api/v2010/account/MessageCreator.html

With that said, first of all, since the username (ie accountSid ) and password should be something shared in a camel-twilio component, let's set them at the component: 话虽如此,首先,由于username (即accountSid )和password应该在camel-twilio组件中共享,因此让我们在该组件上进行设置:

TwilioComponent twilio = context.getComponent("twilio", TwilioComponent.class);
twilio.getConfiguration().setUsername(username);
twilio.getConfiguration().setPassword(password);

(Note most of the time the twilio username and accoundSid refer to the same thing, so you can use only one of them.) (请注意,大多数时候twilio用户名和accoundSid指的是同一件事,因此您只能使用其中之一。)

Once the username/password are set up, let's use the MessageCreator . 设置用户名/密码后,让我们使用MessageCreator The simplest constructor you can use would be MessageCreator(PhoneNumber to, PhoneNumber from, String body) , but since to and from have to be PhoneNumber instances, it's easier to pass them to the endpoint as Camel message headers instead of embedding them to the endpoint URI as endpoint parameters. 你可以用最简单的构造是MessageCreator(PhoneNumber to, PhoneNumber from, String body) ,但因为tofrom必须PhoneNumber情况下,很容易将它们传递到端点骆驼邮件头而不是将它们嵌入到端点的URI作为端点参数。 (NOTE: Any of the camel-twilio endpoint options can be provided in a message header with CamelTwilio prefix.) (注意:可以在带有CamelTwilio前缀的消息头中提供任何camel-twilio端点选项。)

This would look something like the following: 看起来类似于以下内容:

    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");
    }

Note, at this moment, the endpoint URI can be as simple as twilio://message/creator . 注意,此时,端点URI可以像twilio://message/creator一样简单。

Now you should be able to send texts to Twilio. 现在您应该可以将文本发送到Twilio。

FYI, there is a working example of camel-twilio with Spring Boot: 仅供参考,在Spring Boot中有一个camel-twilio的有效示例:
https://github.com/tadayosi/demo-camel-hawtio-springboot https://github.com/tadayosi/demo-camel-hawtio-springboot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM