Blame
Date:
Tue Dec 21 08:11:28 2021 UTC
Message:
Added README to install dependencies
01
2021-12-17
jrmu
================================================================================
02
2021-12-17
jrmu
03
2021-12-17
jrmu
RSSBot Explained
04
2021-12-17
jrmu
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.
07
2021-12-17
jrmu
08
2021-12-17
jrmu
To learn more about RSS, visit: https://rss.softwaregarden.com/aboutrss.html
09
2021-12-17
jrmu
10
2021-12-17
jrmu
In RSSBot, we use the XML::RSS::Parser module from CPAN:
11
2021-12-17
jrmu
12
2021-12-17
jrmu
use XML::RSS::Parser;
13
2021-12-17
jrmu
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:
16
2021-12-17
jrmu
17
2021-12-17
jrmu
my $url = 'https://wiki.ircnow.org/index.php?n=Site.AllRecentChanges?action=rss';
18
2021-12-17
jrmu
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.
21
2021-12-17
jrmu
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:
24
2021-12-17
jrmu
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>
30
2021-12-17
jrmu
</item>
31
2021-12-17
jrmu
<item>
32
2021-12-17
jrmu
33
2021-12-17
jrmu
Let's take a look at inside the subroutine said:
34
2021-12-17
jrmu
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,
48
2021-12-17
jrmu
);
49
2021-12-17
jrmu
}
50
2021-12-17
jrmu
}
51
2021-12-17
jrmu
}
52
2021-12-17
jrmu
53
2021-12-17
jrmu
First, we check if a user types a message that begins with !rss
54
2021-12-17
jrmu
55
2021-12-17
jrmu
if ($arguments->{body} =~ /^!rss/) {
56
2021-12-17
jrmu
57
2021-12-17
jrmu
If the user does, then we create a new Parser object and assign this to $p:
58
2021-12-17
jrmu
59
2021-12-17
jrmu
my $p = XML::RSS::Parser->new;
60
2021-12-17
jrmu
61
2021-12-17
jrmu
Next, we parse (analyze) the feed using $p, and assign this to $feed:
62
2021-12-17
jrmu
63
2021-12-17
jrmu
my $feed = $p->parse_uri($url);
64
2021-12-17
jrmu
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.
69
2021-12-17
jrmu
70
2021-12-17
jrmu
foreach my $i ( $feed->query('//item') ) {
71
2021-12-17
jrmu
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.
74
2021-12-17
jrmu
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');
78
2021-12-17
jrmu
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.
81
2021-12-17
jrmu
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,
85
2021-12-17
jrmu
);
86
2021-12-17
jrmu
87
2021-12-17
jrmu
================================================================================
88
2021-12-17
jrmu
89
2021-12-17
jrmu
Further Reading
90
2021-12-17
jrmu
91
2021-12-17
jrmu
XML::RSS::Parser module: https://metacpan.org/pod/XML::RSS::Parser
92
2021-12-17
jrmu
93
2021-12-17
jrmu
================================================================================
94
2021-12-17
jrmu
95
2021-12-17
jrmu
Learn about Loops
96
2021-12-17
jrmu
97
2021-12-17
jrmu
View the file ~/control to learn about control structures for perl.
98
2021-12-17
jrmu
99
2021-12-17
jrmu
================================================================================
IRCNow