2017-12-20 1 views
1

camel-twilio componentを使用してApache Camelでテキストメッセージを送信しようとしています。 Twilio APIを使用したことはありません(ネイティブでもApache Camelも使用していないため)、パラメータを正しく取得しているかどうかはわかりません。ここに私が書いた方法があります:Apache Camel:Twilioでテキストメッセージを送信

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

だから私は再び私accountSidを提供し、パラメータmessagingServiceSidを追加するために考えた:

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] 

私は間違っていますか?

EDIT:

<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:これらは私のMavenの依存関係です。ここだ修正及び方法の作業バージョン:

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

答えて

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がのが設定でき、ラクダ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)ですが、tofromPhoneNumberインスタンスである必要があるため、エンドポイントのパラメータとしてエンドポイント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にテキストを送信できるようになりました。私が代わりにユーザー名とパスワードの代わり認証トークンのアカウントのSIDを使用する場合
https://github.com/tadayosi/demo-camel-hawtio-springboot

+1

が、それは動作します:

FYI、春ブーツとキャメル-twilioの実施例があります!どうもありがとうございます! –

関連する問題