Search in sources :

Example 1 with TwitterTemplate

use of org.springframework.social.twitter.api.impl.TwitterTemplate in project Gemma by PavlidisLab.

the class TwitterOutboundImpl method sendManualTweet.

@Override
@Secured({ "GROUP_ADMIN" })
public void sendManualTweet(String feed) {
    TwitterOutboundImpl.log.debug("Checking if Twitter is enabled");
    if (!Settings.getBoolean("gemma.twitter.enabled")) {
        TwitterOutboundImpl.log.info("Twitter is disabled.");
        return;
    }
    if (StringUtils.isNotBlank(feed)) {
        TwitterOutboundImpl.log.info("Sending out tweet: '" + feed + "'");
        String consumerKey = Settings.getString("twitter.consumer-key");
        String consumerSecret = Settings.getString("twitter.consumer-secret");
        String accessToken = Settings.getString("twitter.access-token");
        String accessTokenSecret = Settings.getString("twitter.access-token-secret");
        Twitter twitter = new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
        StatusDetails metadata = new StatusDetails();
        metadata.setWrapLinks(true);
        try {
            Tweet tweet = twitter.timelineOperations().updateStatus(feed, metadata);
            TwitterOutboundImpl.log.info("tweet info:" + tweet.toString());
        } catch (Exception e) {
            TwitterOutboundImpl.log.info(e.toString());
            e.printStackTrace();
        }
    }
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) Tweet(org.springframework.social.twitter.api.Tweet) StatusDetails(org.springframework.social.twitter.api.StatusDetails) Twitter(org.springframework.social.twitter.api.Twitter) Secured(org.springframework.security.access.annotation.Secured)

Example 2 with TwitterTemplate

use of org.springframework.social.twitter.api.impl.TwitterTemplate in project spring-integration by spring-projects.

the class SearchReceivingMessageSourceTests method testSearchReceivingMessageSourceInit.

/**
 * Unit Test ensuring some basic initialization properties being set.
 */
@Test
public void testSearchReceivingMessageSourceInit() {
    final SearchReceivingMessageSource messageSource = new SearchReceivingMessageSource(new TwitterTemplate("test"), "foo");
    messageSource.setComponentName("twitterSearchMessageSource");
    final Object metadataStore = TestUtils.getPropertyValue(messageSource, "metadataStore");
    final Object metadataKey = TestUtils.getPropertyValue(messageSource, "metadataKey");
    assertNull(metadataStore);
    assertNotNull(metadataKey);
    messageSource.setBeanFactory(mock(BeanFactory.class));
    messageSource.afterPropertiesSet();
    final Object metadataStoreInitialized = TestUtils.getPropertyValue(messageSource, "metadataStore");
    final Object metadataKeyInitialized = TestUtils.getPropertyValue(messageSource, "metadataKey");
    assertNotNull(metadataStoreInitialized);
    assertTrue(metadataStoreInitialized instanceof SimpleMetadataStore);
    assertNotNull(metadataKeyInitialized);
    assertEquals("foo", metadataKeyInitialized);
    final Twitter twitter = TestUtils.getPropertyValue(messageSource, "twitter", Twitter.class);
    assertFalse(twitter.isAuthorized());
    assertNotNull(twitter.userOperations());
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) BeanFactory(org.springframework.beans.factory.BeanFactory) Twitter(org.springframework.social.twitter.api.Twitter) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Test(org.junit.Test)

Example 3 with TwitterTemplate

use of org.springframework.social.twitter.api.impl.TwitterTemplate in project spring-integration by spring-projects.

the class DirectMessageReceivingMessageSourceTests method demoReceiveDm.

@SuppressWarnings("unchecked")
@Test
@Ignore
public void demoReceiveDm() throws Exception {
    PropertiesFactoryBean pf = new PropertiesFactoryBean();
    pf.setLocation(new ClassPathResource("sample.properties"));
    pf.afterPropertiesSet();
    Properties prop = pf.getObject();
    TwitterTemplate template = new TwitterTemplate(prop.getProperty("z_oleg.oauth.consumerKey"), prop.getProperty("z_oleg.oauth.consumerSecret"), prop.getProperty("z_oleg.oauth.accessToken"), prop.getProperty("z_oleg.oauth.accessTokenSecret"));
    DirectMessageReceivingMessageSource tSource = new DirectMessageReceivingMessageSource(template, "foo");
    tSource.afterPropertiesSet();
    for (int i = 0; i < 50; i++) {
        Message<DirectMessage> message = (Message<DirectMessage>) tSource.receive();
        if (message != null) {
            DirectMessage tweet = message.getPayload();
            logger.info(tweet.getSender().getScreenName() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
        }
    }
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) DirectMessage(org.springframework.social.twitter.api.DirectMessage) Message(org.springframework.messaging.Message) DirectMessage(org.springframework.social.twitter.api.DirectMessage) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with TwitterTemplate

use of org.springframework.social.twitter.api.impl.TwitterTemplate in project spring-integration by spring-projects.

the class DirectMessageSendingMessageHandlerTests method validateSendDirectMessage.

@Test
@Ignore
public void validateSendDirectMessage() throws Exception {
    PropertiesFactoryBean pf = new PropertiesFactoryBean();
    pf.setLocation(new ClassPathResource("sample.properties"));
    pf.afterPropertiesSet();
    Properties prop = pf.getObject();
    TwitterTemplate template = new TwitterTemplate(prop.getProperty("spring_eip.oauth.consumerKey"), prop.getProperty("spring_eip.oauth.consumerSecret"), prop.getProperty("spring_eip.oauth.accessToken"), prop.getProperty("spring_eip.oauth.accessTokenSecret"));
    Message<?> message1 = MessageBuilder.withPayload("Polsihing SI Twitter migration").setHeader(TwitterHeaders.DM_TARGET_USER_ID, "z_oleg").build();
    DirectMessageSendingMessageHandler handler = new DirectMessageSendingMessageHandler(template);
    handler.afterPropertiesSet();
    handler.handleMessage(message1);
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with TwitterTemplate

use of org.springframework.social.twitter.api.impl.TwitterTemplate in project Gemma by PavlidisLab.

the class TwitterOutboundImpl method sendDailyFeed.

@Override
@Secured({ "GROUP_AGENT" })
public void sendDailyFeed() {
    TwitterOutboundImpl.log.debug("Checking if Twitter is enabled");
    if (!TwitterOutboundImpl.enabled.get()) {
        return;
    }
    String feed = this.generateDailyFeed();
    TwitterOutboundImpl.log.info("Twitter is enabled. Checking if Twitter feed is empty.");
    if (StringUtils.isNotBlank(feed)) {
        TwitterOutboundImpl.log.info("Sending out tweet: '" + feed + "'");
        String consumerKey = Settings.getString("twitter.consumer-key");
        String consumerSecret = Settings.getString("twitter.consumer-secret");
        String accessToken = Settings.getString("twitter.access-token");
        String accessTokenSecret = Settings.getString("twitter.access-token-secret");
        Twitter twitter = new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
        StatusDetails metadata = new StatusDetails();
        metadata.setWrapLinks(true);
        try {
            Tweet tweet = twitter.timelineOperations().updateStatus(feed, metadata);
            TwitterOutboundImpl.log.info("tweet info:" + tweet.toString());
        } catch (Exception e) {
            TwitterOutboundImpl.log.info(e.toString());
        }
    }
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) Tweet(org.springframework.social.twitter.api.Tweet) StatusDetails(org.springframework.social.twitter.api.StatusDetails) Twitter(org.springframework.social.twitter.api.Twitter) Secured(org.springframework.security.access.annotation.Secured)

Aggregations

TwitterTemplate (org.springframework.social.twitter.api.impl.TwitterTemplate)10 Test (org.junit.Test)8 Tweet (org.springframework.social.twitter.api.Tweet)6 Properties (java.util.Properties)5 Ignore (org.junit.Ignore)5 PropertiesFactoryBean (org.springframework.beans.factory.config.PropertiesFactoryBean)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 Message (org.springframework.messaging.Message)3 Twitter (org.springframework.social.twitter.api.Twitter)3 Secured (org.springframework.security.access.annotation.Secured)2 SearchOperations (org.springframework.social.twitter.api.SearchOperations)2 StatusDetails (org.springframework.social.twitter.api.StatusDetails)2 ArrayList (java.util.ArrayList)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 SimpleMetadataStore (org.springframework.integration.metadata.SimpleMetadataStore)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 DirectMessage (org.springframework.social.twitter.api.DirectMessage)1 SearchMetadata (org.springframework.social.twitter.api.SearchMetadata)1 SearchParameters (org.springframework.social.twitter.api.SearchParameters)1 SearchResults (org.springframework.social.twitter.api.SearchResults)1