Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

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



With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}