简体   繁体   中英

Apache Camel: Send Text Message with Twilio

I'm trying to send a text message over Apache Camel using the camel-twilio component . Since I never used the Twilio API (neither natively nor using Apache Camel), I'm not sure if I got the parameters right. 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:

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:

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:

<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:

/**
 * 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 . In your case, let's get familiar with the MessageCreator API here:
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:

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.)

Once the username/password are set up, let's use the 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. (NOTE: Any of the camel-twilio endpoint options can be provided in a message header with CamelTwilio prefix.)

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 .

Now you should be able to send texts to Twilio.

FYI, there is a working example of camel-twilio with Spring Boot:
https://github.com/tadayosi/demo-camel-hawtio-springboot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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