[英]Apache Camel: Send Text Message with Twilio
我正在嘗試使用camel-twilio
組件通過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();
}
我必須說,要有效地使用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)
,但因為to
和from
必須PhoneNumber
情況下,很容易將它們傳遞到端點駱駝郵件頭而不是將它們嵌入到端點的URI作為端點參數。 (注意:可以在帶有CamelTwilio
前綴的消息頭中提供任何camel-twilio端點選項。)
看起來類似於以下內容:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.