Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
Chapter 105. Twitter
Twitter 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Available as of Camel 2.10
The Twitter component enables the most useful features of the Twitter API by encapsulating Twitter4J. It allows direct, polling, or event-driven consumption of timelines, users, trends, and direct messages. Also, it supports producing messages as status updates or direct messages.
Twitter now requires the use of OAuth for all client application authentication. In order to use camel-twitter with your account, you'll need to create a new application within Twitter at https://dev.twitter.com/apps/new and grant the application access to your account. Finally, generate your access token and secret.
Maven users will need to add the following dependency to their pom.xml for this component:
URI format 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
twitter://[endpoint]?[options]
twitter://[endpoint]?[options]
TwitterComponent: 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The twitter component can be configured with the Twitter account settings which is mandatory to configure before using. You can also configure these options directly in the endpoint.
Option | Description |
---|---|
consumerKey | The consumer key |
consumerSecret | The consumer secret |
accessToken | The access token |
accessTokenSecret | The access token secret |
Consumer Endpoints: 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Rather than the endpoints returning a List through one single route exchange, camel-twitter creates one route exchange per returned object. As an example, if "timeline/home" results in five statuses, the route will be executed five times (one for each Status).
Endpoint | Context | Body Type |
---|---|---|
directmessage | direct, polling | twitter4j.DirectMessage |
search | direct, polling | twitter4j.Tweet |
streaming/filter | event, polling | twitter4j.Status |
streaming/sample | event, polling | twitter4j.Status |
timeline/home | direct, polling | twitter4j.Status |
timeline/mentions | direct, polling | twitter4j.Status |
timeline/public | direct, polling | twitter4j.Status |
timeline/retweetsofme | direct, polling | twitter4j.Status |
timeline/user | direct, polling | twitter4j.Status |
trends/daily | *Camel 2.10.1:* direct, polling | twitter4j.Status |
trends/weekly | *Camel 2.10.1:* direct, polling | twitter4j.Status |
Producer Endpoints: 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Endpoint | Body Type |
---|---|
directmessage | String |
search | List<twitter4j.Tweet> |
timeline/user | String |
URI Options 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Name | Default Value | Description |
---|---|---|
type |
direct
|
direct, event, or polling |
delay |
60
|
in seconds |
consumerKey |
null
|
Consumer Key. Can also be configured on the TwitterComponent level instead.
|
consumerSecret |
null
|
Consumer Secret. Can also be configured on the TwitterComponent level instead.
|
accessToken |
null
|
Access Token. Can also be configured on the TwitterComponent level instead.
|
accessTokenSecret |
null
|
Access Token Secret. Can also be configured on the TwitterComponent level instead.
|
user |
null
|
Username, used for user timeline consumption, direct message production, etc. |
locations |
null
|
'lat,lon;lat,lon;...' Bounding boxes, created by pairs of lat/lons. Can be used for streaming/filter |
keywords |
null
|
'foo1,foo2,foo3...' Can be used for search and streaming/filter |
userIds |
null
|
'username,username...' Can be used for streaming/filter |
filterOld |
true
|
Filter out old tweets, that has previously been polled. This state is stored in memory only, and based on last tweet id. |
Message body 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
All message bodies utilize objects provided by the Twitter4J API.
Use cases 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
from("direct:foo") .to("twitter://timeline/user?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]);
from("direct:foo")
.to("twitter://timeline/user?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]);
from("twitter://timeline/home?type=polling&delay=5&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]") .to("bean:blah");
from("twitter://timeline/home?type=polling&delay=5&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]")
.to("bean:blah");
To search for all statuses with the keyword 'camel': 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
from("twitter://search?type=direct&keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]") .to("bean:blah");
from("twitter://search?type=direct&keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]")
.to("bean:blah");
Searching using a producer with static keywords 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
from("direct:foo") .to("twitter://search?keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
from("direct:foo")
.to("twitter://search?keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
In the bar header we have the keywords we want to search, so we can assign this value to the
CamelTwitterKeywords
header.
from("direct:foo") .setHeader("CamelTwitterKeywords", header("bar")) .to("twitter://search?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
from("direct:foo")
.setHeader("CamelTwitterKeywords", header("bar"))
.to("twitter://search?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
Example 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
See also the Twitter Websocket Example.