Over a million developers have joined DZone.
Platinum Partner

Spring Integration with Twitter: How to Send Direct Message [DM] to a Twitter Account?

The Integration Zone is brought to you in partnership with 3scale. Take control of your APIs and get a free t-shirt when you complete the 3step Challenge

Spring Integration Module can also send Direct Messages (DM) to a Twitter Account using Spring Twitter Adapters.

What is Twitter DM?

A direct message (DM) is a private message sent via Twitter to one of your followers. You can only send a direct message to a user who is following you; you can only receive direct messages from users you follow.

In order to test this adapter, you will need 2 Twitter Account where one of the Account User is following the other Twitter Account.

In the below example, @SkilledMonster is a follower of @SkilledTester.Below DM message is posted by @SkilledTester to @SkilledMonster.

Spring Configuration

Spring Twitter Outbound Adapter provides support for send a DM to a Twitter account. This will send a private message to a specific user who is a follower. This configuration is similar to the one that we have seen in my previous post on Spring Twitter Update Adapter.

twitter-dm-outbound.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:twitter="http://www.springframework.org/schema/integration/twitter"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/integration/twitter
 
http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd
 
 
http://www.springframework.org/schema/beans
 
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 
 
http://www.springframework.org/schema/integration
 
 
http://www.springframework.org/schema/integration/spring-integration.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:property-placeholder location="/twitter.properties" />
 
    <context:component-scan base-package="com.skilledmonster.spring.integration.twitter" />
 
    <int:channel id="twitterOutbound" />
 
    <twitter:dm-outbound-channel-adapter
        channel="twitterOutbound" twitter-template="twitterTemplate" />
 
</beans>


Test Run
In order to send a DM to a Twitter Account using the below test class, you need to set the target user through the header TwitterHeaders.DM_TARGET_USER_ID. The below test code sets the header value to send a DM to Twitter Account # @SkilledMonster

package com.skilledmonster.spring.integration.twitter;
 
import java.util.Calendar;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.TwitterHeaders;
 
/**
 * Send a DM to Twitter Account
 * @author Jagadeesh
 */
public class TwitterDMOutbound {
 
    public static void main(String[] args) {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    "/twitter-dm-outbound.xml", TwitterDMOutbound.class);
 
            MessageChannel input = context.getBean("twitterOutbound", MessageChannel.class);
            Message<String> message = MessageBuilder.withPayload(Calendar.getInstance().getTime()+ " Direct Message posted using Spring Integration Module")
                    .setHeader(TwitterHeaders.DM_TARGET_USER_ID, "SkilledMonster").build();
            input.send(message);
            context.stop();
    }
}

Output

Here is a snapshot of the DM received from @SkilledTester to @SkilledMonster
 
References:

Twitter Support



The Integration Zone is brought to you in partnership with 3scale. Learn how API providers have changed the way we think about integration in The Platform Vision of API Giants.

Topics:

Published at DZone with permission of Jagadeesh Motamarri , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}