Posting feeds to IRC with Pircbot and Spring Integration 2.0
Hi!
Please view the official documentation for more information about Spring Integration 2.0
This will show you how to combine Spring Integration with the PircBot IRC-framework in order to create a bot that can post new updates to IRC.
Before you start off, you need Maven and eclipse. You can manage to do this without eclipse, but I guess you need to do some steps more manually.
First off, we create a file structure for our new project, make the folders like this:
MyProject
-main
–src
—java
–resources
Create the maven pom.xml in the root folder, and add the following contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.saiboten.integrationfeed</groupId> <artifactId>integrationfeed</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>integrationfeed</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-feed</artifactId> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>rome</groupId> <artifactId>rome</artifactId> <version>0.9</version> </dependency> <dependency> <groupId>pircbot</groupId> <artifactId>pircbot</artifactId> <version>1.4.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies> </project> |
This will get our spring integration dependencies, the pircbot framework, rome for feed parsing and I also included log4j for good measures.
Open a terminal window, maneuver to the root folder and write: mvn eclipse:eclipse to create the project. Import the project into eclipse by using import -> existing projects into workspace.
First we need to create the bot, add a new package declaration, create a new class in that package with the name of your bot, I called mine SaibotBot, and it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package no.saiboten.pircbot; import org.jibble.pircbot.PircBot; public class SaibotBot extends PircBot { public SaibotBot() { this.setName("saibotboten"); } public void startBot() throws Exception { // Enable debugging output. this.setVerbose(true); // Connect to the IRC server. this.connect("irc.freenode.net"); // Join your channel. this.joinChannel("#channelname"); } } |
Next, we need to define an application context for configuring the spring integration parts. We need a channel where messages can be sent, we need a message handler to handle message sent to the channel, we need a channel adapter to read a feed and a poller attached to it in order to define how often we should read the feed for new updates. In this example, it will read from my twitter feed. The polling is sat to 600000 milliseconds, that’s 10 minutes. The service activator is used to connect a channel to a message handler. I also defined the bot here in order to get a singleton object of this class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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:int-feed="http://www.springframework.org/schema/integration/feed" xmlns:si="http://www.springframework.org/schema/integration" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/integration/feed http://www.springframework.org/schema/integration/feed/spring-integration-feed-2.0.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd "> <si:channel id="feedChannel" /> <!-- Saiboten --> <int-feed:inbound-channel-adapter channel="feedChannel" url="https://twitter.com/statuses/user_timeline/14285281.rss"> <si:poller fixed-rate="600000" max-messages-per-poll="100" /> </int-feed:inbound-channel-adapter> <si:service-activator input-channel="feedChannel" ref="messageHandler" /> <bean id="messageHandler" class="no.saiboten.integration.feed.MessageHandler"> <constructor-arg ref="bot" /> </bean> <bean id="bot" class="no.saiboten.pircbot.SaibotBot" /> </beans> |
To Handle the messages retrieved, we need a message handlers. It will need to post the messages to your channel, so it needs to know about your bot. The class looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package no.saiboten.integration.feed; import no.saiboten.pircbot.SaibotBot; import org.springframework.integration.Message; import com.sun.syndication.feed.synd.SyndEntryImpl; public class MessageHandler { SaibotBot bot; public MessageHandler(SaibotBot bot) { this.bot = bot; } public void handleMessage(Message<SyndEntryImpl> message) { SyndEntryImpl syndFeed = message.getPayload(); bot.sendMessage("#channelname", syndFeed.getTitle()); } } |
Next, create a bootstrap class that will use springs applicationContext and launch the bot. A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package no.saiboten.integration.main; import java.io.IOException; import no.saiboten.pircbot.SaibotBot; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BootStrapper { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); SaibotBot bot = (SaibotBot) context.getBean("bot"); try { bot.startBot(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
Aright! That’s it! Make sure you don’t poll to often, as some sites have limitations on how often your can read their feeds.






