## Twitter Components 

*Available as of Camel version 2.10*

The Camel Twitter consists of 4 components:

* xref:twitter-directmessage-component.adoc[Twitter Direct Message]
* xref:twitter-search-component.adoc[Twitter Search]
* xref:twitter-streaming-component.adoc[Twitter Streaming]
* xref:twitter-timeline-component.adoc[Twitter Timeline]

The Twitter components enable the most useful features of the Twitter
API by encapsulating http://twitter4j.org/[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:

[source,xml]
----
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-twitter</artifactId>
    <version>${camel-version}</version>
</dependency>
----

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

[width="100%",cols="10%,10%,10%,70%",options="header",]
|=======================================================================
|Endpoint |Context |Body Type |Notice
|xref:twitter-directmessage-component.adoc[twitter-directmessage] |direct, polling |twitter4j.DirectMessage | 

|xref:twitter-search-component.adoc[twitter-search] |direct, polling |twitter4j.Status | 

|xref:twitter-streaming-component.adoc[twitter-streaming] |event, polling |twitter4j.Status | 

|xref:twitter-timeline-component.adoc[twitter-timeline] |direct, polling |twitter4j.Status | 
|=======================================================================

### Producer endpoints

[width="100%",cols="10%,10%,80%",options="header",]
|==============================
|Endpoint |Body Type |Notice
|xref:twitter-directmessage-component.adoc[twitter-directmessage] |String |
|xref:twitter-search-component.adoc[twitter-search] |List<twitter4j.Status> |
|xref:twitter-timeline-component.adoc[twitter-timeline] |String |Only 'user' timelineType is supported for producer
|==============================

### Message headers

[width="100%",cols="20%,80%",options="header",]
|=======================================================================
|Name |Description
|`CamelTwitterKeywords` |This header is used by the search producer to
change the search key words dynamically.

|`CamelTwitterSearchLanguage` |This header can override
the option of `lang` which set the search language for the search
endpoint dynamically

|`CamelTwitterCount` |This header can override the option
of `count` which sets the max twitters that will be returned.

|`CamelTwitterNumberOfPages` |This header can override
the option of `numberOfPages` which sets how many pages we want to
twitter returns.
|=======================================================================

### Message body

All message bodies utilize objects provided by the Twitter4J API.

### Use cases

NOTE: *API Rate Limits:* Twitter REST APIs encapsulated by http://twitter4j.org/[Twitter4J] are
subjected to https://dev.twitter.com/rest/public/rate-limiting[API Rate
Limiting]. You can find the per method limits in the
https://dev.twitter.com/rest/public/rate-limits[API Rate Limits]
documentation. Note that endpoints/resources not listed in that page are
default to 15 requests per allotted user per window.

#### To create a status update within your Twitter profile, send this producer a String body:

[source,java]
----
from("direct:foo")
  .to("twitter-timeline://user?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]);
----

#### To poll, every 60 sec., all statuses on your home timeline:

[source,java]
----
from("twitter-timeline://home?type=polling&delay=60&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]")
  .to("bean:blah");
----

#### To search for all statuses with the keyword 'camel' only once:

[source,java]
----
from("twitter-search://foo?type=polling&keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]")
  .to("bean:blah");
----

#### Searching using a producer with static keywords:

[source,java]
----
from("direct:foo")
  .to("twitter-search://foo?keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
----

#### Searching using a producer with dynamic keywords from header:

In the `bar` header we have the keywords we want to search, so we can
assign this value to the `CamelTwitterKeywords` header:

[source,java]
----
from("direct:foo")
  .setHeader("CamelTwitterKeywords", header("bar"))
  .to("twitter-search://foo?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
----

### Example

See also the xref:twitter-websocket-example.adoc[Twitter Websocket
Example].

### See Also

* Configuring Camel
* Component
* Endpoint
* Getting Started

* Twitter Websocket Example
