[英]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.