01
2021-12-17
jrmu
================================================================================
03
2021-12-17
jrmu
RSSBot Explained
05
2021-12-17
jrmu
News feeds are generally handled using RSS feeds. RSS is an open format that
06
2021-12-17
jrmu
allows websites to quickly show which articles have been updated recently.
08
2021-12-17
jrmu
To learn more about RSS, visit: https://rss.softwaregarden.com/aboutrss.html
10
2021-12-17
jrmu
In RSSBot, we use the XML::RSS::Parser module from CPAN:
12
2021-12-17
jrmu
use XML::RSS::Parser;
14
2021-12-17
jrmu
Many of your favorite websites have RSS feeds. Use a search engine to find
15
2021-12-17
jrmu
them. In this example, we will use IRCNow's Almanack:
17
2021-12-17
jrmu
my $url = 'https://wiki.ircnow.org/index.php?n=Site.AllRecentChanges?action=rss';
19
2021-12-17
jrmu
You can replace this URL with the RSS feed from your favorite website to
20
2021-12-17
jrmu
change the news that your bot displays.
22
2021-12-17
jrmu
We recommend you download an RSS feed and open it with a text editor to
23
2021-12-17
jrmu
see what the RSS format looks like. Here is one sample:
25
2021-12-17
jrmu
<title>Ircnow / Servers</title>
26
2021-12-17
jrmu
<link>https://wiki.ircnow.org/index.php?n=Ircnow.Servers</link>
27
2021-12-17
jrmu
<dc:contributor>mkf</dc:contributor>
28
2021-12-17
jrmu
<dc:date>2021-08-29T15:27:58Z</dc:date>
29
2021-12-17
jrmu
<pubDate>Sun, 29 Aug 2021 15:27:58 GMT</pubDate>
33
2021-12-17
jrmu
Let's take a look at inside the subroutine said:
35
2021-12-17
jrmu
sub said {
36
2021-12-17
jrmu
my $self = shift;
37
2021-12-17
jrmu
my $arguments = shift;
38
2021-12-17
jrmu
if ($arguments->{body} =~ /^!rss/) {
39
2021-12-17
jrmu
my $p = XML::RSS::Parser->new;
40
2021-12-17
jrmu
my $feed = $p->parse_uri($url);
41
2021-12-17
jrmu
foreach my $i ( $feed->query('//item') ) {
42
2021-12-17
jrmu
my $title = $i->query('title');
43
2021-12-17
jrmu
my $contributor = $i->query('dc:contributor');
44
2021-12-17
jrmu
my $link = $i->query('link');
45
2021-12-17
jrmu
$self->say(
46
2021-12-17
jrmu
channel => $arguments->{channel},
47
2021-12-17
jrmu
body => $title->text_content.' - '.$contributor->text_content.': '.$link->text_content,
53
2021-12-17
jrmu
First, we check if a user types a message that begins with !rss
55
2021-12-17
jrmu
if ($arguments->{body} =~ /^!rss/) {
57
2021-12-17
jrmu
If the user does, then we create a new Parser object and assign this to $p:
59
2021-12-17
jrmu
my $p = XML::RSS::Parser->new;
61
2021-12-17
jrmu
Next, we parse (analyze) the feed using $p, and assign this to $feed:
63
2021-12-17
jrmu
my $feed = $p->parse_uri($url);
65
2021-12-17
jrmu
RSS feeds contain a list of news items, and we need to process each item
66
2021-12-17
jrmu
one at a time. For this task, we will use a foreach loop. We are going
67
2021-12-17
jrmu
to query (ask) $feed for all items, then use the foreach loop to iterate
68
2021-12-17
jrmu
through each item. We assign each item to $i.
70
2021-12-17
jrmu
foreach my $i ( $feed->query('//item') ) {
72
2021-12-17
jrmu
Next, we query $i to find the title, contributor, and link of each item.
73
2021-12-17
jrmu
We assign these values to $title, $contributor, and $link.
75
2021-12-17
jrmu
my $title = $i->query('title');
76
2021-12-17
jrmu
my $contributor = $i->query('dc:contributor');
77
2021-12-17
jrmu
my $link = $i->query('link');
79
2021-12-17
jrmu
For each item, we send a message to the channel with the title, contributor,
80
2021-12-17
jrmu
and link.
82
2021-12-17
jrmu
$self->say(
83
2021-12-17
jrmu
channel => $arguments->{channel},
84
2021-12-17
jrmu
body => $title->text_content.' - '.$contributor->text_content.': '.$link->text_content,
87
2021-12-17
jrmu
================================================================================
89
2021-12-17
jrmu
Further Reading
91
2021-12-17
jrmu
XML::RSS::Parser module: https://metacpan.org/pod/XML::RSS::Parser
93
2021-12-17
jrmu
================================================================================
95
2021-12-17
jrmu
Learn about Loops
97
2021-12-17
jrmu
View the file ~/control to learn about control structures for perl.
99
2021-12-17
jrmu
================================================================================