Over a million developers have joined DZone.

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?

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

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.


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

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();


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

Twitter Support

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.


Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}