DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Spring Integration with Twitter: How to Send Direct Message [DM] to a Twitter Account?

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

Jagadeesh Motamarri user avatar by
Jagadeesh Motamarri
·
Mar. 14, 13 · Interview
Like (0)
Save
Tweet
Share
4.39K Views

Join the DZone community and get the full member experience.

Join For Free
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



Dialog manager twitter Spring Integration Spring Framework Integration

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Create a CLI Chatbot With the ChatGPT API and Node.js
  • Demystifying the Infrastructure as Code Landscape
  • Choosing the Right Framework for Your Project
  • When Should We Move to Microservices?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: